Доработки коллекций, чека и тестов
- `EntityCollection` сильно упрощён, добавлен выброс исключений при пустом содержимом - `Receipt::setItems(), setPayments() и setVats()` получили одинаковые проверки входящих данных - округление в `Vat::setSum()` - доработаны тесты коллекций
This commit is contained in:
@@ -12,7 +12,6 @@ declare(strict_types = 1);
|
||||
namespace AtolOnline\Collections;
|
||||
|
||||
use AtolOnline\Exceptions\InvalidEntityInCollectionException;
|
||||
use Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
@@ -21,60 +20,12 @@ use Illuminate\Support\Collection;
|
||||
abstract class EntityCollection extends Collection
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @return array
|
||||
* @throws InvalidEntityInCollectionException
|
||||
*/
|
||||
public function __construct($items = [])
|
||||
{
|
||||
$this->checkCount($items);
|
||||
//TODO следует переделать EntityCollection в обёртку над Collection,
|
||||
// ибо ломает методы Collection, которые return new static
|
||||
$this->checkItemsClasses($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
|
||||
{
|
||||
$this->checkCount();
|
||||
$this->checkItemsClasses();
|
||||
return parent::jsonSerialize();
|
||||
}
|
||||
@@ -82,20 +33,19 @@ abstract class EntityCollection extends Collection
|
||||
/**
|
||||
* Проверяет количество элементов коллекции
|
||||
*
|
||||
* @param array $items Массив элементов, если пустой - проверит содержимое коллекции
|
||||
* @return void
|
||||
*/
|
||||
public function checkCount(array $items = []): void
|
||||
public function checkCount(): void
|
||||
{
|
||||
//TODO проверять пустоту?
|
||||
if (count($items) > static::MAX_COUNT || $this->count() === static::MAX_COUNT) {
|
||||
throw new (static::EXCEPTION_CLASS)(static::MAX_COUNT);
|
||||
}
|
||||
$this->isEmpty() && throw new (static::EMPTY_EXCEPTION_CLASS)();
|
||||
$this->count() > static::MAX_COUNT && throw new (static::TOO_MANY_EXCEPTION_CLASS)(static::MAX_COUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет корректность класса элемента коллекции
|
||||
*
|
||||
* @param mixed $item
|
||||
* @return void
|
||||
* @throws InvalidEntityInCollectionException
|
||||
*/
|
||||
public function checkItemClass(mixed $item): void
|
||||
@@ -108,10 +58,11 @@ abstract class EntityCollection extends Collection
|
||||
/**
|
||||
* Проверяет корректность классов элементов коллекции
|
||||
*
|
||||
* @return $this
|
||||
* @throws InvalidEntityInCollectionException
|
||||
*/
|
||||
public function checkItemsClasses(array $items = []): void
|
||||
public function checkItemsClasses(): self
|
||||
{
|
||||
(empty($items) ? $this : collect($items))->each(fn ($item) => $this->checkItemClass($item));
|
||||
return $this->each(fn($item) => $this->checkItemClass($item));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace AtolOnline\Collections;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Entities\Item;
|
||||
use AtolOnline\Exceptions\EmptyItemsException;
|
||||
use AtolOnline\Exceptions\TooManyItemsException;
|
||||
|
||||
/**
|
||||
@@ -28,8 +29,13 @@ final class Items extends EntityCollection
|
||||
*/
|
||||
protected const MAX_COUNT = Constraints::MAX_COUNT_DOC_ITEMS;
|
||||
|
||||
/**
|
||||
* Класс исключения для выброса при пустой коллекции
|
||||
*/
|
||||
protected const EMPTY_EXCEPTION_CLASS = EmptyItemsException::class;
|
||||
|
||||
/**
|
||||
* Класс-наследник TooManyException для выброса при превышении количества
|
||||
*/
|
||||
protected const EXCEPTION_CLASS = TooManyItemsException::class;
|
||||
protected const TOO_MANY_EXCEPTION_CLASS = TooManyItemsException::class;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace AtolOnline\Collections;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Entities\Payment;
|
||||
use AtolOnline\Exceptions\EmptyPaymentsException;
|
||||
use AtolOnline\Exceptions\TooManyPaymentsException;
|
||||
|
||||
/**
|
||||
@@ -28,8 +29,13 @@ final class Payments extends EntityCollection
|
||||
*/
|
||||
protected const MAX_COUNT = Constraints::MAX_COUNT_DOC_PAYMENTS;
|
||||
|
||||
/**
|
||||
* Класс исключения для выброса при пустой коллекции
|
||||
*/
|
||||
protected const EMPTY_EXCEPTION_CLASS = EmptyPaymentsException::class;
|
||||
|
||||
/**
|
||||
* Класс-наследник TooManyException для выброса при превышении количества
|
||||
*/
|
||||
protected const EXCEPTION_CLASS = TooManyPaymentsException::class;
|
||||
protected const TOO_MANY_EXCEPTION_CLASS = TooManyPaymentsException::class;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace AtolOnline\Collections;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Entities\Vat;
|
||||
use AtolOnline\Exceptions\EmptyVatsException;
|
||||
use AtolOnline\Exceptions\TooManyVatsException;
|
||||
|
||||
/**
|
||||
@@ -28,8 +29,13 @@ final class Vats extends EntityCollection
|
||||
*/
|
||||
protected const MAX_COUNT = Constraints::MAX_COUNT_DOC_VATS;
|
||||
|
||||
/**
|
||||
* Класс исключения для выброса при пустой коллекции
|
||||
*/
|
||||
protected const EMPTY_EXCEPTION_CLASS = EmptyVatsException::class;
|
||||
|
||||
/**
|
||||
* Класс-наследник TooManyException для выброса при превышении количества
|
||||
*/
|
||||
protected const EXCEPTION_CLASS = TooManyVatsException::class;
|
||||
protected const TOO_MANY_EXCEPTION_CLASS = TooManyVatsException::class;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@ use AtolOnline\Collections\Payments;
|
||||
use AtolOnline\Collections\Vats;
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Exceptions\EmptyItemsException;
|
||||
use AtolOnline\Exceptions\EmptyPaymentsException;
|
||||
use AtolOnline\Exceptions\EmptyVatsException;
|
||||
use AtolOnline\Exceptions\InvalidEntityInCollectionException;
|
||||
use AtolOnline\Exceptions\TooLongAddCheckPropException;
|
||||
use AtolOnline\Exceptions\TooLongCashierException;
|
||||
@@ -93,7 +91,6 @@ class Receipt extends Entity
|
||||
* @param Items $items
|
||||
* @param Payments $payments
|
||||
* @throws EmptyItemsException
|
||||
* @throws EmptyPaymentsException
|
||||
* @throws InvalidEntityInCollectionException
|
||||
*/
|
||||
public function __construct(Client $client, Company $company, Items $items, Payments $payments)
|
||||
@@ -211,12 +208,10 @@ class Receipt extends Entity
|
||||
*/
|
||||
public function setItems(Items $items): self
|
||||
{
|
||||
if ($items->isEmpty()) {
|
||||
throw new EmptyItemsException();
|
||||
}
|
||||
$items->checkCount();
|
||||
$items->checkItemsClasses();
|
||||
$this->items = $items;
|
||||
$this->getItems()->each(fn ($item) => $this->total += $item->getSum());
|
||||
$this->getItems()->each(fn($item) => $this->total += $item->getSum());
|
||||
$this->total = round($this->total, 2);
|
||||
return $this;
|
||||
}
|
||||
@@ -236,13 +231,12 @@ class Receipt extends Entity
|
||||
*
|
||||
* @param Payments $payments
|
||||
* @return Receipt
|
||||
* @throws EmptyPaymentsException
|
||||
* @throws InvalidEntityInCollectionException
|
||||
*/
|
||||
public function setPayments(Payments $payments): self
|
||||
{
|
||||
if ($payments->isEmpty()) {
|
||||
throw new EmptyPaymentsException();
|
||||
}
|
||||
$payments->checkCount();
|
||||
$payments->checkItemsClasses();
|
||||
$this->payments = $payments;
|
||||
return $this;
|
||||
}
|
||||
@@ -262,17 +256,15 @@ class Receipt extends Entity
|
||||
*
|
||||
* @param Vats|null $vats
|
||||
* @return Receipt
|
||||
* @throws EmptyVatsException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setVats(?Vats $vats): self
|
||||
{
|
||||
if ($vats->isEmpty()) {
|
||||
throw new EmptyVatsException();
|
||||
}
|
||||
$vats->checkCount();
|
||||
$vats->checkItemsClasses();
|
||||
$this->vats = $vats;
|
||||
/** @var Vat $vat */
|
||||
$this->getVats()->each(fn ($vat) => $vat->setSum($this->getTotal()));
|
||||
$this->getVats()->each(fn($vat) => $vat->setSum($this->getTotal()));
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -376,13 +368,13 @@ class Receipt extends Entity
|
||||
$json = [
|
||||
'client' => $this->getClient(),
|
||||
'company' => $this->getCompany(),
|
||||
'items' => $this->getItems(),
|
||||
'items' => $this->getItems()->jsonSerialize(),
|
||||
'total' => $this->getTotal(),
|
||||
'payments' => $this->getPayments(),
|
||||
'payments' => $this->getPayments()->jsonSerialize(),
|
||||
];
|
||||
$this->getAgentInfo()?->jsonSerialize() && $json['agent_info'] = $this->getAgentInfo();
|
||||
$this->getSupplier()?->jsonSerialize() && $json['supplier_info'] = $this->getSupplier();
|
||||
$this->getVats()?->jsonSerialize() && $json['vats'] = $this->getVats();
|
||||
$this->getVats()?->isNotEmpty() && $json['vats'] = $this->getVats();
|
||||
!is_null($this->getAddCheckProps()) && $json['additional_check_props'] = $this->getAddCheckProps();
|
||||
!is_null($this->getCashier()) && $json['cashier'] = $this->getCashier();
|
||||
$this->getAddUserProps()?->jsonSerialize() && $json['additional_user_props'] = $this->getAddUserProps();
|
||||
|
||||
@@ -91,7 +91,7 @@ final class Vat extends Entity
|
||||
*/
|
||||
public function setSum(float $rubles): self
|
||||
{
|
||||
$this->sum = $rubles;
|
||||
$this->sum = round($rubles, 2);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user