setName($name); !is_null($email) && $this->setEmail($email); !is_null($phone) && $this->setPhone($phone); !is_null($inn) && $this->setInn($inn); } /** * Возвращает наименование покупателя * * @return string|null */ public function getName(): ?string { return $this->name; } /** * Устанавливает наименование покупателя * * @todo улучшить валидацию по Constraints::PATTERN_PHONE * @param string|null $name * @return $this * @throws TooLongClientNameException */ public function setName(?string $name): self { if (is_string($name)) { $name = preg_replace('/[\n\r\t]/', '', trim($name)); if (mb_strlen($name) > Constraints::MAX_LENGTH_CLIENT_NAME) { throw new TooLongClientNameException($name); } } $this->name = empty($name) ? null : $name; return $this; } /** * Возвращает установленный email * * @return string|null */ public function getEmail(): ?string { return $this->email; } /** * Устанавливает email * * @param string|null $email * @return $this * @throws TooLongEmailException Слишком длинный email * @throws InvalidEmailException Невалидный email */ public function setEmail(?string $email): self { if (is_string($email)) { $email = preg_replace('/[\n\r\t]/', '', trim($email)); if (mb_strlen($email) > Constraints::MAX_LENGTH_EMAIL) { throw new TooLongEmailException($email); } elseif (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { throw new InvalidEmailException($email); } } $this->email = empty($email) ? null : $email; return $this; } /** * Возвращает установленный телефон * * @return string|null */ public function getPhone(): ?string { return $this->phone; } /** * Устанавливает телефон * * @param string|null $phone Номер телефона * @return $this * @throws TooLongClientContactException */ public function setPhone(?string $phone): self { if (is_string($phone)) { $phone = preg_replace('/[^\d]/', '', trim($phone)); if (mb_strlen($phone) > Constraints::MAX_LENGTH_CLIENT_CONTACT) { throw new TooLongClientContactException($phone); } } $this->phone = empty($phone) ? null : "+$phone"; return $this; } /** * Возвращает установленный ИНН * * @return string|null */ public function getInn(): ?string { return $this->inn; } /** * Устанавливает ИНН * * @param string|null $inn * @return $this * @throws InvalidInnLengthException Некорректная длина ИНН */ public function setInn(?string $inn): self { if (is_string($inn)) { $inn = preg_replace('/[^\d]/', '', trim($inn)); if (preg_match_all(Constraints::PATTERN_INN, $inn) === 0) { throw new InvalidInnLengthException($inn); } } $this->inn = empty($inn) ? null : $inn; return $this; } /** * @inheritDoc */ public function jsonSerialize(): array { $json = []; $this->getName() && $json['name'] = $this->getName(); $this->getEmail() && $json['email'] = $this->getEmail(); $this->getPhone() && $json['phone'] = $this->getPhone(); $this->getInn() && $json['inn'] = $this->getInn(); return $json; } }