2021-12-03 10:23:00 +00:00
|
|
|
<?php
|
2022-12-14 16:19:55 +00:00
|
|
|
|
2021-12-03 10:23:00 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2022-12-14 16:19:55 +00:00
|
|
|
use AtolOnline\Constraints;
|
2021-12-03 10:23:00 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 11:04:14 +00:00
|
|
|
$this->email = $email ?: null;
|
2021-12-03 10:23:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Возвращает установленный email
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getEmail(): ?string
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
}
|