Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
IOFactory | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
18 | |
100.00% |
1 / 1 |
createWriter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
createReader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createObject | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
load | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
extractVariables | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
8 | |||
isConcreteClass | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord; |
19 | |
20 | use PhpOffice\PhpWord\Element\Text; |
21 | use PhpOffice\PhpWord\Element\TextRun; |
22 | use PhpOffice\PhpWord\Exception\Exception; |
23 | use PhpOffice\PhpWord\Reader\ReaderInterface; |
24 | use PhpOffice\PhpWord\Writer\WriterInterface; |
25 | use ReflectionClass; |
26 | |
27 | abstract class IOFactory |
28 | { |
29 | /** |
30 | * Create new writer. |
31 | * |
32 | * @param string $name |
33 | * |
34 | * @return WriterInterface |
35 | */ |
36 | public static function createWriter(PhpWord $phpWord, $name = 'Word2007') |
37 | { |
38 | if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) { |
39 | throw new Exception("\"{$name}\" is not a valid writer."); |
40 | } |
41 | |
42 | $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}"; |
43 | |
44 | return new $fqName($phpWord); |
45 | } |
46 | |
47 | /** |
48 | * Create new reader. |
49 | * |
50 | * @param string $name |
51 | * |
52 | * @return ReaderInterface |
53 | */ |
54 | public static function createReader($name = 'Word2007') |
55 | { |
56 | return self::createObject('Reader', $name); |
57 | } |
58 | |
59 | /** |
60 | * Create new object. |
61 | * |
62 | * @param string $type |
63 | * @param string $name |
64 | * @param \PhpOffice\PhpWord\PhpWord $phpWord |
65 | * |
66 | * @return \PhpOffice\PhpWord\Reader\ReaderInterface|\PhpOffice\PhpWord\Writer\WriterInterface |
67 | */ |
68 | private static function createObject($type, $name, $phpWord = null) |
69 | { |
70 | $class = "PhpOffice\\PhpWord\\{$type}\\{$name}"; |
71 | if (class_exists($class) && self::isConcreteClass($class)) { |
72 | return new $class($phpWord); |
73 | } |
74 | |
75 | throw new Exception("\"{$name}\" is not a valid {$type}."); |
76 | } |
77 | |
78 | /** |
79 | * Loads PhpWord from file. |
80 | * |
81 | * @param string $filename The name of the file |
82 | * @param string $readerName |
83 | * |
84 | * @return \PhpOffice\PhpWord\PhpWord $phpWord |
85 | */ |
86 | public static function load($filename, $readerName = 'Word2007') |
87 | { |
88 | /** @var \PhpOffice\PhpWord\Reader\ReaderInterface $reader */ |
89 | $reader = self::createReader($readerName); |
90 | |
91 | return $reader->load($filename); |
92 | } |
93 | |
94 | /** |
95 | * Loads PhpWord ${variable} from file. |
96 | * |
97 | * @param string $filename The name of the file |
98 | * |
99 | * @return array The extracted variables |
100 | */ |
101 | public static function extractVariables(string $filename, string $readerName = 'Word2007'): array |
102 | { |
103 | /** @var \PhpOffice\PhpWord\Reader\ReaderInterface $reader */ |
104 | $reader = self::createReader($readerName); |
105 | $document = $reader->load($filename); |
106 | $extractedVariables = []; |
107 | foreach ($document->getSections() as $section) { |
108 | $concatenatedText = ''; |
109 | foreach ($section->getElements() as $element) { |
110 | if ($element instanceof TextRun) { |
111 | foreach ($element->getElements() as $textElement) { |
112 | if ($textElement instanceof Text) { |
113 | $text = $textElement->getText(); |
114 | $concatenatedText .= $text; |
115 | } |
116 | } |
117 | } |
118 | } |
119 | preg_match_all('/\$\{([^}]+)\}/', $concatenatedText, $matches); |
120 | if (!empty($matches[1])) { |
121 | foreach ($matches[1] as $match) { |
122 | $trimmedMatch = trim($match); |
123 | $extractedVariables[] = $trimmedMatch; |
124 | } |
125 | } |
126 | } |
127 | |
128 | return $extractedVariables; |
129 | } |
130 | |
131 | /** |
132 | * Check if it's a concrete class (not abstract nor interface). |
133 | * |
134 | * @param string $class |
135 | * |
136 | * @return bool |
137 | */ |
138 | private static function isConcreteClass($class) |
139 | { |
140 | $reflection = new ReflectionClass($class); |
141 | |
142 | return !$reflection->isAbstract() && !$reflection->isInterface(); |
143 | } |
144 | } |