petrobolos/fixed-array-functions

这是我创建的包 fixed-array-functions

v1.2.4 2023-01-30 11:07 UTC

README

Latest Version on Packagist GitHub issues GitHub Tests Action Status GitHub Code Style Action Status Total Downloads GitHub

SplFixedArrays 是 PHP 标准库中传统固定数组的实现。

尽管它们需要手动调整大小,但在处理大量数据集时,它们比常规数组或集合要快得多。

需求

目前,需要 PHP 8 或更高版本,但正在努力回滚 7.4 的支持。此包是为 Laravel 8 和 9 设计的,但也可能与较旧版本或 Lumen 稳定地协同工作。请告诉我们。

安装

您可以通过 Composer 安装此包

composer require petrobolos/fixed-array-functions

流畅接口

FixedArray 最佳搭配其流畅接口使用。如果您熟悉 Illuminate 集合,您会感到非常熟悉。提供 SplFixedArray 以开始,或提供标准数组或集合,它将被自动转换。如果您提供任何其他类型的数据,包括 null,它将被插入到新的 SplFixedArray 中。

use Petrobolos\FixedArray\FixedArrayable;

// You can start by either instantiating a new instance of FixedArrayable, or by calling its helper method:
// The array provided will be converted internally into an SplFixedArray.
$array = new FixedArrayable([1, 2, 3]);

// You can also use the helper function to do the same thing!
$array = fixedArray([1, 2, 3]);

// Lastly, you can use specific methods to begin building your interface logic:
// The same will happen with this collection.
$array = FixedArrayable::fromCollection(collect([1, 2, 3]);

// From here, you can chain different methods, just like you would a collection.
$result = $array
->addFrom([4, 5, 6])
->resize(20)
->filter(fn ($value) => $value % 2 === 0))
->map(fn ($value) => $value * 2))
->get();

// The result will be a SplFixedArray containing [2, 4, 6] but still with 20 indices.

静态方法

您不必强制使用流畅接口,可以直接调用方法。如果您只需对固定数组执行一个或两个操作,这很有用。

use Petrobolos\FixedArray;

// Create a fixed array using the create method.
$arr = FixedArray::create();

// Easily push or pop items to and from arrays without worrying about indices.
FixedArray::push('apple', $arr);

// Easily and efficiently merge fixed arrays, regular arrays, and even Illuminate collections.
$everything = FixedArray::merge(
    $arr,
    ['a', 'regular', 'array'],
    collect(['and', 'an', 'illuminate', 'collection']),
);

方法列表

注意:方法 currentkeynextrewindvalid 是基于指针的数组方法的旧别称操作,当前简单地返回 null。它们将在未来版本中实现。

测试

测试是用 Pest 运行的。您可以这样运行套件

composer test

更新日志

请参阅 更新日志 了解最近的变化。

贡献

我们欢迎拉取请求,特别是那些改进包优化和速度,以及将功能与 Collection 平等的新的功能。

请确保提交的功能有足够的测试覆盖率和文档(至少英文。)

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件