承接 guennichi/collection 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

guennichi/collection

最新稳定版本:1.0.3

Composer 安装命令:

composer require guennichi/collection

包简介

A lightweight PHP >= 8.1 library to create immutable collections of objects

README 文档

README

A lightweight PHP >= 8.1 library to create immutable/readonly and strictly typed collection of objects.

Installation

composer require guennichi/collection

Usage

use Guennichi\Collection\Collection;

final class Person
{
    public function __construct(public readonly string $name) {}
}

/**
 * @extends Collection<Person>
 */
final class PersonCollection extends Collection
{
    public function __construct(Person ...$elements)
    {
        parent::__construct(...$elements);
    }
}

$persons = new PersonCollection(
    new Person('Person1'),
    new Person('Person2'),
    new Person('Person3'),
);

$persons->first()->name // Person1. Also supports autocomplete, thanks @template annotations.
$persons->contains(new Person('Person1')); // false
$persons->count(); // 3
count($persons); // 3
$persons->each(static function (Person $person, int $index) {
    // Do something
});
$persons->filter(static fn (Person $person) => $person->name === 'Person3'); // new PersonCollection instance with [Person('Person3')]

final class Employee
{
    public function __construct(public readonly string $name) {}
}

/**
 * @extends Collection<Employee>
 */
final class EmployeeCollection extends Collection
{
    public function __construct(Employee ...$elements)
    {
        parent::__construct(...$elements);
    }
}

$employees = $persons->filter(static fn (Person $person) => in_array($person->name, ['Person1', 'Person3']))
                     ->mapTo(EmployeeCollection::class, static fn (Person $person) => new Employee($person->name));

foreach ($employees as $employee) {
    // $employee is instanceof Employee class
}

$employees->first(); // Employee('Person1'): instance of Employee class
$employees->every(static fn (Employee $employee) => !empty($employee->name)); // True
$employees->sortAscBy(static fn (Employee $employee) => $employee->name); // new collection instance with [Employee('Person1'), Employee('Person3')]
$employees->sortDescBy(static fn (Employee $employee) => $employee->name); // new collection instance with [Employee('Person3'), Employee('Person1')]

json_encode($employees) // [{"name":"Person1"}, {"name":"Person3"}]

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固