DEV Community

Cover image for Small Collection 2.6
sebk69
sebk69

Posted on

Small Collection 2.6

Small Collection 2.6 is out.

A lot of new methods to collections and a new Trigger management.

git : https://git.small-project.dev/lib/small-collection
packagist : https://packagist.org/packages/small/collection

Triggers

Triggers are methods or function to be called at each time a value is inserted :

$collection = new Collection();

$fn = function (&$value, &$key) {
    $value++;
};

$collection->addTrigger($fn);
$collection->merge([1, 2, 3], true);
Enter fullscreen mode Exit fullscreen mode

$collection will be :

[2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

You can add triggers directly in constructor :

  • Single trigger
$fn = function (&$value, &$key) {
    $value++;
};
$collection = new Collection([1, 2, 3], $fn);
Enter fullscreen mode Exit fullscreen mode
  • Multiple triggers
$collection = new Collection([1, 2, 3], [$fn, $fn2]);
Enter fullscreen mode Exit fullscreen mode
  • Or using a CallbackCollection
$callbacks = new \Small\Collection\Collection\CallbackCollection([
    $fn1,
    $fn2,
]);
$collection = new Collection([1, 2, 3], $callbacks);
Enter fullscreen mode Exit fullscreen mode

New methods

flatten

This method bring all values of sub collections in collection.

Example:

$result = (
    new Collection([
        'a' => 1,
        'b' => [
            'e' => 2.1,
            'f' => 2.2
        ], 'c' => 3
    ]))->flatten();
Enter fullscreen mode Exit fullscreen mode

Result :

{
  "a": 1,
  "e": 2.1,
  "f": 2.2,
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

combine

This method combine a collection with another :

$from = new Collection([
    'a' => 1,
    'b' => 2,
    'c' => 3,
]);

$result = $from->combine(['a' => 2, 'c' => 3, 'z' => 26]);
Enter fullscreen mode Exit fullscreen mode

The values of collection will be keys of result and values of collection to apply will be values of result.

Where no corresponding key, the value is dropped.

{
  "1": 2,
  "3": 3
}
Enter fullscreen mode Exit fullscreen mode

find first occurrences

Return first occurrence of value in collection :

$from = new Collection(['a', 'b', 'c', 'd']);
$from->findFirstOccurrence('b'); // return 1
Enter fullscreen mode Exit fullscreen mode

remove

Remove collection entries which value equal :

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$collection->remove(1);
Enter fullscreen mode Exit fullscreen mode

The collection will be :

{
  "b": 2,
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

You can pass array or collections of values :

$collection->remove([2, 3]);
Enter fullscreen mode Exit fullscreen mode

subPart

This method return sub partition of collection from a key with a given length :

$collection = new Collection(range(0, 26));
$sub = $collection->subPart(5, 6);
Enter fullscreen mode Exit fullscreen mode

Result :

{
  "5": 5,
  "6": 6,
  "7": 7,
  "8": 8,
  "9": 9,
  "10": 10
}
Enter fullscreen mode Exit fullscreen mode

countDistinct

This method cound number of distinct values :

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 3]);
$cpt = $collection->countDistinct();
Enter fullscreen mode Exit fullscreen mode

$cpt will be 3

dot

This method return a flatten collection which keys are combined keys of sub collection separated by a dot :

$collection = new Collection(['a' => [1, 2], 'b' => [2, 3], 'c' => 3]);
$doted = $collection->dot();
Enter fullscreen mode Exit fullscreen mode

$doted will be a new collection :

{
  "a.0": 1,
  "a.1": 2,
  "b.0": 2,
  "b.1": 3,
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

It is possible to change the separator :

$doted = $collection->dot('/');
Enter fullscreen mode Exit fullscreen mode

Result :

{
  "a/0": 1,
  "a/1": 2,
  "b/0": 2,
  "b/1": 3,
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

undot

This method does exactly the reverse way of dotted :

$collection = new Collection(['a.1' => 1, 'b.Z.7' => 2, 'c' => 3]);
$undoted = $collection->undot();
Enter fullscreen mode Exit fullscreen mode

Done :

{
  "a": {
    "1": 1
  },
  "b": {
    "Z": {
      "7": 2
    }
  },
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

Same as dot, you can change the separator :

$collection = new Collection(['a/1' => 1, 'b/Z/7' => 2, 'c' => 3]);
$undoted = $collection->undot('/');
Enter fullscreen mode Exit fullscreen mode

remove duplicates

This method remove duplicates in a collection :

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 3]);
$result = $collection->removeDuplicates();
Enter fullscreen mode Exit fullscreen mode

Result :

{
  "a": 1,
  "b": 2,
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

It is possible to apply modifications directly to the collection :

$collection->removeDuplicates(true);
Enter fullscreen mode Exit fullscreen mode

foreach

This method call a callback for each element :

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);

$sum = 0;
$collection->foreach(function($value, $key) use(&$sum) {
    $sum += $value;
    return true;
});
Enter fullscreen mode Exit fullscreen mode

$num will be 6.

flip

This method invert keys and values :

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$collection->flip();
Enter fullscreen mode Exit fullscreen mode

$collection will be :

{
  "1": "a",
  "2": "b",
  "3": "c"
}
Enter fullscreen mode Exit fullscreen mode

It is possible to separate result with original collection :

$collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3]);
$result = $collection->flip(true);
Enter fullscreen mode Exit fullscreen mode

