定制 code-distortion/array-object-extended 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

code-distortion/array-object-extended

Composer 安装命令:

composer require code-distortion/array-object-extended

包简介

ArrayObjectExtended class which adds methods missing from PHP's ArrayObject

README 文档

README

Latest Version on Packagist PHP Version GitHub Workflow Status Buy The World a Tree Contributor Covenant

code-distortion/array-object-extended provides a substitute ArrayObject class that includes the methods missing from PHP's ArrayObject.

Introduction

PHP has many array functions to manipulate arrays. It also has an ArrayObject which is a class that acts like an array.

However, most of the regular array functions aren't implemented by ArrayObject.

This package offers ArrayObjectExtended which extends from PHP's ArrayObject, and adds many of the missing methods in.

ArrayObjectExtended is not intended to:

  • improve the methods or functionality,
  • be complete (it adds methods where it makes sense),
  • be a Collection class, or
  • provide a fluent interface.

It is designed to be faithful to PHP's original method names, parameter usage and return types.

In almost all cases, the only difference between the new class methods and the original is that the $array or $haystack parameter is removed (because the object inherently contains the array, so it doesn't need to be passed).

Installation

Install the package via composer:

composer require code-distortion/array-object-extended

Usage

ArrayObjectExtended acts like PHP's ArrayObject. Instantiate and start using it like normal. e.g.

use CodeDistortion\ArrayObject\ArrayObjectExtended

$myArrayObject = new ArrayObjectExtended(['a', 'b', 'c']);

$myArrayObject->rsort();

foreach ($myArrayObject as $value) {
    print "$value\n";
}

You can extend from ArrayObjectExtended to add in your own functionality.

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
{
    // …
}

Available Methods

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

Notes

ArrayObjects have a key TKey and value TValue type. You can define them in your classes when you extend from ArrayObjectExtendend.

For methods like values() and map(..), a key consideration in having them return plain arrays is those key and value type requirements. If they updated the object, or returned new instances of the class, we can't be sure their keys and values would be valid types.

On-Change Hook

Whenever a change is made to the data in an ArrayObjectExtendend instance, it calls onAfterUpdate(). This gives you an opportunity to clear internal caches, etc.

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();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

SemVer

This library uses SemVer 2.0.0 versioning. This means that changes to X indicate a breaking change: 0.0.X, 0.X.y, X.y.z. When this library changes to version 1.0.0, 2.0.0 and so forth, it doesn't indicate that it's necessarily a notable release, it simply indicates that the changes were breaking.

Treeware

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Contributing

Please see CONTRIBUTING for details.

Code of Conduct

Please see CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email tim@code-distortion.net instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

code-distortion/array-object-extended 适用场景与选型建议

code-distortion/array-object-extended 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 748 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「array」 「serializable」 「arrayobject」 「ArrayAccess」 「countable」 「IteratorAggregate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 code-distortion/array-object-extended 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 code-distortion/array-object-extended 我们能提供哪些服务?
定制开发 / 二次开发

基于 code-distortion/array-object-extended 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 748
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-30