Доработка коллекций и не только

- коллекция `Items` с покрытием
- вынос коллекций из `AtolOnline\Entities` в `AtolOnline\Collections`
- фикс ] в `AtolException`
- финализирован `CorrectionInfo`
- фиксы по тестам коллекций
- прочие мелочи по phpdoc
This commit is contained in:
2021-12-06 16:14:19 +08:00
parent bf09641c8b
commit 557c76fefa
25 changed files with 441 additions and 120 deletions

View File

@@ -15,16 +15,14 @@ use AtolOnline\{
Constants\Constraints,
Enums\SnoTypes,
Traits\HasEmail,
Traits\HasInn
};
Traits\HasInn};
use AtolOnline\Exceptions\{
InvalidEmailException,
InvalidEnumValueException,
InvalidInnLengthException,
InvalidPaymentAddressException,
TooLongEmailException,
TooLongPaymentAddressException
};
TooLongPaymentAddressException};
use JetBrains\PhpStorm\ArrayShape;
/**
@@ -131,10 +129,10 @@ final class Company extends Entity
* @throws InvalidPaymentAddressException
*/
#[ArrayShape([
'email' => "string",
'sno' => "string",
'inn' => "string",
'payment_address' => "string",
'email' => 'string',
'sno' => 'string',
'inn' => 'string',
'payment_address' => 'string',
])]
public function jsonSerialize(): array
{

View File

@@ -16,21 +16,19 @@ use AtolOnline\Enums\CorrectionTypes;
use AtolOnline\Exceptions\{
EmptyCorrectionNumberException,
InvalidCorrectionDateException,
InvalidEnumValueException
};
InvalidEnumValueException};
use DateTime;
use Exception;
use JetBrains\PhpStorm\{
ArrayShape,
Pure
};
Pure};
/**
* Класс, описывающий данные коррекции
*
* @see https://online.atol.ru/files/API_atol_online_v4.pdf Документация, стр 35
*/
class CorrectionInfo extends Entity
final class CorrectionInfo extends Entity
{
/**
* @var string|null Тип коррекции (1173)

View File

@@ -1,119 +0,0 @@
<?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 Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Collection;
/**
* Абстрактное описание коллекции любых сущностей
*
* @todo вот бы ещё проверять классы добавляемых объектов через static.... ммм мякотка
*/
abstract class EntityCollection extends Collection
{
/**
* @inheritDoc
*/
public function __construct($items = [])
{
$this->checkCount($items);
parent::__construct($items);
}
/**
* @inheritDoc
*/
public function prepend($value, $key = null): self
{
$this->checkCount();
return parent::prepend($value, $key);
}
/**
* @inheritDoc
*/
public function add($item): self
{
$this->checkCount();
return parent::add($item);
}
/**
* @inheritDoc
*/
public function push(...$values): self
{
$this->checkCount();
return parent::push(...$values);
}
/**
* @inheritDoc
*/
public function merge($items): self
{
$this->checkCount();
return parent::merge($items);
}
/**
* @inheritDoc
* @throws \Exception
*/
public function jsonSerialize(): array
{
return array_map(function ($value) {
$this->checkEntityClass($value);
if ($value instanceof \JsonSerializable) {
return $value->jsonSerialize();
} elseif ($value instanceof Jsonable) {
return json_decode($value->toJson(), true);
} elseif ($value instanceof Arrayable) {
return $value->toArray();
}
return $value;
}, $this->all());
}
/**
* Проверяет количество ставок
*
* @param array $items Массив элементов, если пустой - проверит содержимое коллекции
* @return void
*/
private function checkCount(array $items = []): void
{
if (
count($items) > static::MAX_COUNT ||
$this->count() === static::MAX_COUNT
) {
$exception = static::EXCEPTION_CLASS;
throw new $exception(static::MAX_COUNT);
}
}
/**
* @throws \Exception
*/
private function checkEntityClass(mixed $item): void
{
if (!is_object($item) || $item::class !== static::ENTITY_CLASS) {
//TODO proper exception
throw new \Exception(
'Коллекция должна содержать только объекты класса ' .
static::ENTITY_CLASS . ', найден ' . $item::class
);
}
}
}

View File

@@ -13,11 +13,13 @@ namespace AtolOnline\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Enums\PaymentTypes;
use AtolOnline\Exceptions\InvalidEnumValueException;
use AtolOnline\Exceptions\NegativePaymentSumException;
use AtolOnline\Exceptions\TooHighPaymentSumException;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use AtolOnline\Exceptions\{
InvalidEnumValueException,
NegativePaymentSumException,
TooHighPaymentSumException,};
use JetBrains\PhpStorm\{
ArrayShape,
Pure};
/**
* Класс, описывающий оплату
@@ -41,9 +43,9 @@ class Payment extends Entity
*
* @param int $type Тип оплаты
* @param float $sum Сумма оплаты
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws InvalidEnumValueException
*/
public function __construct(int $type, float $sum)
{

View File

@@ -1,34 +0,0 @@
<?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\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Exceptions\TooManyPaymentsException;
/**
* Класс, описывающий коллекцию оплат для документа
*/
final class Payments extends EntityCollection
{
/**
* Класс объектов, находящихся в коллекции
*/
protected const ENTITY_CLASS = Payment::class;
/**
* Максмальное количество объектов в коллекции
*/
protected const MAX_COUNT = Constraints::MAX_COUNT_DOC_PAYMENTS;
/**
* Класс-наследник TooManyException для выброса при превышении количества
*/
protected const EXCEPTION_CLASS = TooManyPaymentsException::class;
}

View File

@@ -1,29 +0,0 @@
<?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\Entities;
use AtolOnline\Constants\Constraints;
use AtolOnline\Exceptions\TooManyVatsException;
/**
* Класс, описывающий коллекцию ставок НДС для документа
*/
final class Vats extends EntityCollection
{
/**
* Максмальное количество ставок НДС
*/
protected const MAX_COUNT = Constraints::MAX_COUNT_DOC_VATS;
/**
* Класс-наследник TooManyException для выброса при превышении количества
*/
protected const EXCEPTION_CLASS = TooManyVatsException::class;
}