Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
97f73a73e2 | |||
6a4df8373c | |||
10ec8df983 | |||
2d16baaef9 | |||
2e684cb862 | |||
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.6"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
58
composer.lock
generated
58
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": "0c5a35b9c8987ac3fd0f7480fea87d4c",
|
||||||
"packages": [],
|
"packages": [],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
@@ -245,16 +245,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/php-code-coverage",
|
"name": "phpunit/php-code-coverage",
|
||||||
"version": "12.2.1",
|
"version": "12.3.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
|
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
|
||||||
"reference": "448f2c504d86dbff3949dcd02c95aa85db2c7617"
|
"reference": "9075a8efc66e11bc55c319062e147bdb06777267"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/448f2c504d86dbff3949dcd02c95aa85db2c7617",
|
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/9075a8efc66e11bc55c319062e147bdb06777267",
|
||||||
"reference": "448f2c504d86dbff3949dcd02c95aa85db2c7617",
|
"reference": "9075a8efc66e11bc55c319062e147bdb06777267",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -281,7 +281,7 @@
|
|||||||
"type": "library",
|
"type": "library",
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-main": "12.2.x-dev"
|
"dev-main": "12.3.x-dev"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@@ -310,7 +310,7 @@
|
|||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
|
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy",
|
"security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy",
|
||||||
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/12.2.1"
|
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/12.3.0"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -330,7 +330,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-05-04T05:25:05+00:00"
|
"time": "2025-05-23T15:49:03+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/php-file-iterator",
|
"name": "phpunit/php-file-iterator",
|
||||||
@@ -579,16 +579,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/phpunit",
|
"name": "phpunit/phpunit",
|
||||||
"version": "12.1.4",
|
"version": "12.1.6",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
||||||
"reference": "5ee57ad690bda2c487594577600931a99053436c"
|
"reference": "2fdf0056c673c8f0f1eed00030be5f8243c1e6e0"
|
||||||
},
|
},
|
||||||
"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/2fdf0056c673c8f0f1eed00030be5f8243c1e6e0",
|
||||||
"reference": "5ee57ad690bda2c487594577600931a99053436c",
|
"reference": "2fdf0056c673c8f0f1eed00030be5f8243c1e6e0",
|
||||||
"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",
|
||||||
@@ -610,7 +610,7 @@
|
|||||||
"sebastian/cli-parser": "^4.0.0",
|
"sebastian/cli-parser": "^4.0.0",
|
||||||
"sebastian/comparator": "^7.0.1",
|
"sebastian/comparator": "^7.0.1",
|
||||||
"sebastian/diff": "^7.0.0",
|
"sebastian/diff": "^7.0.0",
|
||||||
"sebastian/environment": "^8.0.0",
|
"sebastian/environment": "^8.0.1",
|
||||||
"sebastian/exporter": "^7.0.0",
|
"sebastian/exporter": "^7.0.0",
|
||||||
"sebastian/global-state": "^8.0.0",
|
"sebastian/global-state": "^8.0.0",
|
||||||
"sebastian/object-enumerator": "^7.0.0",
|
"sebastian/object-enumerator": "^7.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.6"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -680,7 +680,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-05-02T07:01:56+00:00"
|
"time": "2025-05-21T12:36:31+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/cli-parser",
|
"name": "sebastian/cli-parser",
|
||||||
@@ -946,16 +946,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/environment",
|
"name": "sebastian/environment",
|
||||||
"version": "8.0.0",
|
"version": "8.0.2",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/environment.git",
|
"url": "https://github.com/sebastianbergmann/environment.git",
|
||||||
"reference": "8afe311eca49171bf95405cc0078be9a3821f9f2"
|
"reference": "d364b9e5d0d3b18a2573351a1786fbf96b7e0792"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/8afe311eca49171bf95405cc0078be9a3821f9f2",
|
"url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/d364b9e5d0d3b18a2573351a1786fbf96b7e0792",
|
||||||
"reference": "8afe311eca49171bf95405cc0078be9a3821f9f2",
|
"reference": "d364b9e5d0d3b18a2573351a1786fbf96b7e0792",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@@ -998,15 +998,27 @@
|
|||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/environment/issues",
|
"issues": "https://github.com/sebastianbergmann/environment/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/environment/security/policy",
|
"security": "https://github.com/sebastianbergmann/environment/security/policy",
|
||||||
"source": "https://github.com/sebastianbergmann/environment/tree/8.0.0"
|
"source": "https://github.com/sebastianbergmann/environment/tree/8.0.2"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
"url": "https://github.com/sebastianbergmann",
|
"url": "https://github.com/sebastianbergmann",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://liberapay.com/sebastianbergmann",
|
||||||
|
"type": "liberapay"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://thanks.dev/u/gh/sebastianbergmann",
|
||||||
|
"type": "thanks_dev"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://tidelift.com/funding/github/packagist/sebastian/environment",
|
||||||
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-02-07T04:56:08+00:00"
|
"time": "2025-05-21T15:05:44+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/exporter",
|
"name": "sebastian/exporter",
|
||||||
@@ -1609,4 +1621,4 @@
|
|||||||
},
|
},
|
||||||
"platform-dev": {},
|
"platform-dev": {},
|
||||||
"plugin-api-version": "2.6.0"
|
"plugin-api-version": "2.6.0"
|
||||||
}
|
}
|
153
sources/classes/Dictionary.php
Normal file
153
sources/classes/Dictionary.php
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
<?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.3
|
||||||
|
* @since 1.0.14
|
||||||
|
*/
|
||||||
|
final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISerializable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array $Container Контейнер.
|
||||||
|
*/
|
||||||
|
private array $Container = [];
|
||||||
|
|
||||||
|
// Реализация наследуемых интерфейсов и классов
|
||||||
|
use ArrayBasicTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Добавление элементов в словарь.
|
||||||
|
*
|
||||||
|
* @param array $dictionary Ассоциативный массив вида ключ => значение, который будет добавлен в словарь.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function AddRange (array $dictionary): void
|
||||||
|
{
|
||||||
|
// Для каждого элемента массива
|
||||||
|
foreach ($dictionary as $key => $value)
|
||||||
|
// - добавляем его в словарь.
|
||||||
|
$this->Add($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Добавление элемента в словарь.
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает все элементы словаря в виде массива.
|
||||||
|
*
|
||||||
|
* @return array Массив, содержащий все элементы словаря.
|
||||||
|
*/
|
||||||
|
public function ToArray (): array
|
||||||
|
{
|
||||||
|
return $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;
|
||||||
@@ -17,7 +17,7 @@ use IteratorAggregate;
|
|||||||
*
|
*
|
||||||
* @author Александр Бабаев
|
* @author Александр Бабаев
|
||||||
* @package php_components_pack
|
* @package php_components_pack
|
||||||
* @version 1.0
|
* @version 1.0.5
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
final class ObjectArray implements ArrayAccess, IteratorAggregate, Countable, ISerializable
|
final class ObjectArray implements ArrayAccess, IteratorAggregate, Countable, ISerializable
|
||||||
@@ -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;
|
||||||
|
121
sources/classes/Tuple.php
Normal file
121
sources/classes/Tuple.php
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace goodboyalex\php_components_pack\classes;
|
||||||
|
|
||||||
|
use ArrayAccess;
|
||||||
|
use ArrayIterator;
|
||||||
|
use Countable;
|
||||||
|
use Exception;
|
||||||
|
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||||
|
use IteratorAggregate;
|
||||||
|
use Traversable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Класс, реализующий функционал кортежей от C#.
|
||||||
|
*
|
||||||
|
* @author Александр Бабаев
|
||||||
|
* @package php_components_pack
|
||||||
|
* @version 1.2
|
||||||
|
* @since 1.0.20
|
||||||
|
*/
|
||||||
|
final class Tuple implements ISerializable, ArrayAccess, IteratorAggregate, Countable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array Массив значений кортежа.
|
||||||
|
*/
|
||||||
|
private array $Values;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Конструктор кортежа.
|
||||||
|
*
|
||||||
|
* @param mixed ...$values Значения кортежа.
|
||||||
|
*/
|
||||||
|
public function __construct (...$values)
|
||||||
|
{
|
||||||
|
$this->Values = $values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function Serialize (): string
|
||||||
|
{
|
||||||
|
return serialize($this->Values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function UnSerialize (string $serialized): void
|
||||||
|
{
|
||||||
|
$this->Values = unserialize($serialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function offsetExists (mixed $offset): bool
|
||||||
|
{
|
||||||
|
return isset($this->Values[$offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function offsetGet (mixed $offset): mixed
|
||||||
|
{
|
||||||
|
// Если ключ не является целым числом
|
||||||
|
if (!is_int($offset))
|
||||||
|
// - возвращаем null
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Возвращаем значение кортежа по индексу
|
||||||
|
return $this->Get($offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает значение кортежа по индексу.
|
||||||
|
*
|
||||||
|
* @param int $index Индекс значения.
|
||||||
|
*
|
||||||
|
* @return mixed Значение кортежа по индексу.
|
||||||
|
*/
|
||||||
|
public function Get (int $index): mixed
|
||||||
|
{
|
||||||
|
return $this->Values[$index] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
* @throws Exception Элементы кортежа не могут быть изменены после создания! / Tuple elements cannot be changed
|
||||||
|
* after creation!
|
||||||
|
*/
|
||||||
|
public function offsetSet (mixed $offset, mixed $value): void
|
||||||
|
{
|
||||||
|
throw new Exception('Элементы кортежа не могут быть изменены после создания! / Tuple elements cannot be changed after creation!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function offsetUnset (mixed $offset): void
|
||||||
|
{
|
||||||
|
unset($this->Values[$offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getIterator (): Traversable
|
||||||
|
{
|
||||||
|
return new ArrayIterator($this->Values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function count (): int
|
||||||
|
{
|
||||||
|
return count($this->Values);
|
||||||
|
}
|
||||||
|
}
|
34
sources/enums/VarNotBoolAction.php
Normal file
34
sources/enums/VarNotBoolAction.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace goodboyalex\php_components_pack\enums;
|
||||||
|
|
||||||
|
use goodboyalex\php_components_pack\traits\EnumExtensionsTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Перечисление типов действий, которые необходимо выполнить, если переменная не является булевым типом.
|
||||||
|
*
|
||||||
|
* @author Александр Бабаев
|
||||||
|
* @package php_components_pack
|
||||||
|
* @version 1.0
|
||||||
|
* @since 1.0.19
|
||||||
|
*/
|
||||||
|
enum VarNotBoolAction: int
|
||||||
|
{
|
||||||
|
// Подключаю расширение для Enum
|
||||||
|
use EnumExtensionsTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Игнорировать это утверждение.
|
||||||
|
*/
|
||||||
|
case Ignore = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Считать это утверждение истинным.
|
||||||
|
*/
|
||||||
|
case ConsiderItTrue = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Считать это утверждение ложным.
|
||||||
|
*/
|
||||||
|
case ConsiderItFalse = 2;
|
||||||
|
}
|
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
namespace goodboyalex\php_components_pack\extensions;
|
namespace goodboyalex\php_components_pack\extensions;
|
||||||
|
|
||||||
use Exception;
|
use goodboyalex\php_components_pack\enums\VarNotBoolAction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Расширение типа "правда/ложь".
|
* Расширение типа "правда/ложь".
|
||||||
*
|
*
|
||||||
* @author Александр Бабаев
|
* @author Александр Бабаев
|
||||||
* @package php_components_pack
|
* @package php_components_pack
|
||||||
* @version 1.0
|
* @version 1.1
|
||||||
* @since 1.0.7
|
* @since 1.0.7
|
||||||
*/
|
*/
|
||||||
final class BoolExtensions
|
final class BoolExtensions
|
||||||
@@ -34,38 +34,55 @@ final class BoolExtensions
|
|||||||
* <code>true</code>.
|
* <code>true</code>.
|
||||||
*
|
*
|
||||||
* @param array $expressions Переменные формата правда/ложь.
|
* @param array $expressions Переменные формата правда/ложь.
|
||||||
|
* @param VarNotBoolAction $ifNotBool Действие, если какой-то аргумент <code>expressions</code> не является булевым
|
||||||
|
* типом. По умолчанию игнорировать.
|
||||||
*
|
*
|
||||||
* @return bool Есть ли хотя бы одно в значении true.
|
* @return bool Есть ли хотя бы одно в значении true.
|
||||||
*
|
*
|
||||||
* @throws Exception Выбрасывается, если хотя бы один аргумент <code>expressions</code> не являются типом
|
|
||||||
* правда/ложь.
|
|
||||||
*/
|
*/
|
||||||
public static function AnyTrue (array $expressions): bool
|
public static function AnyTrue (array $expressions, VarNotBoolAction $ifNotBool = VarNotBoolAction::Ignore): bool
|
||||||
{
|
{
|
||||||
return self::TrueCount($expressions) > 0;
|
return self::TrueCount($expressions, $ifNotBool) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Вычисляет количество переменных формата правда/ложь <code>expressions</code> в значении <code>true</code>.
|
* Вычисляет количество переменных формата правда/ложь <code>expressions</code> в значении <code>true</code>.
|
||||||
*
|
*
|
||||||
* @param array $expressions Переменные формата правда/ложь.
|
* @param array $expressions Переменные формата правда/ложь.
|
||||||
|
* @param VarNotBoolAction $ifNotBool Действие, если какой-то аргумент <code>expressions</code> не является булевым
|
||||||
|
* типом. По умолчанию игнорировать.
|
||||||
*
|
*
|
||||||
* @return int Количество переменных в значении true.
|
* @return int Количество переменных в значении true.
|
||||||
*
|
|
||||||
* @throws Exception Выбрасывается, если хотя бы один аргумент <code>expressions</code> не являются типом
|
|
||||||
* правда/ложь.
|
|
||||||
*/
|
*/
|
||||||
public static function TrueCount (array $expressions): int
|
public static function TrueCount (array $expressions, VarNotBoolAction $ifNotBool = VarNotBoolAction::Ignore): int
|
||||||
{
|
{
|
||||||
// Проверяем все аргументы
|
// Создаем пустой массив для хранения проверяемых аргументов
|
||||||
|
$checkArray = [];
|
||||||
|
|
||||||
|
// Проверяем все входящие аргументы
|
||||||
foreach ($expressions as $expression)
|
foreach ($expressions as $expression)
|
||||||
// - если аргумент не является типом правда/ложь
|
// - если аргумент не является типом правда/ложь
|
||||||
if (!is_bool($expression))
|
if (!is_bool($expression))
|
||||||
// -- то выбрасываем исключение
|
// -- то делаем следующее в зависимости от настроек:
|
||||||
throw new Exception('All arguments must be bool. / Все аргументы должны быть типа «правда/ложь».');
|
switch ($ifNotBool) {
|
||||||
|
case VarNotBoolAction::Ignore:
|
||||||
|
// --- игнорируем аргумент
|
||||||
|
break;
|
||||||
|
case VarNotBoolAction::ConsiderItTrue:
|
||||||
|
// --- считаем аргумент как истинное значение
|
||||||
|
$checkArray[] = true;
|
||||||
|
break;
|
||||||
|
case VarNotBoolAction::ConsiderItFalse:
|
||||||
|
// --- считаем аргумент как ложное значение
|
||||||
|
$checkArray[] = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
// - иначе добавляем аргумент в массив проверяемых аргументов
|
||||||
|
$checkArray[] = $expression;
|
||||||
|
|
||||||
// Используем array_filter для фильтрации всех истинных значений
|
// Используем array_filter для фильтрации всех истинных значений
|
||||||
$filtered = array_filter($expressions);
|
$filtered = array_filter($checkArray, fn ($value) => $value === true);
|
||||||
|
|
||||||
// Возвращаем количество элементов в отфильтрованном массиве
|
// Возвращаем количество элементов в отфильтрованном массиве
|
||||||
return count($filtered);
|
return count($filtered);
|
||||||
|
@@ -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,6 +77,10 @@ trait ObjectArrayBasicTrait
|
|||||||
*/
|
*/
|
||||||
public function Serialize (): string
|
public function Serialize (): string
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* ВНИМАНИЕ! Не используйте json_encode для сериализации объектов данного класса, так как он НЕ СОХРАНЯЕТ классы объектов!
|
||||||
|
* Корректное восстановление объектов невозможно (восстанавливает только как ассоциативный массив).
|
||||||
|
*/
|
||||||
return serialize($this->Container);
|
return serialize($this->Container);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +89,10 @@ trait ObjectArrayBasicTrait
|
|||||||
*/
|
*/
|
||||||
public function UnSerialize (string $serialized): void
|
public function UnSerialize (string $serialized): void
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* ВНИМАНИЕ! Не используйте json_decode для десериализации объектов данного класса, так как он НЕ ВОССТАНОВЛЯЕТ
|
||||||
|
* классы объектов! Корректное восстановление объектов невозможно (восстанавливается только как ассоциативный массив).
|
||||||
|
*/
|
||||||
$this->Container = unserialize($serialized);
|
$this->Container = unserialize($serialized);
|
||||||
}
|
}
|
||||||
}
|
}
|
154
tests/classes/DictionaryTest.php
Normal file
154
tests/classes/DictionaryTest.php
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
<?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 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(), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddRange ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$dict = new Dictionary();
|
||||||
|
$dict->Add('1', 1);
|
||||||
|
|
||||||
|
$array = ['2' => '2', '3' => true, '4' => false];
|
||||||
|
|
||||||
|
$dict->AddRange($array);
|
||||||
|
|
||||||
|
$this->assertEquals(1, $dict->Get('1'));
|
||||||
|
$this->assertTrue($dict->Get('3'));
|
||||||
|
$this->assertEquals(4, $dict->count());
|
||||||
|
$this->assertFalse($dict->Get("4"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToArray ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$dict = new Dictionary();
|
||||||
|
$dict->Add('1', 1);
|
||||||
|
$dict->Add('3', true);
|
||||||
|
$dict->Add('2', '2');
|
||||||
|
|
||||||
|
$array = $dict->ToArray();
|
||||||
|
|
||||||
|
$this->assertIsArray($array);
|
||||||
|
$this->assertEquals(1, $array['1']);
|
||||||
|
$this->assertTrue($array['3']);
|
||||||
|
$this->assertCount(3, $array);
|
||||||
|
}
|
||||||
|
}
|
@@ -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';
|
||||||
|
37
tests/classes/TupleTest.php
Normal file
37
tests/classes/TupleTest.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace goodboyalex\php_components_pack\tests\classes;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use goodboyalex\php_components_pack\classes\Tuple;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class TupleTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testTuple ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$tuple = new Tuple(1, 'string', ['array1', 'array2', 'array3']);
|
||||||
|
|
||||||
|
// Проверка на то, что возвращает значение кортежа при запросе значения по индексу
|
||||||
|
$this->assertEquals(1, $tuple->Get(0));
|
||||||
|
$this->assertEquals('string', $tuple->Get(1));
|
||||||
|
$this->assertEquals(['array1', 'array2', 'array3'], $tuple->Get(2));
|
||||||
|
|
||||||
|
// Проверка на то, что возвращает переменные
|
||||||
|
[$firstElement, $secondElement, $thirdElement] = $tuple;
|
||||||
|
$this->assertEquals(1, $firstElement);
|
||||||
|
$this->assertEquals('string', $secondElement);
|
||||||
|
$this->assertEquals(['array1', 'array2', 'array3'], $thirdElement);
|
||||||
|
|
||||||
|
// Проверка на то, что выбрасывает исключение при попытке задать данные в кортеж после его создания
|
||||||
|
$this->expectException(Exception::class);
|
||||||
|
$tuple[] = 'New data';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function PrepareForTest (): void
|
||||||
|
{
|
||||||
|
require_once __DIR__ . '/../../sources/classes/Tuple.php';
|
||||||
|
}
|
||||||
|
}
|
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace goodboyalex\php_components_pack\tests\extensions;
|
namespace goodboyalex\php_components_pack\tests\extensions;
|
||||||
|
|
||||||
use Exception;
|
use goodboyalex\php_components_pack\enums\VarNotBoolAction;
|
||||||
use goodboyalex\php_components_pack\extensions\BoolExtensions;
|
use goodboyalex\php_components_pack\extensions\BoolExtensions;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@@ -30,6 +30,7 @@ class BoolExtensionsTest extends TestCase
|
|||||||
|
|
||||||
private function PrepareForTest (): void
|
private function PrepareForTest (): void
|
||||||
{
|
{
|
||||||
|
require_once __DIR__ . '/../../sources/enums/VarNotBoolAction.php';
|
||||||
require_once __DIR__ . '/../../sources/extensions/BoolExtensions.php';
|
require_once __DIR__ . '/../../sources/extensions/BoolExtensions.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,23 +47,19 @@ class BoolExtensionsTest extends TestCase
|
|||||||
$this->assertEquals(14,
|
$this->assertEquals(14,
|
||||||
BoolExtensions::TrueCount($array));
|
BoolExtensions::TrueCount($array));
|
||||||
|
|
||||||
$this->expectException(Exception::class);
|
|
||||||
|
|
||||||
$array[] = "ПРЕДАТЕЛЬ!";
|
$array[] = "ПРЕДАТЕЛЬ!";
|
||||||
|
|
||||||
BoolExtensions::TrueCount($array);
|
$this->assertEquals(14, BoolExtensions::TrueCount($array));
|
||||||
|
$this->assertEquals(14, BoolExtensions::TrueCount($array, VarNotBoolAction::ConsiderItFalse));
|
||||||
|
$this->assertEquals(15, BoolExtensions::TrueCount($array, VarNotBoolAction::ConsiderItTrue));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExportToString ()
|
public function testExportToString ()
|
||||||
{
|
{
|
||||||
$this->PrepareForTest();
|
$this->PrepareForTest();
|
||||||
|
|
||||||
$b = true;
|
$this->assertEquals('О, да!', BoolExtensions::ExportToString(true, 'О, да!', 'О, нет!'));
|
||||||
|
|
||||||
$this->assertEquals('О, да!', BoolExtensions::ExportToString($b, 'О, да!', 'О, нет!'));
|
$this->assertEquals('О, нет!', BoolExtensions::ExportToString(false, 'О, да!', 'О, нет!'));
|
||||||
|
|
||||||
$b = false;
|
|
||||||
|
|
||||||
$this->assertEquals('О, нет!', BoolExtensions::ExportToString($b, 'О, да!', 'О, нет!'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user