Skip to content

Commit

Permalink
Merge pull request #43 from veewee/allow-callable-function-inputs
Browse files Browse the repository at this point in the history
Re-allow callable function inputs
  • Loading branch information
veewee authored Nov 4, 2022
2 parents 55dae47 + 21d36b3 commit c3bd82e
Show file tree
Hide file tree
Showing 34 changed files with 73 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/Xml/Dom/Builder/children.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
/**
* @template T of DOMNode
*
* @param list<\Closure(T): DOMNode> $builders
* @param list<callable(T): DOMNode> $builders
*
* @return \Closure(T): T
* @return Closure(T): T
*/
function children(Closure ...$builders): Closure
function children(callable ...$builders): Closure
{
return static function (DOMNode $node) use ($builders): DOMNode {
foreach ($builders as $builder) {
Expand Down
8 changes: 4 additions & 4 deletions src/Xml/Dom/Builder/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
use DOMElement;
use DOMNode;
use Webmozart\Assert\Assert;
use function Psl\Fun\pipe;
use function VeeWee\Xml\Dom\Assert\assert_element;
use function VeeWee\Xml\Dom\Predicate\is_document;
use function VeeWee\Xml\Internal\configure;

/**
* @param list<\Closure(DOMElement): DOMElement> $configurators
* @param list<callable(DOMElement): DOMElement> $configurators
*
* @return \Closure(DOMNode): DOMElement
*/
function element(string $name, Closure ...$configurators): Closure
function element(string $name, callable ...$configurators): Closure
{
return static function (DOMNode $node) use ($name, $configurators): DOMElement {
$document = is_document($node) ? $node : $node->ownerDocument;
Assert::isInstanceOf($document, DOMDocument::class, 'Can not create an element without a DOM document.');

return assert_element(
pipe(...$configurators)($document->createElement($name))
configure(...$configurators)($document->createElement($name))
);
};
}
8 changes: 4 additions & 4 deletions src/Xml/Dom/Builder/namespaced_element.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
use DOMElement;
use DOMNode;
use Webmozart\Assert\Assert;
use function Psl\Fun\pipe;
use function VeeWee\Xml\Dom\Assert\assert_element;
use function VeeWee\Xml\Dom\Predicate\is_document;
use function VeeWee\Xml\Internal\configure;

/**
* @param list<\Closure(DOMElement): DOMElement> $configurators
* @param list<callable(DOMElement): DOMElement> $configurators
*
* @return \Closure(DOMNode): DOMElement
*/
function namespaced_element(string $namespace, string $qualifiedName, Closure ...$configurators): Closure
function namespaced_element(string $namespace, string $qualifiedName, callable ...$configurators): Closure
{
return static function (DOMNode $node) use ($namespace, $qualifiedName, $configurators): DOMElement {
$document = is_document($node) ? $node : $node->ownerDocument;
Assert::isInstanceOf($document, DOMDocument::class, 'Can not create an element without a DOM document.');

return assert_element(
pipe(...$configurators)($document->createElementNS($namespace, $qualifiedName))
configure(...$configurators)($document->createElementNS($namespace, $qualifiedName))
);
};
}
8 changes: 4 additions & 4 deletions src/Xml/Dom/Builder/nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use function VeeWee\Xml\Dom\Locator\Node\detect_document;

/**
* @param list<\Closure(DOMDocument): (list<DOMNode>|DOMNode)> $builders
* @param list<callable(DOMDocument): (list<DOMNode>|DOMNode)> $builders
*
* @return \Closure(DOMDocument): list<DOMNode>
*/
function nodes(Closure ... $builders): Closure
function nodes(callable ... $builders): Closure
{
return
/**
Expand All @@ -27,10 +27,10 @@ function nodes(Closure ... $builders): Closure
$builders,
/**
* @param list<DOMNode> $builds
* @param \Closure(DOMDocument): (DOMNode|list<DOMNode>) $builder
* @param callable(DOMDocument): (DOMNode|list<DOMNode>) $builder
* @return list<DOMNode>
*/
static function (array $builds, Closure $builder) use ($node): array {
static function (array $builds, callable $builder) use ($node): array {
$result = $builder(detect_document($node));
$newBuilds = is_array($result) ? $result : [$result];

Expand Down
2 changes: 1 addition & 1 deletion src/Xml/Dom/Configurator/comparable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Closure;
use DOMDocument;
use VeeWee\Xml\Dom\Traverser\Visitor\SortAttributes;
use function VeeWee\Xml\Util\configure;
use function VeeWee\Xml\Internal\configure;

/**
* @return \Closure(DOMDocument): DOMDocument
Expand Down
4 changes: 2 additions & 2 deletions src/Xml/Dom/Configurator/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use DOMDocument;

/**
* @param \Closure(DOMDocument): void $loader
* @param callable(DOMDocument): void $loader
*
* @return \Closure(DOMDocument): DOMDocument
*/
function loader(Closure $loader): Closure
function loader(callable $loader): Closure
{
return static function (DOMDocument $document) use ($loader): DOMDocument {
$loader($document);
Expand Down
4 changes: 2 additions & 2 deletions src/Xml/Dom/Configurator/validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use VeeWee\Xml\Exception\RuntimeException;

/**
* @param \Closure(DOMDocument): IssueCollection $validator
* @param callable(DOMDocument): IssueCollection $validator
*
* @return \Closure(DOMDocument): DOMDocument
*/
function validator(Closure $validator, ?Level $minimumLevel = null): Closure
function validator(callable $validator, ?Level $minimumLevel = null): Closure
{
$minimumLevel = $minimumLevel ?? Level::warning();

Expand Down
2 changes: 1 addition & 1 deletion src/Xml/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use function VeeWee\Xml\Dom\Loader\xml_string_loader;
use function VeeWee\Xml\Dom\Locator\document_element;
use function VeeWee\Xml\Dom\Mapper\xml_string;
use function VeeWee\Xml\Util\configure;
use function VeeWee\Xml\Internal\configure;

final class Document
{
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Dom/Loader/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace VeeWee\Xml\Dom\Loader;

use Closure;
use VeeWee\Xml\Exception\RuntimeException;
use function VeeWee\Xml\ErrorHandling\disallow_issues;
use function VeeWee\Xml\ErrorHandling\disallow_libxml_false_returns;

/**
* @param \Closure(): bool $loader
* @param callable(): bool $loader
* @throws RuntimeException
*/
function load(Closure $loader): void
function load(callable $loader): void
{
disallow_issues(
static function () use ($loader) {
Expand Down
4 changes: 2 additions & 2 deletions src/Xml/Dom/Mapper/xslt_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use XSLTProcessor;

/**
* @param list<\Closure(XSLTProcessor): XSLTProcessor> $configurators
* @param list<callable(XSLTProcessor): XSLTProcessor> $configurators
* @return \Closure(DOMDocument): string
*/
function xslt_template(Document $template, Closure ... $configurators): Closure
function xslt_template(Document $template, callable ... $configurators): Closure
{
return static fn (DOMDocument $document): string
=> Processor::fromTemplateDocument($template, ...$configurators)->transformDocumentToString(
Expand Down
6 changes: 3 additions & 3 deletions src/Xml/Dom/Validator/internal_xsd_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use VeeWee\Xml\Xsd\Schema\Schema;
use VeeWee\Xml\Xsd\Schema\SchemaCollection;
use function VeeWee\Xml\Dom\Locator\Xsd\locate_all_xsd_schemas;
use function VeeWee\Xml\Util\configure;
use function VeeWee\Xml\Internal\configure;

/**
* @param list<\Closure(SchemaCollection): SchemaCollection> $schemaManipulators
* @param list<callable(SchemaCollection): SchemaCollection> $schemaManipulators
* @return \Closure(DOMDocument): IssueCollection
*/
function internal_xsd_validator(Closure ... $schemaManipulators): Closure
function internal_xsd_validator(callable ... $schemaManipulators): Closure
{
return static function (DOMDocument $document) use ($schemaManipulators) : IssueCollection {
$schemas = configure(...$schemaManipulators)(locate_all_xsd_schemas($document));
Expand Down
8 changes: 4 additions & 4 deletions src/Xml/Dom/Validator/validator_chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
use function Psl\Iter\reduce;

/**
* @param list<\Closure(DOMDocument): IssueCollection> $validators
* @param list<callable(DOMDocument): IssueCollection> $validators
* @return \Closure(DOMDocument): IssueCollection
*/
function validator_chain(Closure ... $validators): Closure
function validator_chain(callable ... $validators): Closure
{
return static fn (DOMDocument $document): IssueCollection =>
reduce(
$validators,
/**
* @param \Closure(DOMDocument): IssueCollection $validator
* @param callable(DOMDocument): IssueCollection $validator
*/
static fn (IssueCollection $issues, $validator): IssueCollection
static fn (IssueCollection $issues, callable $validator): IssueCollection
=> new IssueCollection(
...$issues->getIterator(),
...$validator($document)->getIterator()
Expand Down
2 changes: 1 addition & 1 deletion src/Xml/Dom/Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function VeeWee\Xml\Dom\Xpath\Locator\evaluate;
use function VeeWee\Xml\Dom\Xpath\Locator\query;
use function VeeWee\Xml\Dom\Xpath\Locator\query_single;
use function VeeWee\Xml\Util\configure;
use function VeeWee\Xml\Internal\configure;

final class Xpath
{
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Encoding/document_encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Encoding;

use Closure;
use DOMDocument;
use VeeWee\Xml\Dom\Document;
use VeeWee\Xml\Encoding\Exception\EncodingException;
Expand All @@ -13,11 +12,11 @@
use function VeeWee\Xml\Encoding\Internal\wrap_exception;

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws EncodingException
*/
function document_encode(array $data, Closure ... $configurators): Document
function document_encode(array $data, callable ... $configurators): Document
{
return wrap_exception(
static function () use ($data, $configurators): Document {
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Encoding/element_decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Encoding;

use Closure;
use DOMDocument;
use DOMElement;
use VeeWee\Xml\Dom\Document;
Expand All @@ -14,11 +13,11 @@
use function VeeWee\Xml\Encoding\Internal\wrap_exception;

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws EncodingException
*/
function element_decode(DOMElement $element, Closure ... $configurators): array
function element_decode(DOMElement $element, callable ... $configurators): array
{
return wrap_exception(
static function () use ($element, $configurators): array {
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Encoding/element_encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

namespace VeeWee\Xml\Encoding;

use Closure;
use DOMDocument;
use VeeWee\Xml\Encoding\Exception\EncodingException;
use function VeeWee\Xml\Dom\Locator\document_element;
use function VeeWee\Xml\Dom\Mapper\xml_string;
use function VeeWee\Xml\Encoding\Internal\wrap_exception;

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws EncodingException
*/
function element_encode(array $data, Closure ... $configurators): string
function element_encode(array $data, callable ... $configurators): string
{
return wrap_exception(
static function () use ($data, $configurators): string {
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Encoding/typed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Encoding;

use Closure;
use DOMDocument;
use Psl\Type\Exception\CoercionException;
use Psl\Type\TypeInterface;
Expand All @@ -15,14 +14,14 @@
*
* @psalm-param non-empty-string $xml
* @psalm-param TypeInterface<T> $type
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @return T
*
* @throws CoercionException
* @throws EncodingException
*/
function typed(string $xml, TypeInterface $type, Closure ... $configurators)
function typed(string $xml, TypeInterface $type, callable ... $configurators)
{
return $type->coerce(xml_decode($xml, ...$configurators));
}
5 changes: 2 additions & 3 deletions src/Xml/Encoding/xml_decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Encoding;

use Closure;
use DOMDocument;
use VeeWee\Xml\Dom\Document;
use VeeWee\Xml\Encoding\Exception\EncodingException;
Expand All @@ -14,11 +13,11 @@

/**
* @param non-empty-string $xml
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws EncodingException
*/
function xml_decode(string $xml, Closure ... $configurators): array
function xml_decode(string $xml, callable ... $configurators): array
{
return wrap_exception(
static function () use ($xml, $configurators): array {
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Encoding/xml_encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace VeeWee\Xml\Encoding;

use Closure;
use DOMDocument;
use VeeWee\Xml\Encoding\Exception\EncodingException;
use function VeeWee\Xml\Encoding\Internal\wrap_exception;

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws EncodingException
*/
function xml_encode(array $data, Closure ... $configurators): string
function xml_encode(array $data, callable ... $configurators): string
{
return wrap_exception(
static function () use ($data, $configurators): string {
Expand Down
7 changes: 3 additions & 4 deletions src/Xml/ErrorHandling/detect_issues.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\ErrorHandling;

use Closure;
use LibXMLError;
use Psl\Result;
use Psl\Result\ResultInterface;
Expand All @@ -22,15 +21,15 @@
*
* @template Tr
*
* @psalm-param \Closure(): Tr $run
* @psalm-param callable(): Tr $run
* @psalm-return array{ResultInterface<Tr>, IssueCollection}
*/
function detect_issues(Closure $run): array
function detect_issues(callable $run): array
{
$previousErrorReporting = libxml_use_internal_errors(true);
libxml_clear_errors();

$result = Result\wrap($run);
$result = Result\wrap($run(...));

/** @var list<LibXMLError> $errors */
$errors = libxml_get_errors();
Expand Down
Loading

0 comments on commit c3bd82e

Please sign in to comment.