From 97f73a73e2657097a4041f3138b77a11b178d7ed Mon Sep 17 00:00:00 2001 From: babaev-an Date: Sun, 25 May 2025 12:45:43 +0300 Subject: [PATCH] =?UTF-8?q?20250525=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D1=81=D1=81=20Tuple,=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D1=83=D1=8E=D1=89=D0=B8=D0=B9=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D1=83=20=D0=BA=D0=BE=D1=80=D1=82=D0=B5=D0=B6=D0=B5=D0=B9?= =?UTF-8?q?=20=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=BA=D0=B0=D0=BA=20=D0=B2?= =?UTF-8?q?=20C#.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 4 +- sources/classes/Tuple.php | 121 ++++++++++++++++++++++++++++++++++++ tests/classes/TupleTest.php | 37 +++++++++++ 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 sources/classes/Tuple.php create mode 100644 tests/classes/TupleTest.php diff --git a/composer.lock b/composer.lock index 43b8373..507a853 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": "62046e22c8307ed8e1f622a0f7bd7a05", + "content-hash": "0c5a35b9c8987ac3fd0f7480fea87d4c", "packages": [], "packages-dev": [ { @@ -1621,4 +1621,4 @@ }, "platform-dev": {}, "plugin-api-version": "2.6.0" -} +} \ No newline at end of file diff --git a/sources/classes/Tuple.php b/sources/classes/Tuple.php new file mode 100644 index 0000000..d72faee --- /dev/null +++ b/sources/classes/Tuple.php @@ -0,0 +1,121 @@ +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); + } +} \ No newline at end of file diff --git a/tests/classes/TupleTest.php b/tests/classes/TupleTest.php new file mode 100644 index 0000000..80df691 --- /dev/null +++ b/tests/classes/TupleTest.php @@ -0,0 +1,37 @@ +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'; + } +} \ No newline at end of file