承接 gamez/typed-collection 相关项目开发

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

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

gamez/typed-collection

Composer 安装命令:

composer require gamez/typed-collection

包简介

Type-safe collections based on Laravel Collections

README 文档

README

Latest Stable Version Total Downloads Tests Sponsor

Note

Laravel 11 added the ensure() collection method that verifies that all elements of a collection are of a given type or list of types. However, this verification does not prevent items of different types to be added at a later time.

Note

If you use Laravel collections combined with Larastan/PHPStan, you won't need this library and can just™ annotate your collection classes directly.

Installation

The package can be installed with Composer:

$ composer require gamez/typed-collection

Usage

class Person
{
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}

$taylor = new Person('Taylor');
$jeffrey = new Person('Jeffrey');

Typed Collections

use Gamez\Illuminate\Support\TypedCollection;

/**
 * @extends TypedCollection<array-key, Person> 
 */
class People extends TypedCollection
{
    protected static array $allowedTypes = [Person::class];
}

$people = People::make([$taylor, $jeffrey])
    ->each(function (Person $person) {
        printf("This is %s.\n", $person->name);
    });
/* Output:
This is Taylor.
This is Jeffrey.
*/

try {
    People::make('Not a person');
} catch (InvalidArgumentException $e) {
    echo $e->getMessage().PHP_EOL;
}
/* Output:
Output: A People collection only accepts items of the following type(s): Person.
*/

Lazy Typed Collections

use Gamez\Illuminate\Support\LazyTypedCollection;

/**
 * @extends LazyTypedCollection<array-key, Person> 
 */
class LazyPeople extends LazyTypedCollection
{
    protected static array $allowedTypes = [Person::class];
}

$lazyPeople = LazyPeople::make([$taylor, $jeffrey])
    ->each(function (Person $person) {
        printf("This is %s.\n", $person->name);
    });
/* Output:
This is Lazy Taylor.
This is Lazy Jeffrey.
*/

try {
    LazyPeople::make('Nope!');
} catch (InvalidArgumentException $e) {
    echo $e->getMessage().PHP_EOL;
}
/* Output:
Output: A People collection only accepts objects of the following type(s): Person.
*/

Mixed collections

/**
 * @extends LazyTypedCollection<array-key, int|string|Person> 
 */
class MixedTypeCollection extends TypedCollection
{
    protected static array $allowedTypes = ['int', 'string', Person::class];
}

Supported types

Supported types are class strings, like Person::class, or types recognized by the get_debug_type() function, int, float, string, bool, and array.

Helper functions

The typedCollect() helper function enables you to dynamically create typed collections on the fly:

$dateTimes = typedCollect([new DateTime(), new DateTime()], DateTimeInterface::class);

For further information on how to use Laravel Collections, have a look at the official documentation.

gamez/typed-collection 适用场景与选型建议

gamez/typed-collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 360k 次下载、GitHub Stars 达 45, 最近一次更新时间为 2017 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 45
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-31