Общие сеттеры-геттеры сущностей вынесены в трейты HasEmail, HasInn, HasPhones
Кодстайл и микрорефакторинг сущностей
This commit is contained in:
57
src/Traits/HasEmail.php
Normal file
57
src/Traits/HasEmail.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
|
||||
*
|
||||
* This code is licensed under MIT.
|
||||
* Этот код распространяется по лицензии MIT.
|
||||
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace AtolOnline\Traits;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Exceptions\InvalidEmailException;
|
||||
use AtolOnline\Exceptions\TooLongEmailException;
|
||||
|
||||
/**
|
||||
* Трейт для сущностей, которые могут иметь email
|
||||
*/
|
||||
trait HasEmail
|
||||
{
|
||||
/**
|
||||
* @var string|null Email (1008, 1117)
|
||||
*/
|
||||
protected ?string $email = null;
|
||||
|
||||
/**
|
||||
* Устанавливает email
|
||||
*
|
||||
* @param string|null $email
|
||||
* @return $this
|
||||
* @throws TooLongEmailException
|
||||
* @throws InvalidEmailException
|
||||
*/
|
||||
public function setEmail(?string $email): static
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает установленный email
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getEmail(): ?string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
}
|
||||
53
src/Traits/HasInn.php
Normal file
53
src/Traits/HasInn.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
|
||||
*
|
||||
* This code is licensed under MIT.
|
||||
* Этот код распространяется по лицензии MIT.
|
||||
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace AtolOnline\Traits;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Exceptions\InvalidInnLengthException;
|
||||
|
||||
/**
|
||||
* Трейт для сущностей, которые могут иметь ИНН
|
||||
*/
|
||||
trait HasInn
|
||||
{
|
||||
/**
|
||||
* @var string|null ИНН (1226, 1228, 1018)
|
||||
*/
|
||||
protected ?string $inn = null;
|
||||
|
||||
/**
|
||||
* Устанавливает ИНН
|
||||
*
|
||||
* @param string|null $inn
|
||||
* @return $this
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function setInn(?string $inn): static
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает установленный ИНН
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getInn(): ?string
|
||||
{
|
||||
return $this->inn;
|
||||
}
|
||||
}
|
||||
57
src/Traits/HasPhones.php
Normal file
57
src/Traits/HasPhones.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
|
||||
*
|
||||
* This code is licensed under MIT.
|
||||
* Этот код распространяется по лицензии MIT.
|
||||
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace AtolOnline\Traits;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Exceptions\InvalidPhoneException;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Трейт для сущностей, которые могут иметь массив номеров телефонов
|
||||
*/
|
||||
trait HasPhones
|
||||
{
|
||||
/**
|
||||
* @var Collection Телефоны платёжного агента (1073), поставщика (1171), оператора по приёму платежей (1074)
|
||||
*/
|
||||
protected Collection $phones;
|
||||
|
||||
/**
|
||||
* Устанавливает массив номеров телефонов
|
||||
*
|
||||
* @param array|Collection|null $phones
|
||||
* @return $this
|
||||
* @throws InvalidPhoneException
|
||||
*/
|
||||
public function setPhones(array|Collection|null $phones): static
|
||||
{
|
||||
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 Collection
|
||||
*/
|
||||
public function getPhones(): Collection
|
||||
{
|
||||
return $this->phones;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user