From 5b83b096e58810db20029c28087162cbc74360ed Mon Sep 17 00:00:00 2001 From: babaev-an Date: Fri, 16 May 2025 23:34:24 +0300 Subject: [PATCH] =?UTF-8?q?20250516=20*=20[Dictionary]=20=D0=94=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=20Keys=20():=20array,=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0?= =?UTF-8?q?=D0=B5=D1=82=20=D0=B2=D1=81=D0=B5=20=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B8=20=D1=81=D0=BB=D0=BE=D0=B2=D0=B0=D1=80=D1=8F.=20*=20[Dic?= =?UTF-8?q?tionary]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20Sort=20(bool=20$descending=20?= =?UTF-8?q?=3D=20false):=20void,=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=D0=B9=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D1=83=D0=B5=D1=82?= =?UTF-8?q?=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B5=D0=BD=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=BF=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B0=D0=BC=20(=D0=B2=20=D0=BE=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BD=D0=BE=D0=BC=20=D0=BF=D0=BE=D1=80=D1=8F=D0=B4?= =?UTF-8?q?=D0=BA=D0=B5,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B2=D1=8B=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=20$descending=20=3D=20true).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 16 ++++++++-------- sources/classes/Dictionary.php | 30 +++++++++++++++++++++++++++++- tests/classes/DictionaryTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/composer.lock b/composer.lock index cd9280b..dec9765 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8024da85d3650f107ba3254f5dfc3b79", + "content-hash": "62046e22c8307ed8e1f622a0f7bd7a05", "packages": [], "packages-dev": [ { @@ -579,16 +579,16 @@ }, { "name": "phpunit/phpunit", - "version": "12.1.4", + "version": "12.1.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "5ee57ad690bda2c487594577600931a99053436c" + "reference": "f93ef2198df8d54b3195bcee381a33be51d8705e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/5ee57ad690bda2c487594577600931a99053436c", - "reference": "5ee57ad690bda2c487594577600931a99053436c", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f93ef2198df8d54b3195bcee381a33be51d8705e", + "reference": "f93ef2198df8d54b3195bcee381a33be51d8705e", "shasum": "" }, "require": { @@ -602,7 +602,7 @@ "phar-io/manifest": "^2.0.4", "phar-io/version": "^3.2.1", "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-invoker": "^6.0.0", "phpunit/php-text-template": "^5.0.0", @@ -656,7 +656,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "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": [ { @@ -680,7 +680,7 @@ "type": "tidelift" } ], - "time": "2025-05-02T07:01:56+00:00" + "time": "2025-05-11T06:44:52+00:00" }, { "name": "sebastian/cli-parser", diff --git a/sources/classes/Dictionary.php b/sources/classes/Dictionary.php index c12e25d..038f8f1 100644 --- a/sources/classes/Dictionary.php +++ b/sources/classes/Dictionary.php @@ -13,7 +13,7 @@ use IteratorAggregate; * * @author Александр Бабаев * @package php_components_pack - * @version 1.0 + * @version 1.0.1 * @since 1.0.14 */ final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISerializable @@ -97,4 +97,32 @@ final class Dictionary implements ArrayAccess, IteratorAggregate, Countable, ISe { 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); + } } \ No newline at end of file diff --git a/tests/classes/DictionaryTest.php b/tests/classes/DictionaryTest.php index f275249..28be388 100644 --- a/tests/classes/DictionaryTest.php +++ b/tests/classes/DictionaryTest.php @@ -99,4 +99,34 @@ class DictionaryTest extends TestCase $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(), []); + } } \ No newline at end of file