-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reader MatchingNode results and a signal to stop reading
- Loading branch information
Showing
10 changed files
with
252 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader; | ||
|
||
use DOMDocument; | ||
use VeeWee\Xml\Dom\Document; | ||
use VeeWee\Xml\Encoding\Exception\EncodingException; | ||
use VeeWee\Xml\Exception\RuntimeException; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function VeeWee\Xml\Encoding\xml_decode; | ||
|
||
final class MatchingNode | ||
{ | ||
/** | ||
* @param non-empty-string $xml | ||
*/ | ||
public function __construct( | ||
private readonly string $xml, | ||
private readonly NodeSequence $nodeSequence | ||
) { | ||
} | ||
|
||
/** | ||
* @return non-empty-string | ||
*/ | ||
public function xml(): string | ||
{ | ||
return $this->xml; | ||
} | ||
|
||
public function nodeSequence(): NodeSequence | ||
{ | ||
return $this->nodeSequence; | ||
} | ||
|
||
/** | ||
* @param list<callable(DOMDocument): DOMDocument> $configurators | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function intoDocument(callable ... $configurators): Document | ||
{ | ||
return Document::fromXmlString($this->xml, ...$configurators); | ||
} | ||
|
||
/** | ||
* @param list<callable(DOMDocument): DOMDocument> $configurators | ||
* | ||
* @throws RuntimeException | ||
* @throws EncodingException | ||
*/ | ||
public function decode(callable ... $configurators): array | ||
{ | ||
return xml_decode($this->xml, ...$configurators); | ||
} | ||
|
||
/** | ||
* @param callable(NodeSequence): bool $matcher | ||
*/ | ||
public function matches(callable $matcher): bool | ||
{ | ||
return $matcher($this->nodeSequence); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Reader; | ||
|
||
final class Signal | ||
{ | ||
private bool $stopRequested = false; | ||
|
||
public function stop(): void | ||
{ | ||
$this->stopRequested = true; | ||
} | ||
|
||
public function stopRequested(): bool | ||
{ | ||
return $this->stopRequested; | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Reader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Xml\Reader\MatchingNode; | ||
use VeeWee\Xml\Reader\Node\ElementNode; | ||
use VeeWee\Xml\Reader\Node\NodeSequence; | ||
use function Psl\Fun\identity; | ||
use function VeeWee\Xml\Dom\Locator\document_element; | ||
use function VeeWee\Xml\Dom\Mapper\xml_string; | ||
use function VeeWee\Xml\Reader\Matcher\element_name; | ||
|
||
final class MatchingNodeTest extends TestCase | ||
{ | ||
|
||
public function test_it_is_a_matching_node(): void | ||
{ | ||
$match = new MatchingNode( | ||
$xml = '<hello/>', | ||
$sequence = new NodeSequence( | ||
new ElementNode(1, 'hello', 'hello', '', '', []) | ||
) | ||
); | ||
|
||
static::assertSame($xml, $match->xml()); | ||
static::assertSame($sequence, $match->nodeSequence()); | ||
} | ||
|
||
|
||
public function test_it_can_match(): void | ||
{ | ||
$match = new MatchingNode( | ||
'<hello/>', | ||
new NodeSequence( | ||
new ElementNode(1, 'hello', 'hello', '', '', []) | ||
) | ||
); | ||
|
||
static::assertTrue($match->matches(element_name('hello'))); | ||
static::assertFalse($match->matches(element_name('world'))); | ||
} | ||
|
||
|
||
public function test_it_can_transform_into_a_dom_document(): void | ||
{ | ||
$match = new MatchingNode( | ||
$xml = '<hello/>', | ||
new NodeSequence( | ||
new ElementNode(1, 'hello', 'hello', '', '', []) | ||
) | ||
); | ||
|
||
$document = $match->intoDocument(identity()); | ||
|
||
static::assertSame($xml, xml_string()($document->map(document_element()))); | ||
} | ||
|
||
public function test_it_can_decode_the_xml(): void | ||
{ | ||
$match = new MatchingNode( | ||
$xml = '<hello/>', | ||
new NodeSequence( | ||
new ElementNode(1, 'hello', 'hello', '', '', []) | ||
) | ||
); | ||
|
||
$decoded = $match->decode(identity()); | ||
|
||
static::assertSame(['hello' => ''], $decoded); | ||
} | ||
} |
Oops, something went wrong.