From 793549aaac286fda265f801eefa524ec31c91035 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Wed, 8 Dec 2021 15:55:52 +0800 Subject: [PATCH] =?UTF-8?q?`Entity`=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C?= =?UTF-8?q?=20=D0=B8=D0=BC=D0=BF=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D1=83=D0=B5=D1=82=20`Arrayable`=20=D0=B8=20`ArrayAccess`?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BE=D0=B2=D0=BC=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B8=20=D1=81=20=D0=B8?= =?UTF-8?q?=D0=BC=D0=BC=D1=83=D1=82=D0=B0=D0=B1=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=BC=D0=B8=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Collections/EntityCollection.php | 1 + src/Entities/Entity.php | 55 +++++++++++++++++++++++++++- src/Entities/Vat.php | 5 +-- 3 files changed, 56 insertions(+), 5 deletions(-) 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/