-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from veewee/xpath-xslt-callback-functions
Introduce XPath and XSLT callback functions
- Loading branch information
Showing
11 changed files
with
183 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Xpath\Configurator; | ||
|
||
use Closure; | ||
use Dom\XPath; | ||
|
||
/** | ||
* @param array<string, (callable(mixed...): mixed)> $functions | ||
* | ||
* @return Closure(XPath): XPath | ||
*/ | ||
function namespaced_functions(string $namespace, string $prefix, array $functions): Closure | ||
{ | ||
return static function (XPath $xpath) use ($namespace, $prefix, $functions) : XPath { | ||
namespaces([$prefix => $namespace])($xpath); | ||
foreach ($functions as $functionName => $callback) { | ||
$xpath->registerPhpFunctionNS($namespace, $functionName, $callback); | ||
} | ||
|
||
return $xpath; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Xslt\Configurator; | ||
|
||
use Closure; | ||
use XSLTProcessor; | ||
|
||
/** | ||
* @param array<string, (callable(mixed...): mixed)> $functions | ||
* | ||
* @return Closure(XSLTProcessor): XSLTProcessor | ||
*/ | ||
function namespaced_functions(string $namespace, array $functions): Closure | ||
{ | ||
return static function (XSLTProcessor $processor) use ($namespace, $functions) : XSLTProcessor { | ||
foreach ($functions as $functionName => $callback) { | ||
$processor->registerPhpFunctionNS($namespace, $functionName, $callback); | ||
} | ||
|
||
return $processor; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/Xml/Dom/Xpath/Configurator/NamespacedFunctionsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Xpath\Configurator; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Type; | ||
use VeeWee\Xml\Dom\Document; | ||
use VeeWee\Xml\Dom\Xpath; | ||
|
||
final class NamespacedFunctionsTest extends TestCase | ||
{ | ||
public function test_it_can_evaluate_with_php_function(): void | ||
{ | ||
$doc = Document::fromXmlString( | ||
$xml = '<hello><item>Jos</item></hello>' | ||
); | ||
$xpath = Xpath::fromDocument($doc, Xpath\Configurator\namespaced_functions( | ||
'http://my-ns', | ||
'my', | ||
['str_replace' => str_replace(...)] | ||
)); | ||
|
||
$result = $xpath->evaluate('my:str_replace("J", "B", string(//item))', Type\string()); | ||
static::assertSame($result, 'Bos'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Xslt; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Xml\Dom\Document; | ||
use VeeWee\Xml\Exception\RuntimeException; | ||
use VeeWee\Xml\Xslt\Processor; | ||
use function VeeWee\Xml\Xslt\Configurator\namespaced_functions; | ||
|
||
final class NamespacedFunctionsTest extends TestCase | ||
{ | ||
public function test_it_can_use_php_functions(): void | ||
{ | ||
$processor = Processor::fromTemplateDocument( | ||
$this->createTemplate(), | ||
namespaced_functions('http://my-xls', ['strtoupper' => strtoupper(...)]) | ||
); | ||
|
||
$result = $processor->transformDocumentToString( | ||
Document::fromXmlString( | ||
<<<EOXML | ||
<root> | ||
<hello>World</hello> | ||
</root> | ||
EOXML | ||
) | ||
); | ||
|
||
static::assertSame('WORLD', $result); | ||
} | ||
|
||
public function test_it_throws_exception_on_unkown_function(): void | ||
{ | ||
$processor = Processor::fromTemplateDocument( | ||
$this->createTemplate(), | ||
namespaced_functions('http://my-xls', ['substr' => substr(...)]) | ||
); | ||
$doc = Document::fromXmlString( | ||
<<<EOXML | ||
<root> | ||
<hello>World</hello> | ||
</root> | ||
EOXML | ||
); | ||
|
||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('strtoupper'); | ||
$processor->transformDocumentToString($doc); | ||
} | ||
|
||
private function createTemplate(): Document | ||
{ | ||
return Document::fromXmlString( | ||
<<<EOXML | ||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | ||
xmlns:my="http://my-xls" | ||
xmlns:str="http://exslt.org/strings" | ||
xmlns:xsdl="http://www.w3.org/1999/XSL/Transform"> | ||
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/> | ||
<xsl:template match="/root"> | ||
<xsl:value-of select="my:strtoupper(string(./hello))"/> | ||
</xsl:template> | ||
</xsl:stylesheet> | ||
EOXML | ||
); | ||
} | ||
} |