code-distortion / array-object-extended
ArrayObjectExtended 类,它为 PHP 的 ArrayObject 添加了缺失的方法
Requires
- php: 8.2.* | 8.3.*
Requires (Dev)
- infection/infection: ^0.10 | ^0.11 | ^0.12 | ^0.13 | ^0.14 | ^0.15 | ^0.16 | ^0.17 | ^0.18 | ^0.19 | ^0.20 | ^0.21 | ^0.22 | ^0.23 | ^0.24 | ^0.25 | ^0.26 | ^0.27
- phpstan/phpstan: ^0.9 | ^0.10 | ^0.11 | ^0.12 | ^1.0
- phpunit/phpunit: ~4.8 | ^5.0 | ^6.0 | ^7.0 | ^8.4 | ^9.0 | ^10.0
- squizlabs/php_codesniffer: ^3.8.0
README
code-distortion/array-object-extended 提供了一个替代的 ArrayObject 类,该类包含 PHP ArrayObject 缺失的方法。
简介
PHP 有许多用于操作数组的 数组函数。它还有一个 ArrayObject,这是一个类似数组的类。
然而,大多数常规数组函数都没有在 ArrayObject
中实现。
此包提供了 ArrayObjectExtended
,它从 PHP 的 ArrayObject
扩展,并添加了许多缺失的方法。
ArrayObjectExtended
并不是
- 改进 方法或功能,
- 完整(它在合理的地方添加方法),
- 是一个集合类,或者
- 提供流畅的接口。
它 是 设计来忠实于 PHP 的原始方法名、参数使用和返回类型。
在几乎所有情况下,新类方法和原始方法之间的唯一区别是移除了 $array
或 $haystack
参数(因为对象本质上包含数组,所以不需要传递)。
安装
使用 composer 安装此包
composer require code-distortion/array-object-extended
用法
ArrayObjectExtended
与 PHP 的 ArrayObject
类似。实例化后,像正常一样使用它。例如。
use CodeDistortion\ArrayObject\ArrayObjectExtended $myArrayObject = new ArrayObjectExtended(['a', 'b', 'c']); $myArrayObject->rsort(); foreach ($myArrayObject as $value) { print "$value\n"; }
您可以从 ArrayObjectExtended
扩展来添加您自己的功能。
use ArrayAccess; use CodeDistortion\ArrayObject\ArrayObjectExtended; use Countable; use IteratorAggregate; use Serializable; /** * @template TKey of integer|string * @template TValue of string * @template-implements IteratorAggregate<TKey, TValue> * @template-implements ArrayAccess<TKey, TValue> */ class MyArrayObject extends ArrayObjectExtended implements IteratorAggregate, ArrayAccess, Serializable, Countable { // … }
可用方法
append(mixed $value): void aRSort(int $flags = SORT_REGULAR): bool aSort(int $flags = SORT_REGULAR): bool changeKeyCase(int $case = CASE_LOWER): bool chunk(int $length, bool $preserveKeys = false): array column(string|int|null $columnKey, string|int|null $indexKey = null): array // array_combine() - not planning to implement // compact() - not planning to implement contains(mixed $needle, bool $strict = false): bool // alias for inArray() count(): int // array_count_values() - not implemented current(): mixed|false // array_diff() - not implemented // array_diff_assoc() - not implemented // array_diff_key() - not implemented // array_diff_uassoc() - not implemented // array_diff_ukey() - not implemented // each() - not planning to implement end(): mixed|false exchangeArray(object|array $array): array // extract() - not planning to implement // array_fill() - not planning to implement // array_fill_keys() - not planning to implement filter(?callable $callback = null, int $mode = 0): array flip(): array getArrayCopy(): array getFlags(): int getIterator(): Iterator getIteratorClass(): string inArray(mixed $needle, bool $strict = false): bool // array_intersect() - not implemented // array_intersect_assoc() - not implemented // array_intersect_key() - not implemented // array_intersect_uassoc() - not implemented // array_intersect_ukey() - not implemented isList(): bool key(): int|string|null keyExists(mixed $key): bool keyFirst(): string|int|null keyLast(): string|int|null keys(mixed $filterValue = null, bool $strict = false): array kRSort(int $flags = SORT_REGULAR): bool kSort(int $flags = SORT_REGULAR): bool // list() - not planning to implement map(?callable $callback): array max(): mixed // array_merge() - not implemented // array_merge_recursive() - not implemented min(): mixed // array_multisort() - not implemented natCaseSort(): bool natSort(): bool next(): mixed|false offsetExists(mixed $key): bool offsetGet(mixed $key): mixed offsetSet(mixed $key, mixed $value): void offsetUnset(mixed $key): void // array_pad() - not implemented pop(): mixed|null // pos() - not planning to implement prev(): mixed|false // array_product() - not implemented push(mixed ...$values): int rand(int $num = 1): array|string|int // range() - not planning to implement // array_reduce() - not implemented // array_replace() - not implemented // array_replace_recursive() - not implemented reset(): mixed|false reverse(bool $preserveKeys = false): void rNatCaseSort(): bool rNatSort(): bool rSort(int $flags = SORT_REGULAR): bool search(mixed $needle, bool $strict = false): string|int|false serialize(): string setFlags(int $flags): void setIteratorClass(string $iteratorClass): void shift(): mixed shuffle(): bool // sizeof() - not planning to implement slice(int $offset, ?int $length = null, bool $preserveKeys = false): array sort(int $flags = SORT_REGULAR): bool // array_splice() - not implemented // array_sum() - not implemented uASort(callable $callback): bool // array_udiff() - not implemented // array_udiff_assoc() - not implemented // array_udiff_uassoc() - not implemented // array_uintersect() - not implemented // array_uintersect_assoc() - not implemented // array_uintersect_uassoc() - not implemented uKSort(callable $callback): bool unique(int $flags = SORT_STRING): array unserialize(string $data): void unshift(mixed ...$values): int uSort(callable $callback): bool values(): array // array_walk() - not implemented // array_walk_recursive() - not implemented
注意
ArrayObjects 有一个键 TKey
和值 TValue
类型。您可以在扩展 ArrayObjectExtendend
时在您的类中定义它们。
对于像 values()
和 map(..)
这样的方法,一个重要的考虑因素是让它们返回普通数组的关键和值类型要求。如果它们更新了对象,或者返回了类的新的实例,我们无法确定它们的键和值是否为有效类型。
变更钩子
每次对 ArrayObjectExtendend
实例中的数据进行更改时,它都会调用 onAfterUpdate()
。这为您提供了一个清除内部缓存等的机会。
use ArrayAccess; use CodeDistortion\ArrayObject\ArrayObjectExtended; use Countable; use IteratorAggregate; use Serializable; /** * @template TKey of integer|string * @template TValue of string * @template-implements IteratorAggregate<TKey, TValue> * @template-implements ArrayAccess<TKey, TValue> */ class MyArrayObject extends ArrayObjectExtended implements IteratorAggregate, ArrayAccess, Serializable, Countable { /** * A hook that's called when the contents of this object has changed. * * @return void */ protected function onAfterUpdate(): void { // clear internal caches, etc. } } $myArrayObject = new MyArrayObject(); // examples of methods that trigger onAfterUpdate() $myArrayObject->unshift('hi'); $myArrayObject->push('there'); $myArrayObject->rSort();
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
SemVer
此库使用 SemVer 2.0.0 版本控制。这意味着对 X
的更改表示破坏性更改:0.0.X
、0.X.y
、X.y.z
。当此库变为 1.0.0、2.0.0 等版本时,这并不一定表示它是重要的发布,它只是表示更改是破坏性的。
实物
此包是 Treeware。如果您在生产中使用它,那么我们要求您 为世界买一棵树 以感谢我们的工作。通过向 Treeware 森林捐款,您将为当地家庭创造就业机会并恢复野生动物栖息地。
贡献
有关详细信息,请参阅 CONTRIBUTING。
行为准则
有关详细信息,请参阅 CODE_OF_CONDUCT。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 tim@code-distortion.net 联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。