Beispiel #1
0
 protected function executeSetRule($types, $discountPercent)
 {
     $contextOptions['true'] = true;
     $andOperators = [];
     $orOperators = [];
     $trueVar = new Variable('true');
     foreach ($types as $type) {
         if (is_array($type)) {
             foreach ($type as $t) {
                 $contextOptions['contains' . $t . 'Products'] = $this->containsProductWithType($t);
                 $orOperators[] = new EqualTo(new Variable('contains' . $t . 'Products'), $trueVar);
             }
             $andOperators[] = new LogicalOr($orOperators);
         } else {
             $contextOptions['contains' . $type . 'Products'] = $this->containsProductWithType($type);
             $var = new Variable('contains' . $type . 'Products');
             $andOperators[] = new EqualTo($var, $trueVar);
         }
     }
     $context = new Context($contextOptions);
     $rule = new Rule(new LogicalAnd($andOperators), function () use($types, $discountPercent) {
         $discount = 0;
         foreach ($types as $type) {
             if (!is_array($type)) {
                 $extractTypes[] = $type;
             }
         }
         while (true) {
             $tuple = $this->extractProductsTuple($extractTypes);
             if (empty($tuple)) {
                 break;
             }
             foreach ($tuple as $product) {
                 $discount += $product['price'] * $discountPercent / 100;
             }
         }
         $this->discount += $discount;
     });
     $rule->execute($context);
 }
Beispiel #2
0
 /**
  * @expectedException \LogicException
  */
 public function testNonCallableActionsWillThrowAnException()
 {
     $context = new Context();
     $rule = new Rule(new TrueProposition(), 'this is not callable');
     $rule->execute($context);
 }