K-Phoen / rulerz

Powerful implementation of the Specification pattern in PHP
MIT License
872 stars 97 forks source link

ObjectContext returns array "as is" #72

Closed vdechenaux closed 7 years ago

vdechenaux commented 7 years ago

Hi,

I'm trying to upgrade from 0.17.x to 0.19.3 and my custom operator intersects is not working:

class ArrayIntersects
{
    public function __invoke($a, $b)
    {
        return sprintf('!empty(array_intersect(%s, %s))', $a, $b);
    }
}

I have this error: Warning: array_intersect(): Argument #1 is not an array /tmp/rulerz_executor_f2795b9b:16. My array is wrapped by a ObjectContext. Maybe it shouldn't?

This patch seems to fix the problem.

So, what do you think? :thinking:

K-Phoen commented 7 years ago

I don't know if your fix really is correct.

ObjectContext wraps objects so that RulerZ can seamlessly traverse the object graph when evaluating rules like this one: user.address.street = :street If we somehow stop wrapping the original data because we encounter an array, we break this graph traversal.

I think that we should "fix" RulerZ so that it unwraps the parameters (if needed) before calling operators. First, I'll write some failing tests and then I'll experiment with a solution :)

K-Phoen commented 7 years ago

Should be fixed in master :)