コード例 #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);