Общие сеттеры-геттеры сущностей вынесены в трейты HasEmail, HasInn, HasPhones

Кодстайл и микрорефакторинг сущностей
This commit is contained in:
2021-12-03 18:23:00 +08:00
parent c30c7d069f
commit 2260233e3f
13 changed files with 305 additions and 453 deletions

View File

@@ -11,9 +11,14 @@ declare(strict_types = 1);
namespace AtolOnline\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Exceptions\InvalidInnLengthException;
use AtolOnline\Exceptions\InvalidPhoneException;
use AtolOnline\Exceptions\{
InvalidInnLengthException,
InvalidPhoneException
};
use AtolOnline\Traits\{
HasInn,
HasPhones
};
use Illuminate\Support\Collection;
/**
@@ -23,21 +28,13 @@ use Illuminate\Support\Collection;
*/
class Supplier extends Entity
{
use HasPhones, HasInn;
/**
* @var string|null Наименование (1225)
*/
protected ?string $name = null;
/**
* @var string|null ИНН (1226)
*/
protected ?string $inn = null;
/**
* @var Collection Телефоны (1171)
*/
protected Collection $phones;
/**
* Конструктор
*
@@ -80,69 +77,6 @@ class Supplier extends Entity
return $this;
}
/**
* Возвращает установленные номера телефонов
*
* @todo вытащить в трейт
* @return Collection
*/
public function getPhones(): Collection
{
return $this->phones;
}
/**
* Устанавливает массив номеров телефонов
*
* @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;
}
/**
* Возвращает установленный ИНН
*
* @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
*/