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 Sep 16, 2024
1 parent 0377972 commit dec15b1
Show file tree
Hide file tree
Showing 54 changed files with 1,821 additions and 265 deletions.
8 changes: 4 additions & 4 deletions src/Xml/Dom/Assert/assert_cdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace VeeWee\Xml\Dom\Assert;

use \DOM\CdataSection;
use \DOM\CDATASection;
use Psl\Type\Exception\AssertException;
use function Psl\Type\instance_of;

/**
* @psalm-assert \DOM\CdataSection $node
* @psalm-assert \DOM\CDATASection $node
* @throws AssertException
*/
function assert_cdata(mixed $node): \DOM\CdataSection
function assert_cdata(mixed $node): \DOM\CDATASection
{
return instance_of(\DOM\CdataSection::class)->assert($node);
return instance_of(\DOM\CDATASection::class)->assert($node);
}
8 changes: 4 additions & 4 deletions src/Xml/Dom/Builder/cdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
namespace VeeWee\Xml\Dom\Builder;

use Closure;
use \DOM\CdataSection;
use \DOM\CDATASection;
use \DOM\Node;
use function VeeWee\Xml\Dom\Assert\assert_cdata;
use function VeeWee\Xml\Dom\Locator\Node\detect_document;
use function VeeWee\Xml\Internal\configure;

/**
* @param list<callable(\DOM\CdataSection): \DOM\CdataSection> $configurators
* @param list<callable(\DOM\CDATASection): \DOM\CDATASection> $configurators
*
* @return Closure(\DOM\Node): \DOM\CdataSection
* @return Closure(\DOM\Node): \DOM\CDATASection
*/
function cdata(string $data, ...$configurators): Closure
{
return static function (\DOM\Node $node) use ($data, $configurators): \DOM\CdataSection {
return static function (\DOM\Node $node) use ($data, $configurators): \DOM\CDATASection {
$document = detect_document($node);

return assert_cdata(
Expand Down
5 changes: 1 addition & 4 deletions src/Xml/Dom/Builder/escaped_value.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
namespace VeeWee\Xml\Dom\Builder;

use Closure;
use \DOM\Element;

/**
* @return Closure(\DOM\Element): \DOM\Element
*/
function escaped_value(string $value): Closure
{
return static function (\DOM\Element $node) use ($value): \DOM\Element {
$node->nodeValue = htmlspecialchars($value, ENT_XML1|ENT_QUOTES);

return $node;
return value(htmlspecialchars($value, ENT_XML1|ENT_QUOTES))($node);
};
}
5 changes: 4 additions & 1 deletion src/Xml/Dom/Builder/value.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

use Closure;
use \DOM\Element;
use function VeeWee\Xml\Dom\Locator\Node\detect_document;

/**
* @return Closure(\DOM\Element): \DOM\Element
*/
function value(string $value): Closure
{
return static function (\DOM\Element $node) use ($value): \DOM\Element {
$node->nodeValue = $value;
$document = detect_document($node);
$text = $document->createTextNode($value);
$node->appendChild($text);

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
10 changes: 7 additions & 3 deletions src/Xml/Dom/Configurator/canonicalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
use \DOM\XMLDocument as DOMDocument;
use VeeWee\Xml\Dom\Document;
use function Psl\Type\non_empty_string;
use function VeeWee\Xml\Dom\Loader\xml_string_loader;

/**
* @return Closure(DOMDocument): DOMDocument
*/
function canonicalize(): Closure
{
return static fn (DOMDocument $document): DOMDocument
=> Document::fromXmlString(
non_empty_string()->assert($document->C14N()), // TODO : LIBXML_NSCLEAN + LIBXML_NOCDATA
// pretty_print() : TODO
=> Document::fromLoader(
xml_string_loader(
non_empty_string()->assert($document->C14N()),
LIBXML_NSCLEAN + LIBXML_NOCDATA
),
pretty_print(),
normalize()
)->toUnsafeDocument();
}
21 changes: 0 additions & 21 deletions src/Xml/Dom/Configurator/loader.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/Xml/Dom/Configurator/normalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
function normalize(): Closure
{
return static function (\DOM\XMLDocument $document): \DOM\XMLDocument {
// TODO : does not exist anmyore
// $document->normalizeDocument();
$document->normalize();

return $document;
};
Expand Down
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;
}
Loading

0 comments on commit dec15b1

Please sign in to comment.