newnakashima / typedarray
PHP 类型数组
0.1.8
2023-08-17 08:25 UTC
Requires (Dev)
- pestphp/pest: ^2.10
README
PHP 类型数组。
TypedArray 对象可以被当作数组处理。但它每个元素都具有相同的类型。
例如,具有字符串类型的 TypedArray 对象不允许开发者向其中添加整数值。
$stringArray = new TypedArray('string', ['foo', 'bar']); // This causes an error. $stringArray[] = 42; // This is OK. $stringArray[] = 'baz';
示例
初始化
$length = 10000; # with initial value. $list = new TypedArray(Primitives::Int->value, range(1, $length)); # without initial value. $empty = new TypedArray('int'); foreach ($list as $item) { $empty->add($item * 2); } assert($empty->count() === $length);
原始类型
整数
$intArray = new TypedArray(Primitives::Int->value, range(1, 100)); # or $intArray = new TypedArray('int', range(1, 100)); # or $intArray = new TypedArray('integer', range(1, 100));
浮点数(double)
$doubleArray = new TypedArray(Primitives::Float->value, [0.1, 1.2]); #or $doubleArray = new TypedArray('float', [0.1, 1.2]); #or $doubleArray = new TypedArray('double', [0.1, 1.2]);
字符串
$stringArray = new TypedArray(Primitives::String->value, ['foo', 'bar']); # or $stringArray = new TypedArray('string', ['foo', 'bar']);
布尔值
$boolArray = new TypedArray(Primitives::Bool->value, [true, false]); # or $boolArray = new TypedArray('bool', [true, false]);
数组
$arrayArray = new TypedArray(Primitives::Array->vale, [ [1, 2, 3], ['foo', 'bar', 'baz'], ]); # or $arrayArray = new TypedArray('array', [ [1, 2, 3], ['foo', 'bar', 'baz'], ]);
对象
$obj1 = new \stdClass(); $obj2 = new \stdClass(); $objectArray = new TypedArray(Primitives::Object->value, [$obj1, $obj2]); # or $objectArray = new TypedArray('object', [$obj1, $obj2]);
其他类或接口
class Foo { public int $value = 0; public function __construct($value) { $this->value = $value; } } $fooArray = new TypedArray(Foo::class, [ new Foo(1), new Foo(2), ]);
如果类只接受一个构造函数参数,您可以省略 new 关键字和类名。
$fooArray = new TypedArray(Foo::class, [1, 2]); assert(get_class($fooArray[0]) == Foo::class);
添加值
$list = new TypedArray('string'); $list[] = 'foo'; # or $list->add('foo');
map
此方法返回一个原始数组。不是 TypedArray 对象。
$mapped = $list->map(fn ($item) => $item * 2); assert($mapped[$length - 1], $length * 2);
mapWithType
此方法返回另一个具有特定类型的 TypedArray。
$list = new TypedArray('string', ['a', 'bb', 'cccc']); $mapped = $list->mapWithType('int', fn ($item) => strlen($item)); assert(get_class($mapped) === TypedArray::class); assert($mapped[0] === 1); assert($mapped[1] === 2); assert($mapped[2] === 4);
mapWithSameType
$list = new TypedArray('string', ['a', 'bb', 'cccc']); $mapped = $list->mapWithSameType(fn ($item) => $item . '!'); assert(get_class($mapped) === TypedArray::class); assert($mapped[0] === 'a!'); assert($mapped[1] === 'bb!'); assert($mapped[2] === 'cccc!');
filter
$filtered = $list->filter(fn ($item) => $item % 2 === 0); assert($filtered->count() === $length / 2);
each
$filtered->each(fn ($item) => assert($item % 2 === 0));
merge
$list = new TypedArray('string', ['foo', 'bar']); $anotherList = new TypedArray('string', ['baz', 'qux']); $list = $list->merge($anotherList); assert($list->count() === 4); assert($list[0] === 'foo'); assert($list[1] === 'bar'); assert($list[2] === 'baz'); assert($list[3] === 'qux');
find
$list = new TypedArray('string', ['foo', 'bar']); $found = $list->find(fn ($item) => $item === 'bar'); assert($found === 'bar');
push
array_push() 函数的包装器。
$list = new TypedArray('string', ['foo', 'bar']); $list->push('baz', 'qux'); assert($list->count() === 4); assert($list[2] === 'baz'); assert($list[3] === 'qux');
pop
$list = new TypedArray('string', ['foo', 'bar', 'baz']); $last = $list->pop(); assert($last === 'baz'); assert($list->count() === 2);
shift
$list = new TypedArray('string', ['foo', 'bar', baz]); $first = $list->shift(); assert($first === 'foo'); assert($list->count() === 2);
unshift
$list = new TypedArray('string', ['foo', 'bar']); $list->unshift('baz', 'qux'); assert($list->count() === 4); assert($list[0] === 'baz');
reverse
$list = new TypedArray('string', ['foo', 'bar', 'baz']); $reversed = $list->reverse(); assert($reversed[0] === 'baz'); assert($reversed[1] === 'bar'); assert($reversed[2] === 'foo');
first
$list = new TypedArray('string', ['foo', 'bar', 'baz']); $first = $list->first(); assert($first === 'foo');
last
$list = new TypedArray('string', ['foo', 'bar', 'baz']); $last = $list->last(); assert($last === 'baz');
toArray
$list = new TypedArray('string', ['foo', 'bar', 'baz']); $primitiveArray = $list->toArray(); assert(is_array($primitiveArray)); var_dump($primitiveArray); /** * array(3) { * [0]=> * string(3) "foo" * [1]=> * string(3) "bar" * [2]=> * string(3) "baz" * } */
__toString
此方法返回用于调试的字符串表示形式。
$list = new TypedArray('string', ['foo', 'bar', 'baz']); $string = (string)$list echo $string; /** * string(51) "array ( * 0 => 'foo', * 1 => 'bar', * 2 => 'baz', * )" */
自动初始化
如果您传递的值不是给定类型的实例,TypedArray 将尝试使用给定值初始化对象。
$exampleArray = new TypedArray(Foo::class, [ new Foo(1), new Foo(2), ]); // This causes an error because the type is not matched. $exampleArray->add(new Bar(3)); // This is OK because the type is matched. $exampleArray->add(new Foo(3)); // This is OK too. TypedArray will try to initialize the object with the given value. // Within add() method, Foo::__construct(3) is called. $exampleArray->add(3); assert($exampleArray[2]->value === 3); assert($exampleArray[2] instanceof Foo);