Skip to content

Commit

Permalink
Merge pull request #42 from veewee/support-callables-again
Browse files Browse the repository at this point in the history
Support callables on main classes again
  • Loading branch information
veewee authored Oct 28, 2022
2 parents 0b903e0 + cb1cb4a commit 55dae47
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 102 deletions.
2 changes: 1 addition & 1 deletion docs/dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $doc = Document::configure(
Configurator\validator(
Validator\internal_xsd_validator()
),
static fn (DOMDocument $document) => (new MyCustomMergeImportsConfigurator())($document),
new MyCustomMergeImportsConfigurator(),
);

$xpath = $doc->xpath(
Expand Down
43 changes: 21 additions & 22 deletions src/Xml/Dom/Collection/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Dom\Collection;

use Closure;
use Countable;
use DOMElement;
use DOMNode;
Expand Down Expand Up @@ -104,19 +103,19 @@ public function count(): int

/**
* @template R
* @param \Closure(T): R $mapper
* @param callable(T): R $mapper
*
* @return list<R>
*/
public function map(Closure $mapper): array
public function map(callable $mapper): array
{
return map($this->nodes, $mapper);
return map($this->nodes, $mapper(...));
}

/**
* @param \Closure(T): void $mapper
* @param callable(T): void $mapper
*/
public function forEach(Closure $mapper): void
public function forEach(callable $mapper): void
{
foreach ($this->nodes as $node) {
$mapper($node);
Expand All @@ -125,27 +124,27 @@ public function forEach(Closure $mapper): void

/**
* @template X of DOMNode
* @param \Closure(T): iterable<X> $mapper
* @param callable(T): iterable<X> $mapper
*
* @return NodeList<X>
*/
public function detect(Closure $mapper): self
public function detect(callable $mapper): self
{
return new self(
...flat_map(
$this->nodes,
$mapper
$mapper(...)
)
);
}

/**
* @param \Closure(T): bool $predicate
* @param callable(T): bool $predicate
* @return NodeList<T>
*/
public function filter(Closure $predicate): self
public function filter(callable $predicate): self
{
return new self(...filter($this->nodes, $predicate));
return new self(...filter($this->nodes, $predicate(...)));
}

/**
Expand All @@ -162,21 +161,21 @@ public function eq(int $index): self

/**
* @template R
* @param \Closure(R, T): R $reducer
* @param callable(R, T): R $reducer
* @param R $initial
* @return R
*/
public function reduce(Closure $reducer, mixed $initial): mixed
public function reduce(callable $reducer, mixed $initial): mixed
{
return reduce($this->nodes, $reducer, $initial);
return reduce($this->nodes, $reducer(...), $initial);
}

/**
* @param list<\Closure(DOMXPath): DOMXPath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
* @throws RuntimeException
* @return NodeList<DOMNode>
*/
public function query(string $xpath, Closure ... $configurators): self
public function query(string $xpath, callable ... $configurators): self
{
return $this->detect(
/**
Expand All @@ -190,11 +189,11 @@ public function query(string $xpath, Closure ... $configurators): self

/**
* @template X
* @param list<\Closure(DOMXPath): DOMXPath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
* @param TypeInterface<X> $type
* @return list<X>
*/
public function evaluate(string $expression, TypeInterface $type, Closure ... $configurators): array
public function evaluate(string $expression, TypeInterface $type, callable ... $configurators): array
{
return $this->map(
static fn (DOMNode $node): mixed
Expand Down Expand Up @@ -269,12 +268,12 @@ public function expectAllOfType(string $type): self
}

/**
* @param \Closure(T, T): int $sorter
* @param callable(T, T): int $sorter
*
* @return NodeList<T>
*/
public function sort(Closure $sorter): self
public function sort(callable $sorter): self
{
return new self(...sort($this->nodes, $sorter));
return new self(...sort($this->nodes, $sorter(...)));
}
}
50 changes: 27 additions & 23 deletions src/Xml/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use VeeWee\Xml\Dom\Traverser\Visitor;
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;
Expand All @@ -33,23 +34,23 @@ public static function empty(): self
}

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws RuntimeException
*/
public static function configure(Closure ... $configurators): self
public static function configure(callable ... $configurators): self
{
$document = configure(...$configurators)(new DOMDocument());

return new self($document);
}

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws RuntimeException
*/
public static function fromXmlFile(string $file, Closure ...$configurators): self
public static function fromXmlFile(string $file, callable ...$configurators): self
{
return self::configure(
loader(xml_file_loader($file)),
Expand All @@ -59,11 +60,11 @@ public static function fromXmlFile(string $file, Closure ...$configurators): sel

/**
* @param non-empty-string $xml
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws RuntimeException
*/
public static function fromXmlString(string $xml, Closure ...$configurators): self
public static function fromXmlString(string $xml, callable ...$configurators): self
{
return self::configure(
loader(xml_string_loader($xml)),
Expand All @@ -72,11 +73,11 @@ public static function fromXmlString(string $xml, Closure ...$configurators): se
}

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws RuntimeException
*/
public static function fromXmlNode(DOMNode $node, Closure ...$configurators): self
public static function fromXmlNode(DOMNode $node, callable ...$configurators): self
{
return self::configure(
loader(xml_node_loader($node)),
Expand All @@ -85,11 +86,11 @@ public static function fromXmlNode(DOMNode $node, Closure ...$configurators): se
}

/**
* @param list<\Closure(DOMDocument): DOMDocument> $configurators
* @param list<callable(DOMDocument): DOMDocument> $configurators
*
* @throws RuntimeException
*/
public static function fromUnsafeDocument(DOMDocument $document, Closure ...$configurators): self
public static function fromUnsafeDocument(DOMDocument $document, callable ...$configurators): self
{
return new self(
configure(...$configurators)($document)
Expand All @@ -103,60 +104,63 @@ public function toUnsafeDocument(): DOMDocument

/**
* @template T
* @param \Closure(DOMDocument): T $locator
* @param callable(DOMDocument): T $locator
*
* @return T
*/
public function locate(Closure $locator)
public function locate(callable $locator)
{
return $locator($this->document);
}

/**
* @param \Closure(DOMDocument): mixed $manipulator
* @param callable(DOMDocument): mixed $manipulator
*
* @return $this
*/
public function manipulate(Closure $manipulator): self
public function manipulate(callable $manipulator): self
{
$manipulator($this->document);

return $this;
}

/**
* @param list<\Closure(DOMDocument): (list<DOMNode>|DOMNode)> $builders
* @param list<callable(DOMDocument): (list<DOMNode>|DOMNode)> $builders
*
* @return list<DOMNode>
*/
public function build(Closure ... $builders): array
public function build(callable ... $builders): array
{
return Builder\nodes(...$builders)($this->document);
return Builder\nodes(...map(
$builders,
fn (callable $builder): Closure => $builder(...)
))($this->document);
}

/**
* @param \Closure(DOMDocument): IssueCollection $validator
* @param callable(DOMDocument): IssueCollection $validator
*/
public function validate(Closure $validator): IssueCollection
public function validate(callable $validator): IssueCollection
{
return $validator($this->document);
}

/**
* @param list<\Closure(DOMXPath): DOMXPath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
*/
public function xpath(Closure ...$configurators): Xpath
public function xpath(callable ...$configurators): Xpath
{
return Xpath::fromDocument($this, ...$configurators);
}

/**
* @template T
* @param \Closure(DOMDocument): T $mapper
* @param callable(DOMDocument): T $mapper
*
* @return T
*/
public function map(Closure $mapper)
public function map(callable $mapper)
{
return $mapper($this->document);
}
Expand Down
13 changes: 6 additions & 7 deletions src/Xml/Dom/Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Dom;

use Closure;
use DOMNode;
use DOMXPath;
use InvalidArgumentException;
Expand All @@ -25,21 +24,21 @@ private function __construct(
}

/**
* @param list<\Closure(DOMXPath): DOMXPath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
*/
public static function fromDocument(Document $document, Closure ... $configurators): self
public static function fromDocument(Document $document, callable ... $configurators): self
{
return new self(
configure(...$configurators)(new DOMXPath($document->toUnsafeDocument()))
);
}

/**
* @param list<\Closure(DOMXPath): DOMXPath> $configurators
* @param list<callable(DOMXPath): DOMXPath> $configurators
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public static function fromUnsafeNode(DOMNode $node, Closure ... $configurators): self
public static function fromUnsafeNode(DOMNode $node, callable ... $configurators): self
{
return self::fromDocument(
Document::fromUnsafeDocument(
Expand All @@ -51,12 +50,12 @@ public static function fromUnsafeNode(DOMNode $node, Closure ... $configurators)

/**
* @template T
* @param \Closure(DOMXpath): T $locator
* @param callable(DOMXpath): T $locator
*
* @return T
* @throws RuntimeException
*/
public function locate(Closure $locator)
public function locate(callable $locator)
{
return $locator($this->xpath);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Xml/ErrorHandling/Issue/IssueCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\ErrorHandling\Issue;

use Closure;
use Countable;
use IteratorAggregate;
use Psl\Dict;
Expand Down Expand Up @@ -46,11 +45,11 @@ public function count(): int
}

/**
* @param (\Closure(Issue): bool) $filter
* @param (callable(Issue): bool) $filter
*/
public function filter(Closure $filter): self
public function filter(callable $filter): self
{
return new self(...Dict\filter($this->issues, $filter));
return new self(...Dict\filter($this->issues, $filter(...)));
}

public function getHighestLevel(): ?Level
Expand Down
5 changes: 2 additions & 3 deletions src/Xml/Reader/Node/ElementNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace VeeWee\Xml\Reader\Node;

use Closure;
use XMLReader;

/**
Expand All @@ -26,9 +25,9 @@ public function __construct(
}

/**
* @param \Closure(): list<AttributeNode> $attributesProvider
* @param callable(): list<AttributeNode> $attributesProvider
*/
public static function fromReader(XMLReader $reader, int $position, Closure $attributesProvider): self
public static function fromReader(XMLReader $reader, int $position, callable $attributesProvider): self
{
return new self(
$position,
Expand Down
Loading

0 comments on commit 55dae47

Please sign in to comment.