tasoft/collection

v8.0.1 2023-02-06 08:19 UTC

This package is auto-updated.

Last update: 2024-09-06 11:53:54 UTC


README

与数组一起工作,并提供一系列功能

  • 使用多个排序描述符对集合进行排序
$collection = new DefaultCollection(
  [
    "echo",
    "alpha",
    "delta",
    "charlie",
    "bravo"
  ]
);
$collection->sort([
  new DefaultSortDescriptor(true) // Sort ascending
]);

print_r( $collection->toArray() ); // [alpha, bravo, charlie, delta, echo]
  • 创建标记集合,因此每个元素都可以有标签
$collection = new TaggedCollection([1, 2, 3], /* accept duplicates */ true, /*case sensitive*/ true, /* tags ...*/ "test", "haha");

print_r($collection["test"]); // [1, 2, 3]
print_r($collection["unexisting tag"]); // NULL -- without notice
  • 创建优先级集合,它将自动根据元素优先级进行排序
$collection = new PriorityCollection();
$collection->add(3, [5, 7]);
$collection->add(1, 8, 7);
$collection->add(7, 11, 3);


print_r($collection->toArray()); // [8, 7, [5, 7], 11, 3]
  • 创建依赖集合,元素可以依赖于其他元素
$collection = new DependencyCollection();
$collection->add('thomas', 1, ["Foo", 'Bar']);
$collection->add('Foo', 2);
$collection->add("Bar", 3, ["Foo"]);
$collection->add("Bettina", 6, ["thomas"]);

print_r($collection->getElementDependencies("thomas")); // [Foo, Bar]
print_r($collection->getOrderedElements());
/*
[
  Foo => 1,
  Bar => 3,
  thomas => 1
  Bettina => 6
]
*/

只需使用 composer 安装即可

$ composer require tasoft/collection