承接 pulli/laravel-collection-macros 相关项目开发

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

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

pulli/laravel-collection-macros

最新稳定版本:v1.3.0

Composer 安装命令:

composer require pulli/laravel-collection-macros

包简介

Package for some custom collection macros.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Contains some handy collection macros.

Installation

You can install the package via composer:

composer require pulli/laravel-collection-macros

You can publish the service provider via:

php artisan vendor:publish --tag="pulli-collection-macros-provider"

You can publish the config file with:

php artisan vendor:publish --tag="pulli-collection-macros-config"

This is the contents of the published config file:

return [
    'auto-update' => true,
];

Usage

Macros

even

Filter the collection to only integer values and return the even ones.

$collection = Collection::range(0, 6)->even();

// returns [0, 2, 4, 6]

// optional you can pass true as parameter to preserve the original array keys

$collection = Collection::range(0, 6)->even(true);

// returns [0 => 0, 2 => 2, 4 => 4, 6 => 6]

explode

Returns a collection from an exploded string. It uses the same signature as the native explode function.

$collection = Collection::explode(';', 'Twenty;Banana;Boats');

// returns ['Twenty', 'Banana', 'Boats']

firstAndLast

Returns the first and last element as array. Optional a callable and a default value for the first and last element can be specified.

[$first, $last] = Collection::make(['Jane', 'John', 'Joe'])->firstAndLast();

// $first = 'Jane', $last = 'Joe'

firstAndLastKey

Returns the first and last key as array. Optional a callable and a default value for the first and last element can be specified.

[$first, $last] = Collection::make(['Jane', 'John', 'Joe'])->firstAndLastKey();

// $first = 0, $last = 2

implodeToStringable

Implodes the collection to a Stringable object.

$collection = Collection::make(['Jane', 'John'])->implodeToStringable(', ');

// Stringable of "Jane, John"

joinToStringable

Joins the collection to a Stringable object.

$collection = Collection::make(['Jane', 'John', 'Jack'])->joinToStringable(', ', ' and ');

// Stringable of "Jane, John and Jack"

mapToCollection

Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling.

$collection = Collection::make([['test' => 1], 2, 3])->mapToCollection([4, 5]);

$collection->get(0)->get('test'); // returns 1

// Item has a toArray() public method, then it can be wrapped into a collection like this:
$collection = Collection::make([Item(), Item()])->mapToCollection([Item()]);

mapToCollectionFrom

Static method: Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling.

$collection = Collection::mapToCollectionFrom([['test' => 1], 2, 3]);

$collection->get(0)->get('test'); // returns 1

// Item has a toArray() public method, then it can be wrapped into a collection like this:
$collection = Collection::mapToCollectionFrom([Item(), Item()], true);

odd

Filter the collection to only integer values and return the odd ones.

$collection = Collection::range(0, 6)->odd();

// returns [1, 3, 5]

// optional you can pass true as parameter to preserve the original array keys

$collection = Collection::range(0, 6)->odd(true);

// returns [1 => 1, 3 => 3, 5 => 5]

positive

Returns a boolean value, if the collection contains elements or not.

Collection::make([1, 2, 3])->positive() // returns true
Collection::make()->positive() // returns false

recursiveToArray

It maps all arrays/objects recursively to an array.

// Item has a toArray() public method, then it can be wrapped into the collection like this:
$array = Collection::make(['item1' => Item(), 'item2' => Item()])->recursiveToArray();

recursiveToArrayFrom

Static method: Like mapToCollectionFrom it maps all arrays/objects recursively to an array.

// Item has a toArray() public method, then it can be wrapped into the collection like this:
$array = Collection::recursiveToArrayFrom(['item1' => Item(), 'item2' => Item()]);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-28