Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Add Generics For Query / LazyQuery #17

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"homepage": "https://github.com/saloonphp/xml-wrangler",
"require": {
"php": "^8.1",
"veewee/xml": "^2.11.1",
"veewee/xml": "^2.11.2",
"spatie/array-to-xml": "^3.2",
"ext-dom": "*"
},
Expand Down
7 changes: 6 additions & 1 deletion src/LazyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Generator;
use Illuminate\Support\LazyCollection;

/**
* @template TReturnType
*
* @extends \Saloon\XmlWrangler\Query<TReturnType>
*/
class LazyQuery extends Query
{
/**
Expand All @@ -30,7 +35,7 @@ public function lazy(): Generator
*
* Requires illuminate/support
*
* @return LazyCollection<int, mixed>
* @return LazyCollection<int, TReturnType>
* @throws \Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException
*/
public function collectLazy(): LazyCollection
Expand Down
11 changes: 8 additions & 3 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException;
use Saloon\XmlWrangler\Exceptions\MultipleNodesFoundException;

/**
* @template TReturnType
*/
class Query
{
/**
Expand Down Expand Up @@ -39,7 +42,7 @@ public function __construct(string $searchTerm, Generator $data)
/**
* Return the node as an array
*
* @return array<string, mixed>
* @return array<string, TReturnType>
* @throws \Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException
*/
public function get(): array
Expand All @@ -54,7 +57,7 @@ public function get(): array
*
* Requires illuminate/support
*
* @return Collection<string, mixed>
* @return Collection<string, TReturnType>
* @throws \Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException
*/
public function collect(): Collection
Expand All @@ -65,6 +68,7 @@ public function collect(): Collection
/**
* Retrieve the first value in the node
*
* @return TReturnType
* @throws \Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException
*/
public function first(): mixed
Expand All @@ -81,6 +85,7 @@ public function first(): mixed
/**
* Retrieve the first value in the node or fail
*
* @return TReturnType
* @throws \Saloon\XmlWrangler\Exceptions\MissingNodeException
* @throws \Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException
*/
Expand All @@ -94,7 +99,7 @@ public function firstOrFail(): mixed
*
* Throws an exception if none exist or more than one exists.
*
* @return string|null
* @return ?TReturnType
* @throws \Saloon\XmlWrangler\Exceptions\MissingNodeException
* @throws \Saloon\XmlWrangler\Exceptions\MultipleNodesFoundException
* @throws \Saloon\XmlWrangler\Exceptions\QueryAlreadyReadException
Expand Down
4 changes: 4 additions & 0 deletions src/XmlReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function elements(): array
* Find an element from the XML
*
* @param array<string, string> $withAttributes
* @return \Saloon\XmlWrangler\LazyQuery<Element>
* @throws \Saloon\XmlWrangler\Exceptions\XmlReaderException
* @throws \Throwable
* @throws \VeeWee\Xml\Encoding\Exception\EncodingException
Expand Down Expand Up @@ -277,6 +278,7 @@ public function element(string $name, array $withAttributes = []): LazyQuery
/**
* Search for an element with xpath
*
* @return \Saloon\XmlWrangler\Query<Element>
* @throws \Throwable
* @throws \VeeWee\Xml\Encoding\Exception\EncodingException
*/
Expand Down Expand Up @@ -340,6 +342,7 @@ public function values(): array
* Find and retrieve value of element
*
* @param array<string, string> $attributes
* @return \Saloon\XmlWrangler\LazyQuery<mixed>
* @throws \Saloon\XmlWrangler\Exceptions\XmlReaderException
* @throws \Throwable
*/
Expand All @@ -353,6 +356,7 @@ public function value(string $name, array $attributes = []): LazyQuery
/**
* Find and retrieve value of element
*
* @return \Saloon\XmlWrangler\Query<mixed>
* @throws \Saloon\XmlWrangler\Exceptions\XmlReaderException
* @throws \Throwable
* @throws \VeeWee\Xml\Encoding\Exception\EncodingException
Expand Down
17 changes: 4 additions & 13 deletions tests/Feature/XmlReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
]);
});

test('root namespaces are removed from the element searches', function () {
test('root namespaces are removed from xpath queries', function () {
$reader = XmlReader::fromString(
<<<XML
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
Expand All @@ -460,6 +460,8 @@
XML
);

// Element should keep the xmlns

$element = $reader->element('container.services.service', ['id' => 'service_container'])->sole();

expect($element)->toEqual(
Expand Down Expand Up @@ -499,6 +501,7 @@
'class' => 'Symfony\Component\DependencyInjection\ContainerInterface',
'public' => 'true',
'synthetic' => 'true',
'xmlns' => 'http://symfony.com/schema/dic/services',
])
);

Expand Down Expand Up @@ -550,15 +553,3 @@

expect($mappedXpathTag)->toBe('1');
});

test('can test large xml files', function () {
$file = '/Users/samcarre/Documents/XML/psd7003.xml';

$reader = XmlReader::fromFile($file);

$values = $reader->value('refinfo')->lazy();

foreach ($values as $value) {
dd($value);
}
})->skip();