Jarves\ConditionOperator::satisfy PHP Method

satisfy() public method

public satisfy ( Condition | array $condition, array $objectItem, string $objectKey = null ) : boolean | null
$condition Jarves\Configuration\Condition | array
$objectItem array
$objectKey string
return boolean | null
    public function satisfy($condition, $objectItem, $objectKey = null)
    {
        if (is_array($condition)) {
            $condition = Condition::create($condition, $this->jarves);
        }
        $complied = null;
        $lastOperator = 'and';
        $rules = $condition->getRules();
        if (!$rules) {
            return null;
        }
        /*
         * [
         *   ['id', '=', 5],
         *   'or',
         *   ['id', '=', 6]
         * ]
         *
         * [
         *   Condition,
         *   'and',
         *   [Condition]
         * ]
         *
         *
         */
        foreach ($rules as $conditionRule) {
            if (is_string($conditionRule)) {
                $lastOperator = strtolower($conditionRule);
                continue;
            }
            $res = false;
            if (is_array($conditionRule) && is_string($conditionRule[0]) && is_string($conditionRule[1])) {
                $res = $this->checkRule($objectItem, $conditionRule, $objectKey);
            } else {
                if ($conditionRule instanceof Condition) {
                    $res = $this->satisfy($conditionRule, $objectItem, $objectKey);
                } else {
                    if (is_array($conditionRule)) {
                        //group
                        $res = $this->satisfy(Condition::create($conditionRule, $this->jarves), $objectItem, $objectKey);
                    }
                }
            }
            if (is_null($complied)) {
                $complied = $res;
            } else {
                if ($lastOperator == 'and') {
                    $complied = $complied && $res;
                }
                if ($lastOperator == 'and not') {
                    $complied = $complied && !$res;
                }
                if ($lastOperator == 'or') {
                    $complied = $complied || $res;
                }
            }
        }
        return $complied === null ? true : ($complied ? true : false);
    }

Usage Example

 /**
  * Checks whether a eventConfig is appropriate to be called (subject fits, condition fits)
  *
  * @param Event $eventConfig
  * @param GenericEvent $event
  * @return bool
  */
 public function isCallable(Event $eventConfig, GenericEvent $event)
 {
     if ($eventConfig->getSubject() && $event->getSubject() != $eventConfig->getSubject()) {
         return false;
     }
     if ($eventConfig->getCondition()) {
         $args = $event->getArguments() ?: [];
         if ($eventConfig->getCondition() && !$this->conditionOperator->satisfy($eventConfig->getCondition(), $args)) {
             return false;
         }
     }
     return true;
 }
All Usage Examples Of Jarves\ConditionOperator::satisfy