Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
f98a277986 | |||
5b83b096e5 | |||
b011d3930c | |||
ee76392d71 |
@@ -15,18 +15,12 @@
|
|||||||
"email": "contact_with_us@babaev-an.ru"
|
"email": "contact_with_us@babaev-an.ru"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"repositories": [
|
|
||||||
{
|
|
||||||
"type": "vcs",
|
|
||||||
"url": "https://git.babaev-an.ru/babaev-an/php_components_pack.git"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.4",
|
"php": "^8.4",
|
||||||
"ext-mbstring": "*"
|
"ext-mbstring": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^12.1.4"
|
"phpunit/phpunit": "^12.1.5"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
16
composer.lock
generated
16
composer.lock
generated
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "8024da85d3650f107ba3254f5dfc3b79",
|
"content-hash": "62046e22c8307ed8e1f622a0f7bd7a05",
|
||||||
"packages": [],
|
"packages": [],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
@@ -579,16 +579,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/phpunit",
|
"name": "phpunit/phpunit",
|
||||||
"version": "12.1.4",
|
"version": "12.1.5",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
||||||
"reference": "5ee57ad690bda2c487594577600931a99053436c"
|
"reference": "f93ef2198df8d54b3195bcee381a33be51d8705e"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/5ee57ad690bda2c487594577600931a99053436c",
|
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f93ef2198df8d54b3195bcee381a33be51d8705e",
|
||||||
"reference": "5ee57ad690bda2c487594577600931a99053436c",
|
"reference": "f93ef2198df8d54b3195bcee381a33be51d8705e",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -602,7 +602,7 @@
|
|||||||
"phar-io/manifest": "^2.0.4",
|
"phar-io/manifest": "^2.0.4",
|
||||||
"phar-io/version": "^3.2.1",
|
"phar-io/version": "^3.2.1",
|
||||||
"php": ">=8.3",
|
"php": ">=8.3",
|
||||||
"phpunit/php-code-coverage": "^12.1.2",
|
"phpunit/php-code-coverage": "^12.2.1",
|
||||||
"phpunit/php-file-iterator": "^6.0.0",
|
"phpunit/php-file-iterator": "^6.0.0",
|
||||||
"phpunit/php-invoker": "^6.0.0",
|
"phpunit/php-invoker": "^6.0.0",
|
||||||
"phpunit/php-text-template": "^5.0.0",
|
"phpunit/php-text-template": "^5.0.0",
|
||||||
@@ -656,7 +656,7 @@
|
|||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
||||||
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.1.4"
|
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.1.5"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -680,7 +680,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-05-02T07:01:56+00:00"
|
"time": "2025-05-11T06:44:52+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/cli-parser",
|
"name": "sebastian/cli-parser",
|
||||||
|
128
sources/classes/Dictionary.php
Normal file
128
sources/classes/Dictionary.php
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
<?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.1
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает все ключи словаря.
|
||||||
|
*
|
||||||
|
* @return array Массив всех ключей словаря.
|
||||||
|
*/
|
||||||
|
public function Keys (): array
|
||||||
|
{
|
||||||
|
return array_keys($this->Container);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Сортирует внутренние данные по ключам.
|
||||||
|
*
|
||||||
|
* @param bool $descending Сортировать ли данные в обратном порядке?
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function Sort (bool $descending = false): void
|
||||||
|
{
|
||||||
|
// Если задана сортировка по убыванию
|
||||||
|
if ($descending)
|
||||||
|
// - сортируем данные в обратном порядке
|
||||||
|
krsort($this->Container);
|
||||||
|
else
|
||||||
|
// - иначе, стандартная сортировка по ключам в порядке возрастания
|
||||||
|
ksort($this->Container);
|
||||||
|
}
|
||||||
|
}
|
@@ -5,7 +5,7 @@ namespace goodboyalex\php_components_pack\classes;
|
|||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
use Countable;
|
use Countable;
|
||||||
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
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\ObjectArrayConstantsTrait;
|
||||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayLINQTrait;
|
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayLINQTrait;
|
||||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArraySearchAndSortTrait;
|
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArraySearchAndSortTrait;
|
||||||
@@ -28,7 +28,7 @@ final class ObjectArray implements ArrayAccess, IteratorAggregate, Countable, IS
|
|||||||
private array $Container;
|
private array $Container;
|
||||||
|
|
||||||
// Реализация наследуемых интерфейсов и классов
|
// Реализация наследуемых интерфейсов и классов
|
||||||
use ObjectArrayBasicTrait;
|
use ArrayBasicTrait;
|
||||||
|
|
||||||
// Константы
|
// Константы
|
||||||
use ObjectArrayConstantsTrait;
|
use ObjectArrayConstantsTrait;
|
||||||
|
@@ -1,18 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace goodboyalex\php_components_pack\traits\ObjectArray;
|
namespace goodboyalex\php_components_pack\traits;
|
||||||
|
|
||||||
use ArrayIterator;
|
use ArrayIterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Часть кода класса ObjectArray, отвечающая за функции из наследуемых интерфейсов и классов.
|
* Часть кода класса ObjectArray и Dictionary, отвечающая за функции из наследуемых интерфейсов и классов.
|
||||||
*
|
*
|
||||||
* @author Александр Бабаев
|
* @author Александр Бабаев
|
||||||
* @package php_components_pack
|
* @package php_components_pack
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
trait ObjectArrayBasicTrait
|
trait ArrayBasicTrait
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
@@ -69,7 +69,7 @@ trait ObjectArrayBasicTrait
|
|||||||
*/
|
*/
|
||||||
public function __isset (mixed $offset): bool
|
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
|
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
|
public function UnSerialize (string $serialized): void
|
||||||
{
|
{
|
||||||
$this->Container = unserialize($serialized);
|
$this->Container = json_decode($serialized, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
132
tests/classes/DictionaryTest.php
Normal file
132
tests/classes/DictionaryTest.php
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
<?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'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testKeys ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$dict = new Dictionary();
|
||||||
|
$dict->Add('1', 1);
|
||||||
|
$dict->Add('3', true);
|
||||||
|
$dict->Add('2', '2');
|
||||||
|
|
||||||
|
$array = ['1', '3', '2'];
|
||||||
|
|
||||||
|
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($array, $dict->Keys(), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSort ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$dict = new Dictionary();
|
||||||
|
$dict->Add('1', 1);
|
||||||
|
$dict->Add('3', true);
|
||||||
|
$dict->Add('2', '2');
|
||||||
|
|
||||||
|
$array = ['1', '2', '3'];
|
||||||
|
|
||||||
|
$dict->Sort();
|
||||||
|
|
||||||
|
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($array, $dict->Keys(), []);
|
||||||
|
}
|
||||||
|
}
|
@@ -39,7 +39,7 @@ class ObjectArrayTest extends TestCase
|
|||||||
require_once __DIR__ . '/../data/A.php';
|
require_once __DIR__ . '/../data/A.php';
|
||||||
require_once __DIR__ . '/../data/B.php';
|
require_once __DIR__ . '/../data/B.php';
|
||||||
require_once __DIR__ . '/../../sources/interfaces/ISerializable.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/ObjectArrayConstantsTrait.php';
|
||||||
require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayLINQTrait.php';
|
require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayLINQTrait.php';
|
||||||
require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArraySearchAndSortTrait.php';
|
require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArraySearchAndSortTrait.php';
|
||||||
|
Reference in New Issue
Block a user