` * (`PHPShopDone::send_to_order()`) — это не адрес покупателя. */ private const GUEST_EMAIL_PREFIX = 'guest@'; /** @var int */ private $id; /** @var string */ private $uid; /** @var Money */ private $amount; /** @var int */ private $statusId; /** @var bool */ private $paid; /** @var int */ private $paymentMethodId; /** @var string */ private $email; /** @var string */ private $phone; /** @var string */ private $customerName; /** @var int */ private $userId; /** @var array */ private $cart; /** @var float */ private $discountPercent; /** @var int */ private $deliveryMethodId; private function __construct() {} /** * @param array $row строка `phpshop_orders` * @param array $details распакованное поле `orders`: `['Cart' => ..., 'Person' => ...]` */ public static function fromRow(array $row, array $details): self { $id = (int) ($row['id'] ?? 0); $uid = (string) ($row['uid'] ?? ''); if (($id <= 0) || ($uid === '')) { throw new InvalidArgumentException('Строка заказа без id или uid.'); } $person = isset($details['Person']) && is_array($details['Person']) ? $details['Person'] : []; $cart = isset($details['Cart']) && is_array($details['Cart']) ? $details['Cart'] : []; $order = new self(); $order->id = $id; $order->uid = $uid; $order->amount = Money::fromFloat((float) ($row['sum'] ?? 0)); $order->statusId = (int) ($row['statusi'] ?? 0); $order->paid = (int) ($row['paid'] ?? 0) === 1; $order->paymentMethodId = (int) ($person['order_metod'] ?? 0); $order->email = self::email((string) ($person['mail'] ?? '')); $order->phone = trim((string) ($row['tel'] ?? '')); $order->customerName = trim((string) ($row['fio'] ?? '')); $order->userId = (int) ($row['user'] ?? 0); $order->cart = $cart; $order->discountPercent = (float) ($person['discount'] ?? 0); $order->deliveryMethodId = (int) ($person['dostavka_metod'] ?? 0); return $order; } public function getId(): int { return $this->id; } /** * Номер заказа, который видит покупатель (`4-36`). */ public function getUid(): string { return $this->uid; } /** * Итог заказа (`phpshop_orders.sum`): товары со скидками, бонусы и доставка уже учтены ядром. */ public function getAmount(): Money { return $this->amount; } public function getStatusId(): int { return $this->statusId; } public function isPaidFlag(): bool { return $this->paid; } public function getPaymentMethodId(): int { return $this->paymentMethodId; } /** * Email покупателя или пустая строка (гостевой `guest@...` не считается). */ public function getEmail(): string { return $this->email; } public function getPhone(): string { return $this->phone; } public function getCustomerName(): string { return $this->customerName; } /** * ID покупателя PHPShop (`phpshop_shopusers.id`), 0 — нет. */ public function getUserId(): int { return $this->userId; } /** * Корзина заказа как её хранит ядро (`Cart`: `cart`, `dostavka`, `sum`...) — для чека. * * @return array */ public function getCart(): array { return $this->cart; } /** * Скидка покупателя в процентах (`Person.discount`): ядро применяет её ко всем товарам, * кроме товаров с промо-ценой. */ public function getDiscountPercent(): float { return $this->discountPercent; } /** * Способ доставки (`phpshop_delivery.id`), 0 — не выбран. */ public function getDeliveryMethodId(): int { return $this->deliveryMethodId; } private static function email(string $raw): string { $email = trim($raw); if (($email === '') || stripos($email, self::GUEST_EMAIL_PREFIX) === 0) { return ''; } return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) ? $email : ''; } }