[new HttpMethod([HttpMethod::METHOD_GET])]]; $write = ["prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf()]]; return [ "state" => $read, "cart" => $read, "set" => $write, "update" => $write, "clear" => $write, "coupon" => $write, "couponRemove" => $write, "addShared" => $write, ]; } public function addSharedAction(string $code): ?array { $items = Cart::parseShare(trim($code)); if (!$items) { $this->addError(new Error("Wrong share code", "share")); return null; } $basket = $this->loadBasket(); if ($basket === null) { return null; } $ok = Cart::addMany($basket, $items); $basket = $this->loadBasket(); return $basket === null ? null : ["ok" => $ok] + Cart::state($basket); } public function stateAction(): ?array { $basket = $this->loadBasket(); return $basket === null ? null : ["ok" => true] + Cart::state($basket); } public function setAction(int $id, int $qty): ?array { $basket = $this->loadBasket(); if ($basket === null || !$this->validId($id)) { return null; } $ok = Cart::setQuantity($basket, $id, $qty); $basket = $this->loadBasket(); return $basket === null ? null : ["ok" => $ok] + Cart::state($basket); } public function cartAction(): ?array { $basket = $this->loadBasket(); return $basket === null ? null : $this->full($basket, true, true); } public function updateAction(int $id, int $qty): ?array { $basket = $this->loadBasket(); if ($basket === null || !$this->validId($id)) { return null; } $ok = Cart::setQuantity($basket, $id, $qty); $basket = $this->loadBasket(); return $basket === null ? null : $this->full($basket, $ok); } public function clearAction(): ?array { $basket = $this->loadBasket(); if ($basket === null) { return null; } $ok = Cart::clear($basket); $basket = $this->loadBasket(); return $basket === null ? null : $this->full($basket, $ok); } public function couponAction(string $code): ?array { $code = trim($code); if ($code === "" || mb_strlen($code) > self::COUPON_MAX_LENGTH) { $this->addError(new Error("Wrong coupon", "coupon")); return null; } $basket = $this->loadBasket(); if ($basket === null) { return null; } if (!Cart::addCoupon($code)) { Cart::removeCoupon($code); $this->addError(new Error("Coupon is not applicable", "coupon")); return null; } $result = $this->full($basket, true); foreach ($result["cart"]["coupons"] as $coupon) { if (mb_strtolower($coupon["code"]) === mb_strtolower($code) && $coupon["applied"]) { return $result; } } Cart::removeCoupon($code); $this->addError(new Error("Coupon is not applicable", "coupon")); return null; } public function couponRemoveAction(string $code): ?array { $basket = $this->loadBasket(); if ($basket === null) { return null; } Cart::removeCoupon(trim($code)); return $this->full($basket, true); } private function full(BasketBase $basket, bool $ok, bool $refresh = false): array { return ["ok" => $ok, "cart" => Cart::view($basket, $refresh), "state" => Cart::state($basket)]; } private function loadBasket(): ?BasketBase { $basket = Cart::load(); if ($basket === null) { $this->addError(new Error("Modules sale and catalog are required")); } return $basket; } private function validId(int $id): bool { if ($id > 0) { return true; } $this->addError(new Error("Wrong product id")); return false; } }