transprime-research/arrayed
Composer 安装命令:
composer require transprime-research/arrayed
包简介
PHP Array(ed) in object oriented way wrapping PHP arrays in a consistent manner.
README 文档
README
About Arrayed
Simple PHP Array(ed) in object oriented way wrapping PHP Arrays in a consistent manner.
No advanced stuff, just wrap PHP array_* functions and a little more. Do it Like a PRO 🆗
Looking for PHP Array on Steroid? See: https://laravel.com/docs/collections
Quick Usage
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
Instead of:
$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: You can still use the old
function() { return v; },fn()is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php
Installation
composer require transprime-research/arrayed
Requirement
Minimum Requirement
-
PHP 7.2 +
-
Composer
-
For using
collect()method, requiresilluminate\support>= 5.5
Additionally on Laravel App, if
arrayed.php's config file doesn't get added automatically then runphp artisan vendor:publish --tag=arrayedafter installation.
Other Usages
Arrayed can be instantiated in 3 ways:
use Transprime\Arrayed\Arrayed; // Nifty arrayed(1, 2)->count(); // Easier Arrayed::on(1, 2)->count(); // Normal with (new instance) (new Arrayed(1,2))->count();
Initial values can be passed in two ways:
//Non associative arrayed(1, 2); //OR arrayed([1, 2]); // For associative array, only this way arrayed(['a' => 1, 'b' => 2]);
With Laravel & Laravel Collection
Laravel Collections
New: collect() method 🎉
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]
In the future, changing the default Collection class will possible by editing
config/arrayed.php's collection_class value
Others:
collect(arrayed(1, 2, 3, 4)); // Or new Collection(arrayed(1, 2, 3, 4)); // Or Collection::make(arrayed(1, 2, 3, 4));
Laravel Response accepts Arrayed:
response()->json(arrayed(1, 2, 3)->flip());
Special methods
New 🎉 tap() method allows other actions on the last resulting Arrayed instance without mutating the last Arrayed result:
arrayed(1, 2, 3) ->tap(function ($arrd) { logger('Array has '.$arrd->count()); });
Others
If any operation normally returns an array, the return value will give Arrayed instance so that other methods can be chained on them otherwise a non-array value is returned as can be seen that sum() returns an integer in the example below:
Example:
arrayed(['a' => 1, 'b' => 2]) ->values() // returns array, we can chain ->sum(); // returns an integer, we cannot chain
You can work on a result (if its an array'ed value) by passing a closure/callable function to result() method:
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'
Get the original array data with raw() method
arrayed([1, 2])->raw(); //[1,2]
Piped calls
As at now not all array_* functions have been implemented.
pipe() method helps to call custom function on the array result.
Such as array_unique used in this way:
arrayed(['a' => 'www', 'b' => 'dot', 'c' => 'www']) ->pipe('array_unique') // data is piped forward to `array_unique` ->flip() ->values()(); //['a', 'b']
The pipe method makes use of Piper - A PHP functional pipe'ing See
\Transprime\Arrayed\Tests\ArrayedTest
Proxied calls
array_* methods that are not yet implemented are automatically proxied to call an array method with the assumption that they accept initial array first. Example is this:
// ->combine() method is not yet implemented arrayed(['a', 'b']) ->combine(['name', 'data']) ->result(); //['a' => 'name', 'b' => 'data']
Coming Soon
-
Implement other
array_*methods -
pipe into Collection with
collectPipe
use Illuminate\Support\Collection; arrayed(1,2,3)->collectPipe(function (Collection $collected) { return $collected->take(2)->all(); })->keys();
Api implementation to be decided
APIs
These are the API's available:
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;
Additional Information
This package is part of a series of "Code Dare"
See other packages in this series here:
- https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way]
- https://github.com/omitobi/conditional [A smart PHP if...elseif...else statement]
- https://github.com/transprime-research/attempt [A smart PHP try...catch statement]
- https://github.com/omitobi/corbonate [A smart Carbon + Collection package]
- https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response]
Similar packages
- https://github.com/cocur/chain - Very identical, but without Piper
- https://github.com/bocharsky-bw/Arrayzy - Identical but with more actions and features
- https://github.com/voku/Arrayy - Perform more than just an OOP style on Arrays
- https://github.com/dantodev/php-array-tools - array tools plus collections
- https://github.com/minwork/array - Pack of advanced PHP array functions
- https://github.com/mblarsen/arrgh - A Sane PHP Array library with advance functions
- More at: https://www.google.com/search?q=php+array+github
Licence
MIT (See LICENCE file)
transprime-research/arrayed 适用场景与选型建议
transprime-research/arrayed 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 253 次下载、GitHub Stars 达 22, 最近一次更新时间为 2020 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「helpers」 「array」 「laravel」 「arrays」 「arrayed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 transprime-research/arrayed 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 transprime-research/arrayed 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 transprime-research/arrayed 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
Convert array or camel case to underscore, or underscore to others.
HTML and form generation
统计信息
- 总下载量: 253
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-22
