diff --git a/composer.json b/composer.json index 44c5e50..906ff55 100644 --- a/composer.json +++ b/composer.json @@ -15,18 +15,12 @@ "email": "contact_with_us@babaev-an.ru" } ], - "repositories": [ - { - "type": "vcs", - "url": "https://git.babaev-an.ru/babaev-an/php_components_pack.git" - } - ], "require": { "php": "^8.4", "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^12.1.4" + "phpunit/phpunit": "^12.1.5" }, "autoload": { "psr-4": { diff --git a/sources/classes/Dictionary.php b/sources/classes/Dictionary.php new file mode 100644 index 0000000..c12e25d --- /dev/null +++ b/sources/classes/Dictionary.php @@ -0,0 +1,100 @@ + значение любого типа (значение). + * + * @author Александр Бабаев + * @package php_components_pack + * @version 1.0 + * @since 1.0.14 + */ +final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISerializable +{ + /** + * @var array $Container Контейнер. + */ + private array $Container = []; + + // Реализация наследуемых интерфейсов и классов + use ArrayBasicTrait; + + /** + * Добавление элемента в словарь. + * + * @param string $key Ключ. + * @param mixed $value Хранимое значение. + * + * @return void + */ + public function Add (string $key, mixed $value): void + { + $this->Container[$key] = $value; + } + + /** + * Получение значения по ключу. + * + * @param string $key Ключ. + * @param mixed $default Значение по умолчанию, которое будет возвращено, если элемент с указанным ключом + * отсутствует. + * + * @return mixed Если элемент существует, то возвращается его значение, иначе default. + */ + public function Get (string $key, mixed $default = null): mixed + { + return $this->Container[$key] ?? $default; + } + + /** + * Удаление элемента по ключу. + * + * @param string $key Ключ. + * + * @return void + */ + public function Remove (string $key): void + { + // Если элемент с указанным ключом существует + if ($this->Has($key)) + // - то удаляем его. + unset($this->Container[$key]); + } + + /** + * Проверка наличия элемента с указанным ключом. + * + * @param string $key Ключ. + * + * @return bool Есть ли элемент с указанным ключом. + */ + public function Has (string $key): bool + { + return isset($this->Container[$key]); + } + + /** + * Очистка всех элементов. + * + * @return void + */ + public function Clear (): void + { + $this->Container = []; + } + + /** + * @inheritDoc + */ + public function count (): int + { + return count($this->Container); + } +} \ No newline at end of file diff --git a/sources/classes/ObjectArray.php b/sources/classes/ObjectArray.php index ea412bd..0476e0b 100644 --- a/sources/classes/ObjectArray.php +++ b/sources/classes/ObjectArray.php @@ -5,7 +5,7 @@ namespace goodboyalex\php_components_pack\classes; use ArrayAccess; use Countable; use goodboyalex\php_components_pack\interfaces\ISerializable; -use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayBasicTrait; +use goodboyalex\php_components_pack\traits\ArrayBasicTrait; use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayConstantsTrait; use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayLINQTrait; use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArraySearchAndSortTrait; @@ -28,7 +28,7 @@ final class ObjectArray implements ArrayAccess, IteratorAggregate, Countable, IS private array $Container; // Реализация наследуемых интерфейсов и классов - use ObjectArrayBasicTrait; + use ArrayBasicTrait; // Константы use ObjectArrayConstantsTrait; diff --git a/sources/traits/ObjectArray/ObjectArrayBasicTrait.php b/sources/traits/ArrayBasicTrait.php similarity index 81% rename from sources/traits/ObjectArray/ObjectArrayBasicTrait.php rename to sources/traits/ArrayBasicTrait.php index 3e3683f..f97e26b 100644 --- a/sources/traits/ObjectArray/ObjectArrayBasicTrait.php +++ b/sources/traits/ArrayBasicTrait.php @@ -1,18 +1,18 @@ data[$offset]); + return isset($this->Container[$offset]); } /** @@ -77,7 +77,7 @@ trait ObjectArrayBasicTrait */ public function Serialize (): string { - return serialize($this->Container); + return json_encode($this->Container); } /** @@ -85,6 +85,6 @@ trait ObjectArrayBasicTrait */ public function UnSerialize (string $serialized): void { - $this->Container = unserialize($serialized); + $this->Container = json_decode($serialized, true); } } \ No newline at end of file diff --git a/tests/classes/DictionaryTest.php b/tests/classes/DictionaryTest.php new file mode 100644 index 0000000..f275249 --- /dev/null +++ b/tests/classes/DictionaryTest.php @@ -0,0 +1,102 @@ +PrepareForTest(); + + $dict = new Dictionary(); + $dict->Add('1', 1); + $dict->Add('2', '2'); + $dict->Add('3', true); + + $dict->Remove('2'); + + $this->assertEquals(1, $dict->Get('1')); + $this->assertTrue($dict->Get('3')); + $this->assertEquals(2, $dict->count()); + $this->assertFalse($dict->Has('2')); + } + + private function PrepareForTest (): void + { + require_once __DIR__ . '/../../sources/interfaces/ISerializable.php'; + require_once __DIR__ . '/../../sources/traits/ArrayBasicTrait.php'; + require_once __DIR__ . '/../../sources/classes/Dictionary.php'; + } + + public function testSerialize () + { + $this->PrepareForTest(); + + $dict = new Dictionary(); + $dict->Add('1', 1); + $dict->Add('2', '2'); + $dict->Add('3', true); + + $this->assertEquals("{\"1\":1,\"2\":\"2\",\"3\":true}", $dict->Serialize()); + } + + public function testGet () + { + $this->PrepareForTest(); + + $dict = new Dictionary(); + $dict->Add('1', 1); + $dict->Add('2', '2'); + $dict->Add('3', true); + + $this->assertEquals(1, $dict->Get('1')); + $this->assertEquals('2', $dict->Get('2')); + $this->assertTrue($dict->Get('3')); + $this->assertEquals(3, $dict->count()); + } + + public function testClear () + { + + $this->PrepareForTest(); + + $dict = new Dictionary(); + $dict->Add('1', 1); + $dict->Add('2', '2'); + $dict->Add('3', true); + $dict->Clear(); + + $this->assertEquals(0, $dict->count()); + } + + public function testAdd () + { + $this->PrepareForTest(); + + $dict = new Dictionary(); + $dict->Add('1', 1); + $dict->Add('2', '2'); + $dict->Add('3', true); + + $this->assertEquals(1, $dict->Get('1')); + $this->assertEquals('2', $dict->Get('2')); + $this->assertTrue($dict->Get('3')); + $this->assertEquals(3, $dict->count()); + } + + public function testHas () + { + $this->PrepareForTest(); + + $dict = new Dictionary(); + $dict->Add('1', 1); + $dict->Add('2', '2'); + $dict->Add('3', true); + + $this->assertTrue($dict->Has('3')); + $this->assertFalse($dict->Has('4')); + } +} \ No newline at end of file diff --git a/tests/classes/ObjectArrayTest.php b/tests/classes/ObjectArrayTest.php index 6c4a176..4f8af0e 100644 --- a/tests/classes/ObjectArrayTest.php +++ b/tests/classes/ObjectArrayTest.php @@ -39,7 +39,7 @@ class ObjectArrayTest extends TestCase require_once __DIR__ . '/../data/A.php'; require_once __DIR__ . '/../data/B.php'; require_once __DIR__ . '/../../sources/interfaces/ISerializable.php'; - require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayBasicTrait.php'; + require_once __DIR__ . '/../../sources/traits/ArrayBasicTrait.php'; require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayConstantsTrait.php'; require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayLINQTrait.php'; require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArraySearchAndSortTrait.php';