nahid / jsonq

A PHP query builder for JSON
Creative Commons Zero v1.0 Universal
872 stars 105 forks source link

How does the macro condition works? #50

Closed Timenator closed 5 years ago

Timenator commented 5 years ago

Hello,

Can someone explain to me how the "macro" condition works. Or has someone an example?

With kind regards, Tim Nobbenhuis

GitHubHubus commented 5 years ago

If I understand right, you can add you own condition function, like:

$jsonq->macro('lengthLessThen', function ($value, $comparable) {
        return count($value) < $comparable;
    }
);

and then use it:

$jsonq->from('users')->where('name', 'lengthLessThen', 10)->get();

@nahid , correct if I am wrong, please.

Timenator commented 5 years ago

It worked, thank you very much.

GitHubHubus commented 5 years ago

@Timenator you are welcome.

nahid commented 5 years ago

@GitHubHubus absolutely right. Thank you for your response

dimitrihilverda commented 5 years ago

@nahid When you use the macro, is there also a possibility to somehow add sorting? maybe add a field?, in our case we calculate a distance, and I like to sort the results by that number.

the items with the shortest distance first.