$result will be :

{
  "1": "a",
  "2": "b",
  "3": "c"
}
Enter fullscreen mode Exit fullscreen mode

and $collection still be :

{
  "a": 1,
  "b": 2,
  "c": 3
}
Enter fullscreen mode Exit fullscreen mode

paginate

This method return a sub collection corresponding to a page num for a given page size.

$collection = new Collection(range(1, 11));

$page = 1
do {
    $last = $collection->paginate($page, 3);
    if ($last->count() > 0) {
        $result[$page - 1] = $last;
    }
    $page++;
} while ($last->count() > 0);
Enter fullscreen mode Exit fullscreen mode

Rsult will be :

[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [10, 11, 12]
]
Enter fullscreen mode Exit fullscreen mode

values as objects

This method create objects from collection values :

$collection = new Collection([['a', 'b'], ['c']]);
$result = $collection->valuesAsObjects(
    Chars::class,
);

foreach ($result as $item) {
    echo $item->toString() . "\n";
}
Enter fullscreen mode Exit fullscreen mode

The result output will be :

ab
c
Enter fullscreen mode Exit fullscreen mode

odd, even and nth

Odd method give a new collection walking original collection populating only odd walk :

$collection = new Collection([1, 2, 3]);
$result = $collection->odd();
Enter fullscreen mode Exit fullscreen mode

Result will be

[1, 3]
Enter fullscreen mode Exit fullscreen mode

Same for even :

$collection = new Collection([1, 2, 3, 4]);
$result = $collection->even();
Enter fullscreen mode Exit fullscreen mode

Result will be

[2, 4]
Enter fullscreen mode Exit fullscreen mode

nth is the same, but you can define the size of each step :

$collection = new Collection([
    1, 2, 3, 4, 5, 6
]);
$result = nth(3);
Enter fullscreen mode Exit fullscreen mode

$result will be :

[1, 4]
Enter fullscreen mode Exit fullscreen mode

You can also give a beginning offset, which must be less that step size :

$collection = new Collection([
    1, 2, 3, 4, 5, 6
]);
$result = nth(3, 1);
Enter fullscreen mode Exit fullscreen mode

Which result :

[2, 5]
Enter fullscreen mode Exit fullscreen mode

skip

This method skip x occurence of collection :

$collection = new Collection([
    1, 2, 3, 4, 5, 6,
]);
$collection->skip(3);
Enter fullscreen mode Exit fullscreen mode

Will result in :

[4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

transform

Transform translate a collection to another :

$collection = new Collection([
    1, 2, 3, 4, 5, 6,
]);
$result = $collection->transform(NumericCollection::class);
Enter fullscreen mode Exit fullscreen mode

The result collection is a clone of collection, but it is an instance of NumericCollection.

The CheckValueInterface is called to check if all collections values are compatible with destination class.

$collection = new Collection([
    1, 2, 3, 4, 5, 6, 'a'
]);
$result = $collection->transform(NumericCollection::class);
Enter fullscreen mode Exit fullscreen mode

Will throw a ValidationFailException.

Triggers

Triggers are methods or function to be called at each time a value is inserted :

$collection = new Collection();

$fn = function (&$value, &$key) {
    $value++;
};

$collection->addTrigger($fn);
$collection->merge([1, 2, 3], true);
Enter fullscreen mode Exit fullscreen mode

$collection will be :

[2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

You can add triggers directly in constructor :

  • Single trigger
$fn = function (&$value, &$key) {
    $value++;
};
$collection = new Collection([1, 2, 3], $fn);
Enter fullscreen mode Exit fullscreen mode
  • Multiple triggers
$collection = new Collection([1, 2, 3], [$fn, $fn2]);
Enter fullscreen mode Exit fullscreen mode
  • Or using a CallbackCollection
$callbacks = new \Small\Collection\Collection\CallbackCollection([
    $fn1,
    $fn2,
]);
$collection = new Collection([1, 2, 3], $callbacks);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)