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