20250525
Добавлен новый класс Tuple, реализующий работу кортежей почти как в C#.
This commit is contained in:
parent
6a4df8373c
commit
97f73a73e2
4
composer.lock
generated
4
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": "62046e22c8307ed8e1f622a0f7bd7a05",
|
"content-hash": "0c5a35b9c8987ac3fd0f7480fea87d4c",
|
||||||
"packages": [],
|
"packages": [],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
@ -1621,4 +1621,4 @@
|
|||||||
},
|
},
|
||||||
"platform-dev": {},
|
"platform-dev": {},
|
||||||
"plugin-api-version": "2.6.0"
|
"plugin-api-version": "2.6.0"
|
||||||
}
|
}
|
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);
|
||||||
|
}
|
||||||
|
}
|
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';
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user