-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
751 additions
and
64 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\Reader\Matcher; | ||
|
||
use Closure; | ||
use Psl\Iter; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
|
||
/** | ||
* @param list<callable(NodeSequence): bool> $matchers | ||
* | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function any(callable ... $matchers): Closure | ||
{ | ||
return static fn (NodeSequence $sequence): bool => Iter\any( | ||
$matchers, | ||
/** | ||
* @param callable(NodeSequence): bool $matcher | ||
*/ | ||
static fn (callable $matcher): bool => $matcher($sequence) | ||
); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\AttributeNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Iter\any; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function attribute_local_name(string $localName): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($localName): bool { | ||
return any( | ||
$sequence->current()->attributes(), | ||
static fn (AttributeNode $attribute): bool => $attribute->localName() === $localName | ||
); | ||
}; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\AttributeNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Iter\any; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function attribute_local_value(string $localName, string $value): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($localName, $value): bool { | ||
return any( | ||
$sequence->current()->attributes(), | ||
static fn (AttributeNode $attribute): bool => $attribute->localName() === $localName && $attribute->value() === $value | ||
); | ||
}; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\AttributeNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Iter\any; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function attribute_name(string $name): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($name): bool { | ||
return any( | ||
$sequence->current()->attributes(), | ||
static fn (AttributeNode $attribute): bool => $attribute->name() === $name | ||
); | ||
}; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\AttributeNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Iter\any; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function attribute_value(string $name, string $value): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($name, $value): bool { | ||
return any( | ||
$sequence->current()->attributes(), | ||
static fn (AttributeNode $attribute): bool => $attribute->name() === $name && $attribute->value() === $value | ||
); | ||
}; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function document_element(): Closure | ||
{ | ||
return static function (NodeSequence $sequence): bool { | ||
return !$sequence->parent(); | ||
}; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function element_local_name(string $localName): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($localName): bool { | ||
return $sequence->current()->localName() === $localName; | ||
}; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function element_name(string $name): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($name): bool { | ||
return $sequence->current()->name() === $name; | ||
}; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function element_position(int $position): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($position): bool { | ||
return $sequence->current()->position() === $position; | ||
}; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\AttributeNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Iter\any; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function namespaced_attribute(string $namespace, string $localName): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($namespace, $localName): bool { | ||
return any( | ||
$sequence->current()->attributes(), | ||
static fn (AttributeNode $attribute): bool => $attribute->localName() === $localName && $attribute->namespace() === $namespace | ||
); | ||
}; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\AttributeNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Iter\any; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function namespaced_attribute_value(string $namespace, string $localName, string $value): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($namespace, $localName, $value): bool { | ||
return any( | ||
$sequence->current()->attributes(), | ||
static fn (AttributeNode $attribute): bool => | ||
$attribute->localName() === $localName | ||
&& $attribute->namespace() === $namespace | ||
&& $attribute->value() === $value | ||
); | ||
}; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
|
||
/** | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function namespaced_element(string $namespace, string $localName): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($namespace, $localName): bool { | ||
$current = $sequence->current(); | ||
|
||
return $current->localName() === $localName && $current->namespace() === $namespace; | ||
}; | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader\Matcher; | ||
|
||
use Closure; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function count; | ||
|
||
/** | ||
* A sequence can be used to match a full XML path to the node you are interested in. | ||
* | ||
* ```php | ||
* sequence(element_name('root'), all(element_name('item'), element_position(1)), element_name('name')); | ||
* ``` | ||
* | ||
* @param list<callable(NodeSequence): bool> $matcherSequence | ||
* | ||
* @return \Closure(NodeSequence): bool | ||
*/ | ||
function sequence(callable ... $matcherSequence): Closure | ||
{ | ||
return static function (NodeSequence $sequence) use ($matcherSequence) : bool { | ||
$nodeSequence = $sequence->sequence(); | ||
if (count($matcherSequence) !== count($nodeSequence)) { | ||
return false; | ||
} | ||
|
||
$currentSequence = new NodeSequence(); | ||
foreach ($nodeSequence as $i => $node) { | ||
$currentSequence = $currentSequence->append($node); | ||
$matcher = $matcherSequence[$i]; | ||
if (!$matcher($currentSequence)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
} |
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
Oops, something went wrong.