Skip to content

Commit

Permalink
Added extractVariables method to IOFactory (#2515)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people committed Jan 8, 2024
1 parent 9bf816b commit 98d038e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Enhancements

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

### Bug fixes

- 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)
Expand Down
14 changes: 14 additions & 0 deletions samples/Sample_44_ExtractVariablesFromReaderWord2007.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

include_once 'Sample_Header.php';

// Read contents
$name = basename(__FILE__, '.php');

$source = __DIR__ . "/resources/{$name}.docx";

echo date('H:i:s'), " Reading contents from `{$source}`", EOL;

$variables = \PhpOffice\PhpWord\IOFactory::extractVariables($source);

var_dump($variables);
Binary file not shown.
39 changes: 39 additions & 0 deletions src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWord;

use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Reader\ReaderInterface;
use PhpOffice\PhpWord\Writer\WriterInterface;
Expand Down Expand Up @@ -89,6 +91,43 @@ public static function load($filename, $readerName = 'Word2007')
return $reader->load($filename);
}

/**
* Loads PhpWord ${variable} from file.
*
* @param string $filename The name of the file
*
* @return array The extracted variables
*/
public static function extractVariables(string $filename, string $readerName = 'Word2007'): array
{
/** @var \PhpOffice\PhpWord\Reader\ReaderInterface $reader */
$reader = self::createReader($readerName);
$document = $reader->load($filename);
$extractedVariables = [];
foreach ($document->getSections() as $section) {
$concatenatedText = '';
foreach ($section->getElements() as $element) {
if ($element instanceof TextRun) {
foreach ($element->getElements() as $textElement) {
if ($textElement instanceof Text) {
$text = $textElement->getText();
$concatenatedText .= $text;
}
}
}
}
preg_match_all('/\$\{([^}]+)\}/', $concatenatedText, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $match) {
$trimmedMatch = trim($match);
$extractedVariables[] = $trimmedMatch;
}
}
}

return $extractedVariables;
}

/**
* Check if it's a concrete class (not abstract nor interface).
*
Expand Down
13 changes: 13 additions & 0 deletions tests/PhpWordTests/IOFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ public function testLoad(): void
IOFactory::load($file)
);
}

/**
* Test for extractVariables method.
*/
public function testExtractVariables(): void
{
$file = __DIR__ . '/_files/templates/extract-variable.docx';
$extractedVariables = IOFactory::extractVariables($file, 'Word2007');

$expectedVariables = ['date', 'A1', 'B1'];

self::assertEquals($expectedVariables, $extractedVariables, 'Extracted variables do not match expected variables.');
}
}
Binary file not shown.

0 comments on commit 98d038e

Please sign in to comment.