Туча доработок

- класс `PayingAgent`, покрытый тестами
- новые константы для тегов ФФД 1.05 `Ffd105Tags`
- `Entity::jsonSerialize()` object -> array (again)
- `TooManyException::$max` int -> float
- тесты по psr-4, потому что почему бы и нет
- некоторые провайдеры вынесены в `BasicTestCase`
- улучшен тест покупателя
This commit is contained in:
2021-11-24 01:30:54 +08:00
parent b5a01debd2
commit 42d194116f
40 changed files with 622 additions and 192 deletions

View File

@@ -17,8 +17,8 @@ use AtolOnline\{
Exceptions\InvalidInnLengthException,
Exceptions\TooLongClientContactException,
Exceptions\TooLongClientNameException,
Exceptions\TooLongEmailException,
Exceptions\TooLongItemNameException};
Exceptions\TooLongEmailException
};
/**
* Класс Client, описывающий сущность покупателя
@@ -50,11 +50,11 @@ class Client extends Entity
/**
* Конструктор объекта покупателя
*
* @param string|null $name Наименование. Тег ФФД - 1227.
* @param string|null $phone Email. Тег ФФД - 1008.
* @param string|null $email Телефон покупателя. Тег ФФД - 1008.
* @param string|null $inn ИНН. Тег ФФД - 1228.
* @throws TooLongItemNameException
* @param string|null $name Наименование (1227)
* @param string|null $phone Email (1008)
* @param string|null $email Телефон покупателя (1008)
* @param string|null $inn ИНН (1228)
* @throws TooLongClientNameException
* @throws TooLongClientContactException
* @throws TooLongEmailException
* @throws InvalidEmailException
@@ -90,6 +90,7 @@ class Client extends Entity
*
* Тег ФФД - 1227
*
* @todo улучшить валидацию по Constraints::PATTERN_PHONE
* @param string|null $name
* @return $this
* @throws TooLongClientNameException
@@ -203,13 +204,13 @@ class Client extends Entity
/**
* @inheritDoc
*/
public function jsonSerialize(): object
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 (object)$json;
return $json;
}
}

View File

@@ -205,9 +205,9 @@ class Company extends Entity
* @throws InvalidInnLengthException
* @throws InvalidPaymentAddressException
*/
public function jsonSerialize(): object
public function jsonSerialize(): array
{
return (object)[
return [
'email' => $this->email
? $this->getEmail()
: throw new InvalidEmailException(),
@@ -222,4 +222,4 @@ class Company extends Entity
: throw new InvalidPaymentAddressException(),
];
}
}
}

View File

@@ -123,12 +123,12 @@ class CorrectionInfo extends Entity
/**
* @inheritDoc
*/
public function jsonSerialize(): object
public function jsonSerialize(): array
{
return (object)[
'type' => $this->getType() ?? '', // обязателен
'base_date' => $this->getDate() ?? '', // обязателен
'base_number' => $this->getNumber() ?? '', // обязателен
return [
'type' => $this->getType() ?? '',
'base_date' => $this->getDate() ?? '',
'base_number' => $this->getNumber() ?? '',
];
}
}
}

View File

@@ -129,8 +129,8 @@ final class Kkt extends Entity
/**
* @inheritDoc
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->data;
return (array)$this->data;
}
}

View File

@@ -0,0 +1,123 @@
<?php
/*
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
declare(strict_types = 1);
namespace AtolOnline\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Exceptions\InvalidPhoneException;
use AtolOnline\Exceptions\TooLongPayingAgentOperationException;
use Illuminate\Support\Collection;
/**
* Класс, описывающий данные платёжного агента
*
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 19
*/
class PayingAgent extends Entity
{
/**
* @var string|null Наименование операции (1044)
*/
protected ?string $operation = null;
/**
* @var Collection Телефоны платежного агента (1073)
*/
protected Collection $phones;
/**
* Конструктор
*
* @param string|null $operation Наименование операции (1044)
* @param array|Collection|null $phones Телефоны платежного агента (1073)
* @throws TooLongPayingAgentOperationException
* @throws InvalidPhoneException
*/
public function __construct(
?string $operation = null,
array|Collection|null $phones = null,
) {
!is_null($operation) && $this->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;
}
/**
* Устанавливает массив номеров телефонов
*
* @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 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;
}
}