Skip to content

Commit

Permalink
Merge pull request #17 from saloonphp/fix/use-veewee-xml-2.11.2
Browse files Browse the repository at this point in the history
Feature | Add Generics For Query / LazyQuery
  • Loading branch information
Sammyjo20 authored Nov 12, 2023
2 parents 8161fe1 + 49a2b37 commit f5ea79c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
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();

0 comments on commit f5ea79c

Please sign in to comment.