/** * Populates the given processor with the rule sets as outlined in the $data * * @param array $data * @param Processor $processor * * @return Processor */ public function build(array $data, Processor $processor = null) { if ($processor === null) { $processor = new Processor(); } // Pull out the rule sets and build each one if (isset($data['rule_sets'])) { foreach ($data['rule_sets'] as $set) { $processor->addRuleSet($this->buildRuleSet($set)); } } return $processor; }
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);