/** * Serialize a collection of conditions * * @param ConditionCollection $collection A collection of conditions * * @return string A ready to be stored Condition collection */ public function serializeConditionCollection(ConditionCollection $collection) { if ($collection->count() == 0) { /** @var ConditionInterface $conditionNone */ $conditionNone = $this->container->get('thelia.condition.match_for_everyone'); $collection[] = $conditionNone; } $serializableConditions = []; /** @var $condition ConditionInterface */ foreach ($collection as $condition) { $serializableConditions[] = $condition->getSerializableCondition(); } return base64_encode(json_encode($serializableConditions)); }
/** * @covers Thelia\Condition\ConditionCollection::__toString */ public function test__toString() { $stubFacade = $this->generateFacadeStub(); $condition1 = new MatchForTotalAmount($stubFacade); $operators1 = array(MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL); $values1 = array(MatchForTotalAmount::CART_TOTAL => 400, MatchForTotalAmount::CART_CURRENCY => 'EUR'); $condition1->setValidatorsFromForm($operators1, $values1); $condition2 = new MatchForTotalAmount($stubFacade); $operators2 = array(MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL); $values2 = array(MatchForTotalAmount::CART_TOTAL => 600, MatchForTotalAmount::CART_CURRENCY => 'EUR'); $condition2->setValidatorsFromForm($operators2, $values2); $collection = new ConditionCollection(); $collection[] = $condition1; $collection[] = $condition2; $expected = '[{"conditionServiceId":"thelia.condition.match_for_total_amount","operators":{"price":">","currency":"=="},"values":{"price":400,"currency":"EUR"}},{"conditionServiceId":"thelia.condition.match_for_total_amount","operators":{"price":"<","currency":"=="},"values":{"price":600,"currency":"EUR"}}]'; $actual = $collection->__toString(); $this->assertEquals($expected, $actual); }