Skip to content

Commit 98d038e

Browse files
sibalonatoleibmandependabot[bot]Progi1984
authored
Added extractVariables method to IOFactory (#2515)
* Added extractVariables method to IOFactory * remove var_dumps * remove vardump * fix return and fix php stan errors and instances * fix order and whitespace * extra space * remove new lines * white space * new lines * whiteline * new line * white space * fix * new line * white space * remove some unneecessary if statement * Correct Font Size Calculated by MsDoc Reader Fix #2526. Most of that issue has already been fixed. The one remaining problem was a deprecation message handling font size. The code used `dechex($operand / 2)`, and issued the deprecation message whenever `$operand` was odd because `dechex` is designed only for integer conversion. `$operand` is actually 2 times the point size, so it will be odd only when the point size is some integer plus half a point (no other fractions are allowed). At any rate, it seems that `dechex` should not be used here in the first place; font size is a numeric value, not a hex string. There are many problems with MsDoc Reader at the moment. This PR is narrowly focused on the problem at hand. Its test is, at least, more detailed than the existing MsDoc Reader test, which does nothing more than confirm that read successfully creates a PhpWord object. The new test verifies that the font size is as expected, but does not validate any other aspect of the read. * Suggestions from @Progi1984 * Typo Tolerated By Windows but Not By Unix * Correct Title Line of Change Log * Bump dompdf/dompdf from 2.0.3 to 2.0.4 Bumps [dompdf/dompdf](https://github.com/dompdf/dompdf) from 2.0.3 to 2.0.4. - [Release notes](https://github.com/dompdf/dompdf/releases) - [Commits](dompdf/dompdf@v2.0.3...v2.0.4) --- updated-dependencies: - dependency-name: dompdf/dompdf dependency-type: direct:development ... Signed-off-by: dependabot[bot] <[email protected]> * Bump phpunit/phpunit from 9.6.13 to 9.6.14 Bumps [phpunit/phpunit](https://github.com/sebastianbergmann/phpunit) from 9.6.13 to 9.6.14. - [Changelog](https://github.com/sebastianbergmann/phpunit/blob/9.6.14/ChangeLog-9.6.md) - [Commits](sebastianbergmann/phpunit@9.6.13...9.6.14) --- updated-dependencies: - dependency-name: phpunit/phpunit dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Bump mpdf/mpdf from 8.2.0 to 8.2.2 Bumps [mpdf/mpdf](https://github.com/mpdf/mpdf) from 8.2.0 to 8.2.2. - [Release notes](https://github.com/mpdf/mpdf/releases) - [Changelog](https://github.com/mpdf/mpdf/blob/development/CHANGELOG.md) - [Commits](mpdf/mpdf@v8.2.0...v8.2.2) --- updated-dependencies: - dependency-name: mpdf/mpdf dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Bump phpmd/phpmd from 2.14.1 to 2.15.0 Bumps [phpmd/phpmd](https://github.com/phpmd/phpmd) from 2.14.1 to 2.15.0. - [Release notes](https://github.com/phpmd/phpmd/releases) - [Changelog](https://github.com/phpmd/phpmd/blob/master/CHANGELOG) - [Commits](phpmd/phpmd@2.14.1...2.15.0) --- updated-dependencies: - dependency-name: phpmd/phpmd dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Updated Changelog * Bump phpunit/phpunit from 9.6.14 to 9.6.15 Bumps [phpunit/phpunit](https://github.com/sebastianbergmann/phpunit) from 9.6.14 to 9.6.15. - [Changelog](https://github.com/sebastianbergmann/phpunit/blob/9.6.15/ChangeLog-9.6.md) - [Commits](sebastianbergmann/phpunit@9.6.14...9.6.15) --- updated-dependencies: - dependency-name: phpunit/phpunit dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Updated Changelog * Bump symfony/process from 5.4.28 to 5.4.34 Bumps [symfony/process](https://github.com/symfony/process) from 5.4.28 to 5.4.34. - [Release notes](https://github.com/symfony/process/releases) - [Changelog](https://github.com/symfony/process/blob/7.0/CHANGELOG.md) - [Commits](symfony/process@v5.4.28...v5.4.34) --- updated-dependencies: - dependency-name: symfony/process dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * Updated Changelog * Added extractVariables method to IOFactory * remove var_dumps * fix return and fix php stan errors and instances * fix order and whitespace * remove new lines * white space * new lines * whiteline * new line * white space * fix * white space * remove some unneecessary if statement * variable changes * 2.0.0 md * 2.0.0 * new line at end of the file * add entry to md * additional space, type fixes in enhancement md and iofactory * some fixes on spacing * based on fixer suggestions * new line at the end of the file --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: oleibman <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Progi1984 <[email protected]>
1 parent 9bf816b commit 98d038e

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Enhancements
66

7+
- IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515)
8+
79
### Bug fixes
810

911
- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
include_once 'Sample_Header.php';
4+
5+
// Read contents
6+
$name = basename(__FILE__, '.php');
7+
8+
$source = __DIR__ . "/resources/{$name}.docx";
9+
10+
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
11+
12+
$variables = \PhpOffice\PhpWord\IOFactory::extractVariables($source);
13+
14+
var_dump($variables);
Binary file not shown.

src/PhpWord/IOFactory.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord;
1919

20+
use PhpOffice\PhpWord\Element\Text;
21+
use PhpOffice\PhpWord\Element\TextRun;
2022
use PhpOffice\PhpWord\Exception\Exception;
2123
use PhpOffice\PhpWord\Reader\ReaderInterface;
2224
use PhpOffice\PhpWord\Writer\WriterInterface;
@@ -89,6 +91,43 @@ public static function load($filename, $readerName = 'Word2007')
8991
return $reader->load($filename);
9092
}
9193

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+
92131
/**
93132
* Check if it's a concrete class (not abstract nor interface).
94133
*

tests/PhpWordTests/IOFactoryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,17 @@ public function testLoad(): void
116116
IOFactory::load($file)
117117
);
118118
}
119+
120+
/**
121+
* Test for extractVariables method.
122+
*/
123+
public function testExtractVariables(): void
124+
{
125+
$file = __DIR__ . '/_files/templates/extract-variable.docx';
126+
$extractedVariables = IOFactory::extractVariables($file, 'Word2007');
127+
128+
$expectedVariables = ['date', 'A1', 'B1'];
129+
130+
self::assertEquals($expectedVariables, $extractedVariables, 'Extracted variables do not match expected variables.');
131+
}
119132
}
Binary file not shown.

0 commit comments

Comments
 (0)