id = $id; $this->name = $name; $this->price = $price; $this->weight = $weight; $this->options = new CartItemOptions($options); $this->rowId = $this->generateRowId($id, $options); } /** * Set the quantity for this cart item. * * @param int|float $qty */ public function setQuantity($qty) { if (empty($qty) || !is_numeric($qty)) { throw new \InvalidArgumentException('Please supply a valid quantity.'); } $this->qty = $qty; } /** * Update the cart item from a Buyable. * * @param \Gloudemans\Shoppingcart\Contracts\Buyable $item * * @return void */ public function updateFromBuyable(Buyable $item) { $this->id = $item->getBuyableIdentifier($this->options); $this->name = $item->getBuyableDescription($this->options); $this->price = $item->getBuyablePrice($this->options); } /** * Update the cart item from an array. * * @param array $attributes * * @return void */ public function updateFromArray(array $attributes) { $this->id = Arr::get($attributes, 'id', $this->id); $this->qty = Arr::get($attributes, 'qty', $this->qty); $this->name = Arr::get($attributes, 'name', $this->name); $this->price = Arr::get($attributes, 'price', $this->price); $this->weight = Arr::get($attributes, 'weight', $this->weight); $this->options = new CartItemOptions(Arr::get($attributes, 'options', $this->options)); $this->rowId = $this->generateRowId($this->id, $this->options->all()); } /** * Associate the cart item with the given model. * * @param mixed $model * * @return \Gloudemans\Shoppingcart\CartItem */ public function associate($model) : self { $this->associatedModel = is_string($model) ? $model : get_class($model); return $this; } /** * Set the tax rate. * * @param int|float $taxRate * * @return \Gloudemans\Shoppingcart\CartItem */ public function setTaxRate($taxRate) : self { $this->taxRate = $taxRate; return $this; } /** * Set the discount rate. */ public function setDiscount(float $discount) : self { $this->discountRate = $discountRate; return $this; } /** * Set cart instance. * * @param null|string $instance * * @return \Gloudemans\Shoppingcart\CartItem */ public function setInstance(?string $instance) : self { $this->instance = $instance; return $this; } /** * Get an attribute from the cart item or get the associated model. * * @param string $attribute * * @return mixed */ public function __get($attribute) { if (property_exists($this, $attribute)) { return $this->{$attribute}; } $decimals = config('cart.format.decimals', 2); switch ($attribute) { case 'model': if (isset($this->associatedModel)) { return with(new $this->associatedModel())->find($this->id); } // no break case 'modelFQCN': if (isset($this->associatedModel)) { return $this->associatedModel; } // no break case 'weightTotal': return round($this->weight * $this->qty, $decimals); } $class = new ReflectionClass(config('cart.calculator', DefaultCalculator::class)); if (!$class->implementsInterface(Calculator::class)) { throw new InvalidCalculatorException('The configured Calculator seems to be invalid. Calculators have to implement the Calculator Contract.'); } return call_user_func($class->getName().'::getAttribute', $attribute, $this); } /** * Create a new instance from a Buyable. * * @param \Gloudemans\Shoppingcart\Contracts\Buyable $item * @param array $options * * @return \Gloudemans\Shoppingcart\CartItem */ public static function fromBuyable(Buyable $item, array $options = []) : self { return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $item->getBuyableWeight($options), $options); } /** * Create a new instance from the given array. * * @param array $attributes * * @return \Gloudemans\Shoppingcart\CartItem */ public static function fromArray(array $attributes) : self { $options = Arr::get($attributes, 'options', []); return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['weight'], $options); } /** * Create a new instance from the given attributes. * * @param int|string $id * @param string $name * @param float $price * @param array $options * * @return \Gloudemans\Shoppingcart\CartItem */ public static function fromAttributes($id, string $name, Money $price, $weight, array $options = []) : self { return new self($id, $name, $price, $weight, $options); } /** * Get the instance as an array. * * @return array */ public function toArray() { return [ 'rowId' => $this->rowId, 'id' => $this->id, 'name' => $this->name, 'qty' => $this->qty, 'price' => $this->price, 'weight' => $this->weight, 'options' => $this->options->toArray(), 'discount' => $this->discount, 'tax' => $this->tax, 'subtotal' => $this->subtotal, ]; } /** * Convert the object to its JSON representation. * * @param int $options * * @return string */ public function toJson($options = 0) { return json_encode($this->toArray(), $options); } /** * Generate a unique id for the cart item. * * @param string $id * @param array $options * * @return string */ protected function generateRowId(string $id, array $options) : string { ksort($options); return md5($id . serialize($options)); } }