20250724 1.2
[+] Добавлен новый тип GUID. [*] В класс ObjectArray добавлена реализация интерфейсов IHashable, ISortable, IComparable. [*] В класс Dictionary добавлена реализация интерфейсов IArrayable, IHashable, ISortable, IComparable. [-] Класс GUIDExtension и все его методы помечены как устаревшие и скоро будут удалены.
This commit is contained in:
@@ -4,8 +4,13 @@ namespace goodboyalex\php_components_pack\classes;
|
||||
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use goodboyalex\php_components_pack\interfaces\IArrayable;
|
||||
use goodboyalex\php_components_pack\interfaces\IComparable;
|
||||
use goodboyalex\php_components_pack\interfaces\IHashable;
|
||||
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||
use goodboyalex\php_components_pack\interfaces\ISortable;
|
||||
use goodboyalex\php_components_pack\traits\ArrayBasicTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayComparableTrait;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
@@ -16,7 +21,8 @@ use IteratorAggregate;
|
||||
* @version 1.0.3
|
||||
* @since 1.0.14
|
||||
*/
|
||||
final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISerializable
|
||||
final class Dictionary
|
||||
implements ArrayAccess, IteratorAggregate, Countable, ISerializable, IArrayable, IHashable, ISortable, IComparable
|
||||
{
|
||||
/**
|
||||
* @var array $Container Контейнер.
|
||||
@@ -26,33 +32,8 @@ final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISe
|
||||
// Реализация наследуемых интерфейсов и классов
|
||||
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;
|
||||
}
|
||||
// Реализация интерфейса IComparable
|
||||
use ObjectArrayComparableTrait;
|
||||
|
||||
/**
|
||||
* Получение значения по ключу.
|
||||
@@ -95,16 +76,6 @@ final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISe
|
||||
return isset($this->Container[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистка всех элементов.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Clear (): void
|
||||
{
|
||||
$this->Container = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -124,13 +95,77 @@ final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISe
|
||||
}
|
||||
|
||||
/**
|
||||
* Сортирует внутренние данные по ключам.
|
||||
*
|
||||
* @param bool $descending Сортировать ли данные в обратном порядке?
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function ToArray (): array
|
||||
{
|
||||
return $this->Container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function FromArray (array $array): void
|
||||
{
|
||||
// Очищаем словарь
|
||||
$this->Clear();
|
||||
|
||||
// Добавляем элементы в словарь
|
||||
$this->AddRange($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистка всех элементов.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Sort (bool $descending = false): void
|
||||
public function Clear (): void
|
||||
{
|
||||
$this->Container = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавление элементов в словарь.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function Hash (): string
|
||||
{
|
||||
return md5(json_encode($this->Container, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @warning Свойство <code>$property</code> не используется.
|
||||
*/
|
||||
public function Sort (string $property = '', bool $descending = false): void
|
||||
{
|
||||
// Если задана сортировка по убыванию
|
||||
if ($descending)
|
||||
@@ -142,12 +177,12 @@ final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISe
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает все элементы словаря в виде массива.
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return array Массив, содержащий все элементы словаря.
|
||||
* @warning Свойство <code>$propertyFunction</code> не используется.
|
||||
*/
|
||||
public function ToArray (): array
|
||||
public function SortCallback (callable $propertyFunction, bool $descending = false): void
|
||||
{
|
||||
return $this->Container;
|
||||
$this->Sort(descending: $descending);
|
||||
}
|
||||
}
|
@@ -5,9 +5,14 @@ namespace goodboyalex\php_components_pack\classes;
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use goodboyalex\php_components_pack\interfaces\IArrayable;
|
||||
use goodboyalex\php_components_pack\interfaces\IComparable;
|
||||
use goodboyalex\php_components_pack\interfaces\IHashable;
|
||||
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||
use goodboyalex\php_components_pack\interfaces\ISortable;
|
||||
use goodboyalex\php_components_pack\traits\ArrayBasicTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayComparableTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayConstantsTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayHashableTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArrayLINQTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArraySearchAndSortTrait;
|
||||
use goodboyalex\php_components_pack\traits\ObjectArray\ObjectArraySerializeExTrait;
|
||||
@@ -20,10 +25,11 @@ use IteratorAggregate;
|
||||
*
|
||||
* @author Александр Бабаев
|
||||
* @package php_components_pack
|
||||
* @version 1.0.6
|
||||
* @version 1.1
|
||||
* @since 1.0
|
||||
*/
|
||||
final class ObjectArray implements ArrayAccess, IteratorAggregate, Countable, ISerializable, IArrayable
|
||||
final class ObjectArray
|
||||
implements ArrayAccess, IteratorAggregate, Countable, ISerializable, IArrayable, IHashable, ISortable, IComparable
|
||||
{
|
||||
/**
|
||||
* @var array $Container Массив объектов, хранящихся в данном классе.
|
||||
@@ -51,6 +57,12 @@ final class ObjectArray implements ArrayAccess, IteratorAggregate, Countable, IS
|
||||
// Реализация методов интерфейса IArrayable
|
||||
use ObjectArrayToArrayTrait;
|
||||
|
||||
// Реализация методов интерфейса IHashable
|
||||
use ObjectArrayHashableTrait;
|
||||
|
||||
// Реализация методов интерфейса IComparable
|
||||
use ObjectArrayComparableTrait;
|
||||
|
||||
/**
|
||||
* Конструктор класса.
|
||||
*
|
||||
|
Reference in New Issue
Block a user