Exemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function isEnabled()
 {
     if (null === $this->rule) {
         throw new FeatureException('No Rule has been set');
     }
     if (null === $this->context) {
         $this->context = new Context();
     }
     return $this->rule->evaluate($this->context);
 }
Exemplo n.º 2
0
 public function testConstructorEvaluationAndExecution()
 {
     $test = $this;
     $context = new Context();
     $executed = false;
     $actionExecuted = false;
     $ruleOne = new Rule(new CallbackProposition(function ($c) use($test, $context, &$executed, &$actionExecuted) {
         $test->assertSame($c, $context);
         $executed = true;
         return false;
     }), function () use($test, &$actionExecuted) {
         $actionExecuted = true;
     });
     $this->assertFalse($ruleOne->evaluate($context));
     $this->assertTrue($executed);
     $ruleOne->execute($context);
     $this->assertFalse($actionExecuted);
     $executed = false;
     $actionExecuted = false;
     $ruleTwo = new Rule(new CallbackProposition(function ($c) use($test, $context, &$executed, &$actionExecuted) {
         $test->assertSame($c, $context);
         $executed = true;
         return true;
     }), function () use($test, &$actionExecuted) {
         $actionExecuted = true;
     });
     $this->assertTrue($ruleTwo->evaluate($context));
     $this->assertTrue($executed);
     $ruleTwo->execute($context);
     $this->assertTrue($actionExecuted);
 }
Exemplo n.º 3
0
 /**
  * evaluateRule 
  * 
  * @param RankRule $rule 
  * @param Context $ctx 
  * @access private
  * @return void
  */
 private function evaluateRule(RankRule $rule, Context $ctx)
 {
     $propositionClass = sprintf("Ruler\\Operator\\%s", ucfirst($rule->getComparator()));
     if (!class_exists($propositionClass)) {
         throw new \Exception("Class {$propositionClass} does not exist");
     }
     // If fact is a date, we must convert value to unix timestamp
     // to do comparison
     if (preg_match('/date/i', $rule->getFact()->getName())) {
         $date = new \DateTime($rule->getValue());
         $value = $date->getTimestamp();
     } else {
         $value = $rule->getValue();
     }
     $proposition = new $propositionClass(new Variable($rule->getFact()->getName()), new Variable('x' . $rule->getFact()->getName(), $value));
     // add x value to context
     $myRule = new Rule($proposition, function () {
     });
     if ($myRule->evaluate($ctx)) {
         if ($this->debug) {
             print "<h2>rule passed!</h2>";
         }
         return true;
     } else {
         if ($this->debug) {
             print "<h2>rule failed</h2>";
         }
         return false;
     }
 }