judahnator/iterable-collection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

judahnator/iterable-collection

Composer 安装命令:

composer require judahnator/iterable-collection

包简介

A fluent interface for working with iterables and generators.

README 文档

README

pipeline status coverage report

The Gist

This library allows you to use a fluent interface to manipulate iterable items.

The goal was to have at minimum feature parity with all the array_* functions, but the library falls a bit short of that goal due to a limitation of being unable to rewind generators. For that reason, some functions have been deliberately excluded.

Of course, if you can think of a good way to implement said functions feel free to submit a pull request.

A few additional functions were added as well that do not exist as native array_* methods.

Usage

Object Creation:

$o = new IterableCollection(['foo' => 'bar']);
// or
$o = IterableCollection::wrap(['foo' => 'bar']);

Iteration:

foreach (IterableCollection::wrap(['foo' => 'bar']) as $key => $value) {
    // use $key and $value
}

Check All True:

IterableCollection::wrap([true, false, true])->all();
# false

IterableCollection::wrap([true, true, true])->all();
# true

IterableCollection::wrap([1, 2, 'a'])->all('is_numeric');
# false

Check Any True:

IterableCollection::wrap([true, false, true])->any();
# true

IterableCollection::wrap(['a', 'b', 'c'])->any(fn ($i) => $i === 'd');
# false

Change Key Case:

$o = IterableCollection::wrap(['Hello' => 'World!']);

$o->changeKeyCase(CASE_LOWER);
# IterableCollection {
#   hello: "World!",
# }

$o->changeKeyCase(CASE_UPPER);
# IterableCollection {
#   HELLO: "World!",
# }

Chunk:

$o = IterableCollection::wrap(range(1,4));

// No preserve keys (default)
$o->chunk(2);
# IterableCollection {
#   0: [
#     1,
#     2,
#   ],
#   1: [
#     3,
#     4,
#   ],
# }

// Preserve keys
$o->chunk(2, true);
# IterableCollection {
#   0: [
#     0 => 1,
#     1 => 2,
#   ],
#   1: [
#     2 => 3,
#     3 => 4,
#   ],
# }

Column:

$o = IterableCollection::wrap([
    ['id' => 123, 'name' => 'Judah Wright',],
    ['id' => 456, 'name' => 'Jane Doe',],
]);

// Without column key (default)
$o->column('name');
# IterableCollection {
#   0: "Judah Wright",
#   1: "Jane Doe",
# }

// With a column key
$o->column('name', 'id');
# IterableCollection {
#   123: "Judah Wright",
#   456: "Jane Doe",
# }

Count:

IterableCollection::wrap([1, 2, 3])->count();
# 3

Combine:

$keys = ['foo', 'bing'];
$values = ['bar', 'baz'];
IterableCollection::wrap($keys)->combine($values);
# IterableCollection {
#   foo: "bar",
#   bing: "baz",
# }

Count Values:

IterableCollection::wrap(['foo', 'bar', 'foo', 'baz'])->countValues();
# IterableCollection {
#   foo: 2,
#   bar: 1,
#   baz: 1,
# }

Fill Keys:

IterableCollection::wrap(['a', 'b', 'c'])->fillKeys('foo');
# IterableCollection {
#   a: "foo",
#   b: "foo",
#   c: "foo",
# }

Filter:

$inputs = [true, false, null, "Hello!"];

// Without a callback (default) returns keeps the "truthy" values
IterableCollection::wrap($inputs)->filter();
# IterableCollection {
#   0: true,
#   3: "Hello!",
# }

// With a callback you can do your own evaluation
IterableCollection::wrap($inputs)->filter(
    static fn ($value): bool => !is_null($value)
);
# IterableCollection {
#   0: true,
#   1: false,
#   3: "Hello!",
# }

Flip:

IterableCollection::wrap(['key' => 'value'])->flip();
# IterableCollection {
#   value: "key",
# }

In:

$o = IterableCollection::wrap(['foo', 'bar']);

$o->in('foo');
# true

$o->in('baz');
# false

Key Exists:

$o = IterableCollection::wrap(['foo' => 'bar', 'bing' => 'baz']);

$o->keyExists('foo');
# true

$o->keyExists('asdf');
# false

Keys:

IterableCollection::wrap(['foo' => 'bar', 'bing' => 'baz'])->keys();
# IterableCollection {
#   0: "foo",
#   1: "bing",
# }

Map:

$o = IterableCollection::wrap(['John' => 'Smith', 'Jane' => 'Doe']);
$names = $o->map(
    fn ($firstname, $lastname): string => "{$firstname} {$lastname}"
);
# IterableCollection {
#   0: "Smith John",
#   1: "Doe Jane",
# }

Reduce:

$o = IterableCollection::wrap([1, 2, 3]);
$o->reduce(static fn (int $carry, int $item): int => $carry + $item, 0);
# 6

To Array:

$iterator = new ArrayIterator(['foo' => 'bar']);
print_r(IterableCollection::wrap($iterator)->toArray());
# Array
# (
#     [foo] => bar
# )

Values:

IterableCollection::wrap(['key1' => 'value1', 'key2' => 'value2'])->values();
# IterableCollection {#2519
#   0: "value1",
#   1: "value2",
# }

Walk Recursive:

$tree = [
    'limb1' => [
        'branch1' => 'leaf1', 
        'branch2' => 'leaf2',
    ], 
    'limb2' => 'leaf3'
];
IterableCollection::wrap($tree)->walkRecursive(
    fn ($item, $key, $userdata) => "{$userdata} - {$key} - {$item}",
    'tree',
);
# IterableCollection {
#   0: "tree - branch1 - leaf1",
#   1: "tree - branch2 - leaf2",
#   2: "tree - limb2 - leaf3",
# }

Walk:

$o = IterableCollection::wrap(['Bears', 'Beets', 'Battlestar Galactica']);
$o->walk(
    function (&$item, &$key, int &$userdata): void {
        $item = strtoupper($item);
        $key = ['a', 'b', 'c'][$userdata];    
        $userdata++;
    },
    0,
);
# IterableCollection {
#   a: "BEARS",
#   b: "BEETS",
#   c: "BATTLESTAR GALACTICA",
# }

Deliberately Excluded Functions

Items that would require the input to implement the Iterator class, such as:

Items that would require some form of indexing, ordering, or searching. These include:

Some functions responsible for creating a new list, such as:

judahnator/iterable-collection 适用场景与选型建议

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

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

围绕 judahnator/iterable-collection 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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