20250515
* [ObjectArrayBasicTrait] заменён на [ArrayBasicTrait]. * [ArrayBasicTrait] изменён метод __isset. Теперь он обрабатывается корректно. * [ArrayBasicTrait] методы Serialize и UnSerialize теперь используют json_encode / json_decode * [+Dictionary] Класс, описывающий словарь типа строка (ключ) -> значение любого типа (значение).
This commit is contained in:
parent
2a03797bfe
commit
ee76392d71
@ -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": {
|
||||
|
100
sources/classes/Dictionary.php
Normal file
100
sources/classes/Dictionary.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace goodboyalex\php_components_pack\classes;
|
||||
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||
use goodboyalex\php_components_pack\traits\ArrayBasicTrait;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* Класс, описывающий словарь типа строка (ключ) -> значение любого типа (значение).
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace goodboyalex\php_components_pack\traits\ObjectArray;
|
||||
namespace goodboyalex\php_components_pack\traits;
|
||||
|
||||
use ArrayIterator;
|
||||
|
||||
/**
|
||||
* Часть кода класса ObjectArray, отвечающая за функции из наследуемых интерфейсов и классов.
|
||||
* Часть кода класса ObjectArray и Dictionary, отвечающая за функции из наследуемых интерфейсов и классов.
|
||||
*
|
||||
* @author Александр Бабаев
|
||||
* @package php_components_pack
|
||||
* @version 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
trait ObjectArrayBasicTrait
|
||||
trait ArrayBasicTrait
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
@ -69,7 +69,7 @@ trait ObjectArrayBasicTrait
|
||||
*/
|
||||
public function __isset (mixed $offset): bool
|
||||
{
|
||||
return isset($this->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);
|
||||
}
|
||||
}
|
102
tests/classes/DictionaryTest.php
Normal file
102
tests/classes/DictionaryTest.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace goodboyalex\php_components_pack\tests\classes;
|
||||
|
||||
use goodboyalex\php_components_pack\classes\Dictionary;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DictionaryTest extends TestCase
|
||||
{
|
||||
public function testRemove ()
|
||||
{
|
||||
$this->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'));
|
||||
}
|
||||
}
|
@ -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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user