pion/laravel-support-collection 问题修复 & 功能扩展

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

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

pion/laravel-support-collection

Composer 安装命令:

composer require pion/laravel-support-collection

包简介

The support classes for the Laravels collection

README 文档

README

A mix of classes for improving the Collection.

Installation

composer require pion/laravel-support-collection

NestedObjectCollection

Creates a nested collection from flat collection to child nested collection based on parent property and id property.

It's expected that the values are objects, it will insert the childs into the desired property.

All childs are stored to childs property in the object. Can be empty.

You can call all Collection methods on the NestedObjectCollection object, all method calls are passed into the collection

Construct

Groups the items by the parent property and loops from the top of tree (null property or different value) and adds childs to the object.

Parameters

  • Collection $items

  • string $parentProperty the property name to get the parent id

  • string $idProperty the property name to get the id value

  • string $rootIndexKeyOnGroup the index key for getting the root elements. When the parent property value returns null, it will be empty string.

  • string $propertyForChildren the property name to store the children

    public function __construct(Collection $items, $parentProperty = "parent_id", $idProperty = "id", $rootIndexKeyOnGroup = "", $propertyForChildren = "childs")

Eloquent usage

It's advised to set a property in the Eloquent model to prevent creating childs into the attributes. For default property name you can use the ChildsCollectionTrait. This provides a public $childs property for your Eloquent model

Example

$object1 = new stdClass();
$object1->id = 1;

// this is the root!
$object1->parent_id = null;

$object2 = new stdClass();

$object2->id = 2;
$object2->parent_id = 2;


$object3 = new stdClass();

$object3->id = 3;
$object3->parent_id = 1;

$example = new \Pion\Support\Collection\NestedObjectCollection(new \Illuminate\Support\Collection([
    $object1, $object2, $object3
]));

$collection = $example->getCollection();

This example will create a collection of single item (the $object1). This objects will have a new property $childs which is holding a Collection of single item $object2 that holds again property $childs with a single item $object3

GroupedCollection

Enables a quick how to build a grouped collection. Just add a new item to the collection with given group key and the value. Optionally you can provide also the key for the value.

public function add($groupKey, $value, $key = null)

Example

The use of the collection is same as standard collection.

$groupedCollection = new GroupedCollection();
$groupedCollection->add("1", "test 1");
$groupedCollection->add("1", "test 2");
$groupedCollection->add("1", "test 3");
$groupedCollection->add("2", "test 2-1");
$groupedCollection->add("2", "test 2-2");

This will result in a collection of items with a collection object. If you export the collection into array you will grouped array as expected.

var_dump($groupedCollection->toArray());

array(2) {
  [1] =>
  array(3) {
    [0] =>
    string(6) "test 1"
    [1] =>
    string(6) "test 2"
    [2] =>
    string(6) "test 3"
  }
  [2] =>
  array(2) {
    [0] =>
    string(8) "test 2-1"
    [1] =>
    string(8) "test 2-2"
  }
}

Testing

Run the full support matrix and get a pass/fail table:

bin/test-matrix.sh

Run the PHPUnit suite in Docker for each supported PHP runtime:

docker compose run --rm php82 composer test
docker compose run --rm php83 composer test
docker compose run --rm php85 composer test

Resolve and test a specific Laravel support line inside Docker:

docker compose run --rm php82 bin/test-target.sh '^9.0'
docker compose run --rm php82 bin/test-target.sh '^10.0'
docker compose run --rm php83 bin/test-target.sh '^11.0'
docker compose run --rm php83 bin/test-target.sh '^12.0'
docker compose run --rm php85 bin/test-target.sh '^13.0'

pion/laravel-support-collection 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-18