Conditional application rules

Instead of Copy/Pasting you can make standalone Rules objects that decorate other rules apply their rules based on the state in the entities/relations that are being persisted.

class AdminAllowedRule
{
    public function __construct($callable)
    {
            $this->callable = $callable;
    }

    public function __invoke(EntityInterface $entity, $options)
    {
            if ($entity->user->is_admin) {
                    return true;
            }
            $cb = $this->callable;
            return $cb($entity, $options);
    }
}

This decorator approach would allow you do decorate your rules with the ‘admin allowed’ logic.

    $rules->add(new AdminAllowedRule(function ($entity, $options) {
            return $entity->created < time();
    });

This is pretty rough code, but with some polish it could work.

1 Like