setOperation($operation); $this->setPhones($phones); } /** * Устанавливает операцию * * @param string|null $operation * @return $this * @throws TooLongPayingAgentOperationException */ public function setOperation(?string $operation): self { if (!is_null($operation)) { $operation = trim($operation); if (mb_strlen($operation) > Constraints::MAX_LENGTH_PAYING_AGENT_OPERATION) { throw new TooLongPayingAgentOperationException($operation); } } $this->operation = empty($operation) ? null : $operation; return $this; } /** * Вoзвращает установленную операцию * * @return string|null */ public function getOperation(): ?string { return $this->operation; } /** * Устанавливает массив номеров телефонов * * @todo вытащить в трейт * @param array|Collection|null $phones * @return $this * @throws InvalidPhoneException */ public function setPhones(array|Collection|null $phones): self { if (!is_null($phones)) { $phones = is_array($phones) ? collect($phones) : $phones; $phones->each(function ($phone) { $phone = preg_replace('/[^\d]/', '', trim($phone)); if (preg_match(Constraints::PATTERN_PHONE, $phone) != 1) { throw new InvalidPhoneException($phone); } }); } $this->phones = empty($phones) ? collect() : $phones; return $this; } /** * Возвращает установленные номера телефонов * * @todo вытащить в трейт * @return Collection */ public function getPhones(): Collection { return $this->phones; } /** * @inheritDoc */ public function jsonSerialize(): array { $json = []; $this->getOperation() && $json['operation'] = $this->getOperation(); !$this->getPhones()->isEmpty() && $json['phones'] = $this->getPhones()->toArray(); return $json; } }