2021-12-06 06:16:34 +00:00
|
|
|
<?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);
|
|
|
|
|
2021-12-06 08:14:19 +00:00
|
|
|
namespace AtolOnline\Collections;
|
2021-12-06 06:16:34 +00:00
|
|
|
|
2021-12-06 08:14:19 +00:00
|
|
|
use AtolOnline\Exceptions\InvalidEntityInCollectionException;
|
2021-12-06 06:16:34 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Абстрактное описание коллекции любых сущностей
|
|
|
|
*/
|
|
|
|
abstract class EntityCollection extends Collection
|
|
|
|
{
|
|
|
|
/**
|
2021-12-11 07:53:57 +00:00
|
|
|
* @inheritDoc
|
2021-12-07 12:04:03 +00:00
|
|
|
* @throws InvalidEntityInCollectionException
|
2021-12-06 06:16:34 +00:00
|
|
|
*/
|
|
|
|
public function jsonSerialize(): array
|
|
|
|
{
|
2021-12-09 12:13:43 +00:00
|
|
|
$this->checkCount();
|
2021-12-07 12:04:03 +00:00
|
|
|
$this->checkItemsClasses();
|
2021-12-06 08:14:19 +00:00
|
|
|
return parent::jsonSerialize();
|
2021-12-06 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-07 12:04:03 +00:00
|
|
|
* Проверяет количество элементов коллекции
|
2021-12-06 06:16:34 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-12-09 12:13:43 +00:00
|
|
|
public function checkCount(): void
|
2021-12-06 06:16:34 +00:00
|
|
|
{
|
2021-12-09 12:13:43 +00:00
|
|
|
$this->isEmpty() && throw new (static::EMPTY_EXCEPTION_CLASS)();
|
|
|
|
$this->count() > static::MAX_COUNT && throw new (static::TOO_MANY_EXCEPTION_CLASS)(static::MAX_COUNT);
|
2021-12-06 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-07 12:04:03 +00:00
|
|
|
* Проверяет корректность класса элемента коллекции
|
2021-12-06 08:14:19 +00:00
|
|
|
*
|
2021-12-09 12:13:43 +00:00
|
|
|
* @param mixed $item
|
|
|
|
* @return void
|
2021-12-06 08:14:19 +00:00
|
|
|
* @throws InvalidEntityInCollectionException
|
2021-12-06 06:16:34 +00:00
|
|
|
*/
|
2021-12-07 12:04:03 +00:00
|
|
|
public function checkItemClass(mixed $item): void
|
2021-12-06 06:16:34 +00:00
|
|
|
{
|
|
|
|
if (!is_object($item) || $item::class !== static::ENTITY_CLASS) {
|
2021-12-06 08:14:19 +00:00
|
|
|
throw new InvalidEntityInCollectionException(static::class, static::ENTITY_CLASS, $item);
|
2021-12-06 06:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 12:04:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Проверяет корректность классов элементов коллекции
|
|
|
|
*
|
2021-12-09 12:13:43 +00:00
|
|
|
* @return $this
|
2021-12-07 12:04:03 +00:00
|
|
|
* @throws InvalidEntityInCollectionException
|
|
|
|
*/
|
2021-12-09 12:13:43 +00:00
|
|
|
public function checkItemsClasses(): self
|
2021-12-07 12:04:03 +00:00
|
|
|
{
|
2021-12-09 12:13:43 +00:00
|
|
|
return $this->each(fn($item) => $this->checkItemClass($item));
|
2021-12-07 12:04:03 +00:00
|
|
|
}
|
2021-12-06 06:16:34 +00:00
|
|
|
}
|