Skip to content

Commit

Permalink
Merge d3eac68 into 9bf816b
Browse files Browse the repository at this point in the history
  • Loading branch information
sibalonat committed Jan 7, 2024
2 parents 9bf816b + d3eac68 commit c45f550
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 c45f550

Please sign in to comment.