Skip to content

Commit

Permalink
Differences between legacy and new API
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Feb 23, 2024
1 parent ca03288 commit d61b7bb
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 181 deletions.
2 changes: 1 addition & 1 deletion src/Xml/Dom/Builder/escaped_value.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
function escaped_value(string $value): Closure
{
return static function (\DOM\Element $node) use ($value): \DOM\Element {
$node->nodeValue = htmlspecialchars($value, ENT_XML1|ENT_QUOTES);
$node->textContent = htmlspecialchars($value, ENT_XML1|ENT_QUOTES);

return $node;
};
Expand Down
2 changes: 1 addition & 1 deletion src/Xml/Dom/Builder/value.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
function value(string $value): Closure
{
return static function (\DOM\Element $node) use ($value): \DOM\Element {
$node->nodeValue = $value;
$node->textContent = $value;

return $node;
};
Expand Down
9 changes: 9 additions & 0 deletions src/Xml/Dom/Collection/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public static function empty(): self
return new self();
}

/**
* @param \DOM\HTMLCollection $list
* @return NodeList<\DOM\Element>
*/
public static function fromDOMHTMLCollection(\DOM\HTMLCollection $list): self
{
return new self(...values($list->getIterator()));
}

/**
* @template X of \DOM\Node
* @param DOMNodeList<X> $list
Expand Down
21 changes: 0 additions & 21 deletions src/Xml/Dom/Configurator/loader.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Xml/Dom/Configurator/pretty_print.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
function pretty_print(): Closure
{
return static function (\DOM\XMLDocument $document): \DOM\XMLDocument {
$document->preserveWhiteSpace = false;
// TODO : not fully implemented yet in the new API
//$document->preserveWhiteSpace = false;
$document->formatOutput = true;

return $document;
Expand Down
3 changes: 2 additions & 1 deletion src/Xml/Dom/Configurator/trim_spaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
function trim_spaces(): Closure
{
return static function (\DOM\XMLDocument $document): \DOM\XMLDocument {
$document->preserveWhiteSpace = false;
// TODO : not fully implemented yet in the new API
//$document->preserveWhiteSpace = false;
$document->formatOutput = false;

return $document;
Expand Down
2 changes: 1 addition & 1 deletion src/Xml/Dom/Configurator/utf8.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
function utf8(): Closure
{
return static function (\DOM\XMLDocument $document): \DOM\XMLDocument {
$document->encoding = 'UTF-8';
$document->charset = 'UTF-8';

return $document;
};
Expand Down
35 changes: 17 additions & 18 deletions src/Xml/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
use VeeWee\Xml\ErrorHandling\Issue\IssueCollection;
use VeeWee\Xml\Exception\RuntimeException;
use function Psl\Vec\map;
use function VeeWee\Xml\Dom\Configurator\loader;
use function VeeWee\Xml\Dom\Loader\xml_file_loader;
use function VeeWee\Xml\Dom\Loader\xml_node_loader;
use function VeeWee\Xml\Dom\Loader\xml_string_loader;
use function VeeWee\Xml\Dom\Loader;
use function VeeWee\Xml\Dom\Locator\document_element;
use function VeeWee\Xml\Dom\Mapper\xml_string;
use function VeeWee\Xml\Internal\configure;
Expand Down Expand Up @@ -47,18 +44,28 @@ public static function configure(callable ... $configurators): self
}

/**
* @param callable(): XMLDocument $loader
* @param list<callable(XMLDocument): XMLDocument> $configurators
*
* @throws RuntimeException
*/
public static function fromXmlFile(string $file, callable ...$configurators): self
public static function fromLoader(callable $loader, callable ...$configurators): self
{
return new self(
configure(...$configurators)(XMLDocument::createFromFile($file))
// TODO : What with loader(xml_file_loader($file))
configure(...$configurators)($loader())
);
}

/**
* @param list<callable(XMLDocument): XMLDocument> $configurators
*
* @throws RuntimeException
*/
public static function fromXmlFile(string $file, callable ...$configurators): self
{
return self::fromLoader(Loader\xml_file_loader($file), ...$configurators);
}

/**
* @param non-empty-string $xml
* @param list<callable(XMLDocument): XMLDocument> $configurators
Expand All @@ -67,10 +74,7 @@ public static function fromXmlFile(string $file, callable ...$configurators): se
*/
public static function fromXmlString(string $xml, callable ...$configurators): self
{
return new self(
configure(...$configurators)(XMLDocument::createFromString($xml))
// TODO : What with loader(xml_string_loader($file))
);
return self::fromLoader(Loader\xml_string_loader($xml), ...$configurators);
}

/**
Expand All @@ -80,10 +84,7 @@ public static function fromXmlString(string $xml, callable ...$configurators): s
*/
public static function fromXmlNode(\DOM\Node $node, callable ...$configurators): self
{
return self::configure(
loader(xml_node_loader($node)),
...$configurators
);
return self::fromLoader(Loader\xml_node_loader($node), ...$configurators);
}

/**
Expand All @@ -93,9 +94,7 @@ public static function fromXmlNode(\DOM\Node $node, callable ...$configurators):
*/
public static function fromUnsafeDocument(XMLDocument $document, callable ...$configurators): self
{
return new self(
configure(...$configurators)($document)
);
return self::fromLoader(static fn () => $document, ...$configurators);
}

public function toUnsafeDocument(): XMLDocument
Expand Down
2 changes: 1 addition & 1 deletion src/Xml/Dom/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface Loader
{
public function __invoke(\DOM\XMLDocument $document): void;
public function __invoke(): XMLDocument;
}
25 changes: 0 additions & 25 deletions src/Xml/Dom/Loader/load.php

This file was deleted.

18 changes: 8 additions & 10 deletions src/Xml/Dom/Loader/xml_file_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
namespace VeeWee\Xml\Dom\Loader;

use Closure;
use \DOM\XMLDocument;
use DOM\XMLDocument;
use Webmozart\Assert\Assert;
use function VeeWee\Xml\ErrorHandling\disallow_issues;

/**
* @param int $options - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
* @return Closure(\DOM\XMLDocument): void
* @return Closure(): XMLDocument
*/
function xml_file_loader(string $file, int $options = 0): Closure
{
return static function (\DOM\XMLDocument $document) use ($file, $options): void {
load(
static function () use ($document, $file, $options): bool {
Assert::fileExists($file);
return $document->load($file, $options);
}
);
};
return static fn () => disallow_issues(static function () use ($file, $options): XMLDocument {
Assert::fileExists($file);

return XMLDocument::createFromFile($file, $options);
});
}
14 changes: 9 additions & 5 deletions src/Xml/Dom/Loader/xml_node_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

use Closure;
use \DOM\XMLDocument;

use \DOM\Node;
use function VeeWee\Xml\Dom\Manipulator\Node\append_external_node;
use function VeeWee\Xml\ErrorHandling\disallow_issues;

/**
* @return Closure(\DOM\XMLDocument): void
* @return Closure(): XMLDocument
*/
function xml_node_loader(\DOM\Node $importedNode): Closure
{
return static function (\DOM\XMLDocument $document) use ($importedNode): void {
load(static fn (): bool => (bool) append_external_node($document, $importedNode));
};
return static fn () => disallow_issues(static function () use ($importedNode): XMLDocument {
$document = XMLDocument::createEmpty();

append_external_node($document, $importedNode);

return $document;
});
}
9 changes: 5 additions & 4 deletions src/Xml/Dom/Loader/xml_string_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

use Closure;
use \DOM\XMLDocument;
use function VeeWee\Xml\ErrorHandling\disallow_issues;

/**
* @param non-empty-string $xml
* @param int $options - bitmask of LIBXML_* constants https://www.php.net/manual/en/libxml.constants.php
* @return Closure(\DOM\XMLDocument): void
* @return Closure(): XMLDocument
*/
function xml_string_loader(string $xml, int $options = 0): Closure
{
return static function (\DOM\XMLDocument $document) use ($xml, $options): void {
load(static fn (): bool => $document->loadXML($xml, $options));
};
return static fn () => disallow_issues(static function () use ($xml, $options): XMLDocument {
return XMLDocument::createFromString($xml, $options);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
*/
function locate_by_namespaced_tag_name(\DOM\Element $node, string $namespace, string $localTagName): NodeList
{
return NodeList::fromDOMNodeList($node->getElementsByTagNameNS($namespace, $localTagName));
return NodeList::fromDOMHTMLCollection($node->getElementsByTagNameNS($namespace, $localTagName));
}
2 changes: 1 addition & 1 deletion src/Xml/Dom/Locator/Element/locate_by_tag_name.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
*/
function locate_by_tag_name(\DOM\Element $node, string $tag): NodeList
{
return NodeList::fromDOMNodeList($node->getElementsByTagName($tag));
return NodeList::fromDOMHTMLCollection($node->getElementsByTagName($tag));
}
5 changes: 1 addition & 4 deletions src/Xml/Dom/Locator/Node/detect_document.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@
*/
function detect_document(\DOM\Node $node): \DOM\XMLDocument
{
$document = is_document($node) ? $node : $node->ownerDocument;
Assert::notNull($document, 'Expected to find an ownerDocument on provided \DOM\Node.');

return $document;
return is_document($node) ? $node : $node->ownerDocument;
}
10 changes: 5 additions & 5 deletions src/Xml/Dom/Mapper/xslt_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace VeeWee\Xml\Dom\Mapper;

use Closure;
use DOMDocument as DOMDocument;
use VeeWee\XmlDOMDocument;
use VeeWee\Xml\Dom\Document;
use DOM\XmlDocument;
use VeeWee\Xml\Xslt\Processor;
use XSLTProcessor;

/**
* @param list<callable(XSLTProcessor): XSLTProcessor> $configurators
* @return Closure(DOMDocument): string
* @return Closure(XmlDocument): string
*/
function xslt_template(Document $template, callable ... $configurators): Closure
{
return static fn (DOMDocument $document): string
=> Processor::fromTemplateDocument($template, ...$configurators)->transformDocumentToString(
return static fn (XmlDocument $document): string =>
Processor::fromTemplateDocument($template, ...$configurators)->transformDocumentToString(
Document::fromUnsafeDocument($document)
);
}
2 changes: 0 additions & 2 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
'Xml\Dom\Configurator\canonicalize' => __DIR__.'/Xml/Dom/Configurator/canonicalize.php',
'Xml\Dom\Configurator\comparable' => __DIR__.'/Xml/Dom/Configurator/comparable.php',
'Xml\Dom\Configurator\document_uri' => __DIR__.'/Xml/Dom/Configurator/document_uri.php',
'Xml\Dom\Configurator\loader' => __DIR__.'/Xml/Dom/Configurator/loader.php',
'Xml\Dom\Configurator\normalize' => __DIR__.'/Xml/Dom/Configurator/normalize.php',
'Xml\Dom\Configurator\optimize_namespaces' => __DIR__.'/Xml/Dom/Configurator/optimize_namespaces.php',
'Xml\Dom\Configurator\pretty_print' => __DIR__.'/Xml/Dom/Configurator/pretty_print.php',
'Xml\Dom\Configurator\traverse' => __DIR__.'/Xml/Dom/Configurator/traverse.php',
'Xml\Dom\Configurator\trim_spaces' => __DIR__.'/Xml/Dom/Configurator/trim_spaces.php',
'Xml\Dom\Configurator\utf8' => __DIR__.'/Xml/Dom/Configurator/utf8.php',
'Xml\Dom\Configurator\validator' => __DIR__.'/Xml/Dom/Configurator/validator.php',
'Xml\Dom\Loader\load' => __DIR__.'/Xml/Dom/Loader/load.php',
'Xml\Dom\Loader\xml_file_loader' => __DIR__.'/Xml/Dom/Loader/xml_file_loader.php',
'Xml\Dom\Loader\xml_node_loader' => __DIR__.'/Xml/Dom/Loader/xml_node_loader.php',
'Xml\Dom\Loader\xml_string_loader' => __DIR__.'/Xml/Dom/Loader/xml_string_loader.php',
Expand Down
41 changes: 0 additions & 41 deletions tests/Xml/Dom/Configurator/LoaderTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Xml/Dom/Configurator/PrettyPrintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function test_it_can_trim_contents(): void
EOXML;

static::assertSame($doc, $result);
static::assertFalse($doc->preserveWhiteSpace);
// TODO : static::assertFalse($doc->preserveWhiteSpace);
static::assertTrue($doc->formatOutput);
static::assertSame($expected, xml_string()($doc->documentElement));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Xml/Dom/Configurator/TrimSpacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_it_can_trim_contents(): void
$result = $configurator($doc);

static::assertSame($doc, $result);
static::assertFalse($doc->preserveWhiteSpace);
// TODO : static::assertFalse($doc->preserveWhiteSpace);
static::assertFalse($doc->formatOutput);
static::assertSame('<hello><world/></hello>', xml_string()($doc->documentElement));
}
Expand Down
Loading

0 comments on commit d61b7bb

Please sign in to comment.