예제 #1
0
파일: Builder.php 프로젝트: indigophp/ruler
 /**
  * Builds a full rule set with a rule and results
  *
  * @param array $data
  *
  * @return RuleSet
  */
 public function buildRuleSet(array $data)
 {
     $ruleSet = new RuleSet();
     if (isset($data['identifier'])) {
         $ruleSet->setIdentifier($data['identifier']);
     }
     // Build the rule
     if (isset($data['rule'])) {
         $ruleSet->setRule($this->buildRule($data['rule']));
     }
     // Loop over each result and build that too
     if (isset($data['result'])) {
         foreach ($data['result'] as $result) {
             $ruleSet->addResult($this->buildResult($result));
         }
     }
     return $ruleSet;
 }
예제 #2
0
파일: example.php 프로젝트: indigophp/ruler
        foreach ($target as $id => &$product) {
            if ($product['id'] == $this->getParameter()) {
                $product['price'] = (int) $this->getModifier()->modify($target[$id]['price']);
                break;
            }
        }
    }
}
$context = [['id' => 1, 'name' => 'foobar', 'qty' => 12, 'price' => 10], ['id' => 2, 'name' => 'bazbat', 'qty' => 9, 'price' => 25]];
// Set up a rule that will give a 50% discount on product 1
// Set up the "has product" rule and point it at product 1
$assertion = new Equal();
$assertion->setTargetValue(1);
$rule = new HasProduct();
$rule->setAssertion($assertion);
// Set up our 50% discount
$result = new ProductDiscount();
$result->setParameter(1);
$modifier = new Percent();
$modifier->setTargetValue(50);
$result->setModifier($modifier);
// Roll them into a rule set
$ruleSet = new RuleSet();
$ruleSet->setRule($rule);
$ruleSet->addResult($result);
// Add to the rule set and run
$processor = new Processor();
$processor->addRuleSet($ruleSet);
$processor->process($context, $context);
// New price for product 1 is now 5
var_dump($context);