diff --git a/src/Collections/EntityCollection.php b/src/Collections/EntityCollection.php index d66da8f..080cb52 100644 --- a/src/Collections/EntityCollection.php +++ b/src/Collections/EntityCollection.php @@ -87,6 +87,7 @@ abstract class EntityCollection extends Collection */ public function checkCount(array $items = []): void { + //TODO проверять пустоту? if (count($items) > static::MAX_COUNT || $this->count() === static::MAX_COUNT) { throw new (static::EXCEPTION_CLASS)(static::MAX_COUNT); } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 490fdc8..e0eee84 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -11,14 +11,29 @@ declare(strict_types = 1); namespace AtolOnline\Entities; +use ArrayAccess; +use Illuminate\Contracts\Support\Arrayable; use JsonSerializable; use Stringable; /** * Абстрактное описание любой сущности, представляемой как json */ -abstract class Entity implements JsonSerializable, Stringable +abstract class Entity implements JsonSerializable, Stringable, Arrayable, ArrayAccess { + /** + * @inheritDoc + */ + abstract public function jsonSerialize(): array; + + /** + * @inheritDoc + */ + public function toArray() + { + return $this->jsonSerialize(); + } + /** * Возвращает строковое представление json-структуры объекта * @@ -26,6 +41,42 @@ abstract class Entity implements JsonSerializable, Stringable */ public function __toString() { - return json_encode($this->jsonSerialize(), JSON_UNESCAPED_UNICODE); + return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE); + } + + /** + * @inheritDoc + */ + public function offsetExists(mixed $offset): bool + { + return isset($this->toArray()[$offset]); + } + + /** + * @inheritDoc + */ + public function offsetGet(mixed $offset): mixed + { + return $this->toArray()[$offset]; + } + + /** + * @inheritDoc + */ + public function offsetSet(mixed $offset, mixed $value) + { + throw new \BadMethodCallException( + 'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.' + ); + } + + /** + * @inheritDoc + */ + public function offsetUnset(mixed $offset): void + { + throw new \BadMethodCallException( + 'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.' + ); } } diff --git a/src/Entities/Vat.php b/src/Entities/Vat.php index 69f737c..771380c 100644 --- a/src/Entities/Vat.php +++ b/src/Entities/Vat.php @@ -16,8 +16,7 @@ use AtolOnline\Exceptions\InvalidEnumValueException; use AtolOnline\Helpers; use JetBrains\PhpStorm\{ ArrayShape, - Pure -}; + Pure}; /** * Класс, описывающий ставку НДС @@ -97,7 +96,7 @@ final class Vat extends Entity } /** - * Возвращает sdрасчитанный итоговый размер ставки НДС в рублях + * Возвращает расчитанный итоговый размер ставки НДС в рублях * * @return float * @see https://nalog-nalog.ru/nds/nalogovaya_baza_nds/kak-schitat-nds-pravilno-vychislyaem-20-ot-summy-primer-algoritm/