/** * @param array $args (name, type, target, value) * @throws InvalidConditionException */ public function __construct(array $args) { $this->args = $args; if (Helpers::isMultiArray($args)) { throw new InvalidConditionException('Multi dimensional array is not supported.'); } else { $this->validate($this->args); } }
/** * add item to the cart, it can be an array or multi dimensional array * * @param string|array $id * @param string $name * @param float $price * @param int $quantity * @param array $attributes * @param CartCondition|array $conditions * @return $this * @throws InvalidItemException */ public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array()) { // if the first argument is an array, // we will need to call add again if (is_array($id)) { // the first argument is an array, now we will need to check if it is a multi dimensional // array, if so, we will iterate through each item and call add again if (Helpers::isMultiArray($id)) { foreach ($id as $item) { $this->add($item['id'], $item['name'], $item['price'], $item['quantity'], Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()), Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array())); } } else { $this->add($id['id'], $id['name'], $id['price'], $id['quantity'], Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()), Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array())); } return $this; } // validate data $item = $this->validate(array('id' => $id, 'name' => $name, 'price' => Helpers::normalizePrice($price), 'quantity' => $quantity, 'attributes' => new ItemAttributeCollection($attributes), 'conditions' => $conditions)); // get the cart $cart = $this->getContent(); // if the item is already in the cart we will just update it if ($cart->has($id)) { $this->events->fire($this->getInstanceName() . '.updating', array($item, $this)); $this->update($id, $item); $this->events->fire($this->getInstanceName() . '.updated', array($item, $this)); } else { $this->events->fire($this->getInstanceName() . '.adding', array($item, $this)); $this->addRow($id, $item); $this->events->fire($this->getInstanceName() . '.added', array($item, $this)); } return $this; }