定制 transprime-research/arrayed 二次开发

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

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

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

Build Status Latest Stable Version Total Downloads Latest Unstable Version Latest Monthly Downloads License

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, requires illuminate\support >= 5.5

Additionally on Laravel App, if arrayed.php's config file doesn't get added automatically then run php artisan vendor:publish --tag=arrayed after 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:

Similar packages

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 22
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-22