transprime-research / arrayed
以面向对象的方式包装 PHP 数组,以一致的方式处理 PHP 数组。
Requires
- php: >=7.4
- transprime-research/piper: ^2.0
Requires (Dev)
- ext-json: *
- illuminate/support: >=5.5
- phpunit/phpunit: >=8.0
- squizlabs/php_codesniffer: 3.*
- symfony/var-dumper: ^6.4
This package is auto-updated.
Last update: 2024-09-30 18:24:21 UTC
README
关于 Arrayed
以面向对象的方式简单包装 PHP 数组,以一致的方式封装 PHP 数组。
没有高级功能,只是封装 PHP 数组_* 函数和一点点更多。做到像专业人士一样 🆗
寻找增强版的 PHP 数组?请参阅:https://laravel.net.cn/docs/collections
快速使用
arrayed(1, 2, 'ninja') ->filter(fn($val) => is_int($val)) // [1,2] ->map(fn($val) => $val + 1) // [2, 3] ->flip() // [0, 1] ->values() // [0, 1] ->sum(); // 1
而不是
$result = array_filter([1, 2, 'ninja'], fn($val) => is_int($val)); $result = array_map(fn($val) => $val + 1, $result); $result = array_flip($result); $result = array_values($result); $result = array_sum($result);
PS:您仍然可以使用旧的
function() { return v; }
,fn()
是 PHP 7.4+ 中的新短箭头函数。请参阅:https://php.ac.cn/manual/en/functions.arrow.php
安装
composer require transprime-research/arrayed
要求
最低要求
-
PHP 7.2+
-
Composer
-
使用
collect()
方法,需要illuminate\support
>= 5.5
此外,在 Laravel 应用中,如果
arrayed.php
的配置文件没有自动添加,请在安装后运行php artisan vendor:publish --tag=arrayed
。
其他用法
Arrayed 可以通过三种方式实例化
use Transprime\Arrayed\Arrayed; // Nifty arrayed(1, 2)->count(); // Easier Arrayed::on(1, 2)->count(); // Normal with (new instance) (new Arrayed(1,2))->count();
初始值可以通过两种方式传入
//Non associative arrayed(1, 2); //OR arrayed([1, 2]); // For associative array, only this way arrayed(['a' => 1, 'b' => 2]);
与 Laravel 及 Laravel Collection
Laravel Collections
新功能:collect()
方法 🎉
arrayed(1,2)->collect(); // instance of Illuminate/Support/Collection arrayed(1,2)->collect(3, 4); //merged with first one to give [1, 2, 3, 4]
将来,可以通过编辑
config/arrayed.php
的 collection_class 值来更改默认的 Collection 类
其他
collect(arrayed(1, 2, 3, 4)); // Or new Collection(arrayed(1, 2, 3, 4)); // Or Collection::make(arrayed(1, 2, 3, 4));
Laravel 响应接受 Arrayed
response()->json(arrayed(1, 2, 3)->flip());
特殊方法
新功能 🎉 tap()
方法允许对最后一个结果的 Arrayed
实例执行其他操作,而不会修改最后一个 Arrayed
结果
arrayed(1, 2, 3) ->tap(function ($arrd) { logger('Array has '.$arrd->count()); });
其他
如果任何操作通常返回一个数组,则返回值将给出 Arrayed
实例,以便可以在它们上链式调用其他方法,否则将返回一个非数组值,如以下示例中的 sum()
返回一个整数
示例
arrayed(['a' => 1, 'b' => 2]) ->values() // returns array, we can chain ->sum(); // returns an integer, we cannot chain
您可以通过将闭包/可调用函数传递给 result()
方法来处理结果(如果它是一个 'arrayed' 值)
arrayed(['a' => 'name', 'b' => 'age']) ->values() ->result(fn($val) => implode(',', $val)); //'name,age' //Or arrayed(['a' => 'name', 'b' => 'age']) ->values()(fn($val) => implode(',', $val)); //'name,age'
使用 raw()
方法获取原始数组数据
arrayed([1, 2])->raw(); //[1,2]
管道调用
目前尚未实现所有 array_*
函数。 pipe()
方法有助于在数组结果上调用自定义函数。
例如,使用这种方式使用 array_unique
arrayed(['a' => 'www', 'b' => 'dot', 'c' => 'www']) ->pipe('array_unique') // data is piped forward to `array_unique` ->flip() ->values()(); //['a', 'b']
管道方法使用 Piper - 一个面向对象的 PHP 函数式管道。请参阅
\Transprime\Arrayed\Tests\ArrayedTest
代理调用
尚未实现的 array_*
方法将自动代理为调用一个假设接受初始数组作为第一个参数的数组方法。例如,这是这样
// ->combine() method is not yet implemented arrayed(['a', 'b']) ->combine(['name', 'data']) ->result(); //['a' => 'name', 'b' => 'data']
即将推出
-
实现其他
array_*
方法 -
使用
collectPipe
将管道输入 Collection
use Illuminate\Support\Collection; arrayed(1,2,3)->collectPipe(function (Collection $collected) { return $collected->take(2)->all(); })->keys();
API 实现待定
API
以下是可用的 API
static Arrayed::on(...$values): ArrayedInterface; //new instance of Arrayed Arrayed::map($callback): ArrayedInterface; Arrayed::filter($callback = null, int $flag = 0): ArrayedInterface; Arrayed::reduce($function, $initial = null): ArrayedInterface; Arrayed::merge(array $array2 = null, ...$_): ArrayedInterface; Arrayed::mergeRecursive(...$_): ArrayedInterface; Arrayed::flip(): ArrayedInterface; Arrayed::intersect(array $array2, ...$_): ArrayedInterface; Arrayed::values(): ArrayedInterface; Arrayed::keys($overwrite = true): ArrayedInterface; Arrayed::offsetGet($offset); Arrayed::offsetSet($offset, $value): ArrayedInterface; Arrayed::offsetUnset($offset): ArrayedInterface; Arrayed::sum(): int; Arrayed::contains($needle, bool $strict = false): bool; Arrayed::isArray(): bool; Arrayed::keyExists($key): bool; Arrayed::offsetExists($offset): bool; Arrayed::empty(): bool; Arrayed::count(): int; Arrayed::pipe(callable $action, ...$parameters); Arrayed::result(callable $callable = null); Arrayed::raw(): array; Arrayed::initial(): array; // Deprecated, use raw() instead Arrayed::tap(Closure $closure): ArrayedInterface; Arrayed::copy(): ArrayedInterface; Arrayed::collect(...$with): array; // Other Array_* methods Arrayed::changeKeyCase(int $case = null): ArrayedInterface; Arrayed::chunk(int $size, bool $preserve_keys = false): ArrayedInterface; Arrayed::column($column, $index_key = null): ArrayedInterface; Arrayed::countValues(): ArrayedInterface; Arrayed::diffAssoc(array $array2, array ...$_): ArrayedInterface; Arrayed::diff(array $array2, array ...$_): ArrayedInterface; Arrayed::reverse(bool $preserve_keys = false): ArrayedInterface; Arrayed::diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface; Arrayed::diffKey(array $array2, array ...$_): ArrayedInterface;
附加信息
此软件包是“代码挑战”系列的一部分
在此处查看该系列中的其他软件包
- https://github.com/transprime-research/piper [一个面向对象的 PHP 函数式管道]
- https://github.com/omitobi/conditional [智能PHP if...elseif...else语句]
- https://github.com/transprime-research/attempt [智能PHP try...catch语句]
- https://github.com/omitobi/corbonate [智能Carbon + Collection包]
- https://github.com/omitobi/laravel-habitue [支持集合响应的Jsonable Http Request(er)包]
类似包
- https://github.com/cocur/chain - 非常相似,但没有Piper
- https://github.com/bocharsky-bw/Arrayzy - 相似但功能更多
- https://github.com/voku/Arrayy - 对数组进行操作不仅限于OOP风格
- https://github.com/dantodev/php-array-tools - 数组工具加上集合
- https://github.com/minwork/array - 高级PHP数组函数包
- https://github.com/mblarsen/arrgh - 具有高级函数的理智PHP数组库
- 更多信息: https://www.google.com/search?q=php+array+github
许可证
MIT (见LICENCE文件)