/** * @throws InvalidContextException * @throws InvalidRuleException * @throws InvalidCallbackException */ private function validate() { if (!$this->context->valid()) { throw new InvalidContextException('Context must be an array with string keys and values.'); } if (!$this->rules->valid()) { throw new InvalidRuleException('Rule must be a string or an array of strings.'); } if (!$this->evaluator->valid($this->rules, $this->context)) { throw new InvalidRuleException('Rules aren\'t semantically valid (' . implode(',', $this->interpret()) . ').'); } if ($this->successCallback !== null && !is_callable($this->successCallback)) { throw new InvalidCallbackException('Success callback must be callable.'); } if ($this->failCallback !== null && !is_callable($this->failCallback)) { throw new InvalidCallbackException('Fail callback must be callable.'); } }
/** * @param string $rule * @param Context $context * @return string */ private function prepare($rule, $context) { $replacements = array_merge((new ComparisonOperator())->all(), (new LogicalOperator())->all(), $context->get()); return str_replace(array_keys($replacements), array_values($replacements), $rule); }