Skip to content

Conversation

@jigius
Copy link

@jigius jigius commented Aug 9, 2017

Hi, SuRaMoN!

I've added simple filter to PathIterator's constructor via forth parameter.
It makes possible to throw out some data from result.

Examples how it use into examples/filter-example{1,2}.php files.

I've used it in project and it realy works.

@SuRaMoN
Copy link
Owner

SuRaMoN commented Aug 10, 2017

hey i'm sorry, but as far as i can see the new functionality can already be achieved with the use of a CallbackFilterIterator

If i look at example1 your code can be replaced with:

$animals = new \CallbackFilterIterator($xml->path('root/animal'), function ($animal) {
    return in_array($animal->attributes()->type, ['dog', 'kakariki']);
});
foreach(...) { ... }

The second example can also be replaced with a callbackfilteriterator:

$skipNext = false;
$animals = new \CallbackFilterIterator($xml->path('root/group/*'), function ($animal) use (& $skipNext) {
    if ($animal->getName() == 'type') {
        $skipNext = (string) $animal == 'wild';
        return false;
    }
    return ! $skipNext;
});

I prefer the CallbackFilterIterator approach above yours because it reuses existing iterators in the php standard library.

so unless i overlooked something, i cannot accept this

@jigius
Copy link
Author

jigius commented Aug 11, 2017

Hi,
In case huge XML with deep nested structure and requirement to get a little edge subset data from it. And when "a little edge subset data" depends on some elements/attributes' values of its parents/siblings - my variant, imho, is consuming less memory/cpu and allows to get data in one pass without its later postprocessing, in case of using CallbackFilterIterator.

I've added examples/filter-example3.php. The schema of xml data is a little crazy :)

Try to implement such filtering with using CallbackFilterIterator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants