定制 tleckie/collection 二次开发

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

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

tleckie/collection

Composer 安装命令:

composer require tleckie/collection

包简介

PHP Collection object

README 文档

README

Latest Version on Packagist Scrutinizer Code Quality Build Status Total Downloads Code Intelligence Status

Installation

You can install the package via composer:

composer require tleckie/collection

Usage

Hash

Hash collections should be used when you need the keys to be strings.

<?php

use Tleckie\Collection\Hash;
use Tleckie\Collection\HashInterface;

// Create your own collection type
class UserCollection extends Hash {}

$users = [
    'user1' => new User('John'),
    'user2' => new User('Pedro'),
    'user3' => new User('Mario'),
    'user4' => new User('Walter'),
    'user5' => new User('Mario')
];

$userCollection = new UserCollection($users);

/**
 * @param HashInterface $userCollection
 */
public function addCollection(HashInterface $userCollection){ ... }

/**
 * @param UserCollection $userCollection
 */
public function addCollection(UserCollection $userCollection){ ... }
use Tleckie\Collection\Hash;

$collection = new Hash(['key' => 'value']);
$collection->append('otherKey', 'other value');

Hash methods:

public function current(): mixed;

public function next(): void;

public function key(): null|string;

public function valid(): bool;

public function rewind(): void;

public function count(): int;

public function sortByKey(): HashInterface;

public function sortByValue(): HashInterface;

public function sort(callable$callable): HashInterface;

public function reverse(): HashInterface;

public function filter(callable $filterFunction): HashInterface;

public function find(callable $findFunction): HashInterface;

public function empty(): HashInterface;

public function hasKey(string $key): bool;

public function hasValue(mixed $item): bool;

public function get(string $key): mixed;

public function append(string $key, mixed $item): HashInterface;

public function prepend(string $key, mixed $item): HashInterface;

public function remove(string $key): HashInterface;

public function shuffle(): HashInterface;

public function toArray(): array;

Sequence:

The sequences are used when the key of the collection is not important besides being ordered. Many of its methods are immutable.

If you want you can create your own type of collection

<?php

use Tleckie\Collection\Sequence;
use Tleckie\Collection\SequenceInterface;

// Create your own collection type
class UserCollection extends Sequence {}

$users = [
    new User('John'),
    new User( 'Pedro'),
    new User('Mario'),
    new User('Walter'),
    new User('Mario')
];

$userCollection = new UserCollection($users);

/**
 * @param SequenceInterface $userCollection
 */
public function addCollection(SequenceInterface $userCollection){ ... }

/**
 * @param UserCollection $userCollection
 */
public function addCollection(UserCollection $userCollection){ ... }
use Tleckie\Collection\Sequence;

$collection = new Sequence(['value', 'value2']);
$collection->append('other value');

Sequence methods:

public function current(): mixed;

public function next(): void;

public function key(): null|int;

public function valid(): bool;

public function rewind(): void;

public function count(): int;

public function sortByKey(): SequenceInterface;

public function sortByValue(): SequenceInterface;

public function sort(callable$callable): SequenceInterface;

public function reverse(): SequenceInterface;

public function filter(callable $filterFunction): SequenceInterface;

public function find(callable $findFunction): SequenceInterface;

public function empty(): SequenceInterface;

public function hasKey(int $index): bool;

public function hasValue(mixed $item): bool;

public function get(int $index): mixed;

public function append(mixed $item): SequenceInterface;

public function prepend(mixed $item): SequenceInterface;

public function remove(int $index): SequenceInterface;

public function shuffle(): SequenceInterface;

public function toArray(): array;

Collection:

Use the collection when you have numeric and string keys.

<?php

use Tleckie\Collection\Collection;

// Create your own collection type
class UserCollection extends Collection {}

$users = [
    new User(1, 'John'),
    new User(2, 'Pedro'),
    new User(3, 'Mario'),
    new User(4, 'Walter'),
    new User(5, 'Mario')
];

$collection = new UserCollection($users);



// iterate
foreach($collection as $user){
    $user->name();
}


// count elements
$collection->count();   //5
count($collection);     //5


// filter
$collection = $collection->filter(function (int $key, User $user) {
    return $user->id() > 4;
}); // UserCollection( [ User(5) ] );


// filterNot
$collection = $collection->filterNot(function (int $key, User $user) {
    return $user->id() > 4;
}); // UserCollection( [ User(1),User(2),User(3),User(4) ] );


// find
$collection = $collection->find(function (int $key, User $user) {
    return $user->name() === 'Mario';
}); // UserCollection( [ User(3), User(5) ] );


// findIndex
$collection = $collection->find(function (int $key, User $user) {
    return $key === 1;
}); // UserCollection( [ User(2) ] );


// sort
$collection = $collection->sort(function (User $current, User $next) {
    if ($current->id() === $next->id()) {
        return 0;
    }
    return ($current->id() > $next->id()) ? -1 : 1;
}); // UserCollection( [ User(5), User(4), User(3), User(2), User(1) ] );

Other methods:

$collection->shuffle(): CollectionInterface;

$collection->reverse(): CollectionInterface;

$collection->prepend(mixed $item): CollectionInterface;

$collection->append(mixed $item): CollectionInterface;

$collection->toArray(): array;

$collection->push(mixed $item): CollectionInterface;

$collection->pull(): mixed;

tleckie/collection 适用场景与选型建议

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

它主要适用于以下技术方向: 「php」 「collection」 「poo」 「php-collection」 「php-8」 「tleckie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-30