astronphp/collection

This package is abandoned and no longer maintained. The author suggests using the astronphp/collection package instead.

better abstraction of array built in type

0.0.1 2019-09-17 18:43 UTC

This package is auto-updated.

Last update: 2022-03-30 00:16:20 UTC


README

Instalação

composer require astronphp/collection

Guia do Usuário

construct

public function __construct($content = [])

Recebe opcionalmente um array ou um objeto, que será convertido internamente.

use Astronphp\Collection\Collection;

$collection1 = new Collection();
$collection2 = new Collection(['lorem' => 'ipsum']);
$collection3 = new Collection(new \DateTime('now'));

var_dump($collection1, $collection2, $collection3);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => array (size = 0)
  protected 'length'  => int 0

object(Astronphp\Collection\Collection)[2]
  protected 'content' => array (size=1)
    'lorem' => string 'ipsum' (length=5)
  protected 'length' => int 1

object(Astronphp\Collection\Collection)[4]
  protected 'content' => array (size=3)
      'date' => string '2019-09-17 14:42:47.000000' (length=26)
      'timezone_type' => int 3
      'timezone' => string 'UTC' (length=3)
  protected 'length' => int 3
*/

unshift

public function unshift(...$values): self

Adiciona valores no início da coleção.

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->unshift('lorem');
$collection->unshift('ipsum', 'dolor');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=3)
      0 => string 'ipsum' (length=5)
      1 => string 'dolor' (length=5)
      2 => string 'lorem' (length=5)
  protected 'length' => int 3
*/

push

public function push(...$values): self

Adiciona valores ao final da coleção.

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->push('lorem');
$collection->push('ipsum', 'dolor');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=3)
      0 => string 'lorem' (length=5)
      1 => string 'ipsum' (length=5)
      2 => string 'dolor' (length=5)
  protected 'length' => int 3
*/

set

public function set(string $key, $value): self

Associa uma chave à um valor.

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');
$collection->set('dolor.amet', 'consectetur');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=2)
      'lorem' => string 'ipsum' (length=5)
      'dolor' => 
        array (size=1)
          'amet' => string 'consectetur' (length=11)
  protected 'length' => int 2
*/

get

public function get(string $key)

Recupera valores da coleção

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');
$collection->set('dolor.amet', 'consectetur');

$collection->get('lorem'); // ipsum
$collection->get('dolor.amet'); // consectetur

isset

public function isset(string $key): bool

Verifica chaves não inicializadas ou com valor nulo

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');

$collection->isset('lorem'); // true
$collection->isset('dolor'); // false

empty

public function empty(string $key): bool

Verifica chaves vazias

use Astronphp\Collection\Collection;

$collection = new Collection();

$collection->set('lorem', 1);
$collection->set('ipsum', 0);

$collection->empty('lorem'); // false
$collection->empty('ipsum'); // true

unset

public function unset(string $key)

Remove chaves

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=1)
      'lorem' => string 'ipsum' (length=5)
  protected 'length' => int 1
*/

$collection->unset('lorem');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=0)
      empty
  protected 'length' => int 0
*/

length

public function length(): int

Recupera o tamanho da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->length(); // 5

shift

public function shift()

Remove o primeiro elemento da coleção retornando o elemento removido

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->shift(); // 1

pop

public function pop()

Remove o ultimo elemento da coleção retornando o elemento removido

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->pop(); // 5

first

public function first()

Recupera o primeiro item da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->first(); // 1

last

Recupera o último item da coleção

public function last()
use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->last(); // 5

each

public function each(callable $callback)

Percorre toda a coleção

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
$collection->each(function($key, $value) {
    var_dump($key, $value);
});

/*
string 'lorem' (length=5)
string 'ipsum' (length=5)

string 'dolor' (length=5)
string 'amet' (length=4)
*/

for

public function for(int $start, int $step, callable $callback)

Percorre a coleção em passos

use Astronphp\Collection\Collection;

$collection = new Collection([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$collection->for(0, 2, function($key, $value) {
    var_dump($value);
});

/* int 0, int 2, int 4, int 6, int 8, int 10 */

walk

Percorre a coleção recursivamente

public function walk(callable $callback, $type = \RecursiveIteratorIterator::LEAVES_ONLY)
use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', ['ipsum', 'dolor'], ['sit' => ['amet' => 'consectetur']]]);
$collection->walk(function($key, $value) {
    var_dump($key, $value);
});

/*
int 0
string 'lorem' (length=5)
int 0
string 'ipsum' (length=5)
int 1
string 'dolor' (length=5)
string 'amet' (length=4)
string 'consectetur' (length=11)
*/

sum

public function sum()

Soma todos os elementos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->sum(); // 15

contains

public function contains($value): bool

Verifica se um dado valor existe na coleção

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', 'ipsum', 'dolor']);
$collecion->contains('dolor'); // true

map

public function map(callable $callback): self

Aplica um callback em todos os elementos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', 'ipsum', 'dolor']);
var_dump($collection->map(function($key, $value) {
    return [$key => strtoupper($value)];
}));

/*
object(Astronphp\Collection\Collection)[4]
  protected 'content' => 
    array (size=3)
      0 => string 'LOREM' (length=5)
      1 => string 'IPSUM' (length=5)
      2 => string 'DOLOR' (length=5)
  protected 'length' => int 3
*/

filter

public function filter(callable $callback): self

Filtra a coleção utilizando um callback

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var_dump($collection->filter(function($key, $value) {
    return $value > 5;
}));

/*
object(Astronphp\Collection\Collection)[4]
  protected 'content' => 
    array (size=4)
      5 => int 6
      6 => int 7
      7 => int 8
      8 => int 9
  protected 'length' => int 4
*/

reduce

public function reduce(callable $callback): self

Reduz a coleção a um único valor

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$result     = $collection->reduce(function($a, $b) {
    return $a + $b;
});

var_dump($result); // int 15

join

public function join(string $glue)

Junta os elementos em uma string

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->join('-')); // string '1-2-3-4-5'

random

public function random(int $num = 1)

Recupera elementos aleatórios da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->random(); // 3

shuffle

public function shuffle(): self

Embaralha os elementos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->shuffle());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=5)
      0 => int 3
      1 => int 1
      2 => int 5
      3 => int 2
      4 => int 4
  protected 'length' => int 5
*/

flip

public function flip(): self

Inverte a relação entre chaves e valores da coleção

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', 'ipsum', 'dolor']);
var_dump($collection->flip());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=3)
      'lorem' => int 0
      'ipsum' => int 1
      'dolor' => int 2
  protected 'length' => int 3
*/

keys

public function keys(): self

Retorna uma coleção apenas com as chaves da coleção anterior

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
var_dump($collection->keys());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=2)
      0 => string 'lorem' (length=5)
      1 => string 'dolor' (length=5)
  protected 'length' => int 2
*/

values

public function values(): self

Retorna uma coleção apenas com os valores da coleção anterior

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor', 'amet']);
var_dump($collection->values());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=2)
      0 => string 'ipsum' (length=5)
      1 => string 'amet' (length=4)
  protected 'length' => int 2
*/

column

public function column($key, $index = null)

Recupera dados de uma coluna da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([
    [
        'lorem' => 'ipsum',
        'dolor' => 'amet',
    ],
    [
        'lorem' => 'dolor',
        'dolor' => 'consectetur',
    ],
]);

var_dump($collection->column('lorem'));

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=2)
      0 => string 'ipsum' (length=5)
      1 => string 'dolor' (length=5)
  protected 'length' => int 2
*/

chunk

public function chunk(int $size, bool $preserve_keys = false): self

Divide a coleção em partes iguais

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var_dump($collection->chunk(3));

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=3)
      0 => 
        array (size=3)
          0 => int 1
          1 => int 2
          2 => int 3
      1 => 
        array (size=3)
          0 => int 4
          1 => int 5
          2 => int 6
      2 => 
        array (size=3)
          0 => int 7
          1 => int 8
          2 => int 9
  protected 'length' => int 3
*/

unique

public function unique(int $flags = SORT_STRING): self

Remove duplicatas

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 1, 2, 3, 4, 5, 2, 3, 4]);
var_dump($collection->unique());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      6 => int 4
      7 => int 5
  protected 'length' => int 5
*/

coalesce

public function coalesce()

Retorna o primeiro valor não nulo encontrado

use Astronphp\Collection\Collection;

$collection = new Collection([null, null, null, 'lorem', null, null]);
var_dump($collection->coalesce()); // string 'lorem'

merge

public function merge();

Mescla todas as dimensões da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([
    ['lorem' => 'ipsum'],
    ['dolor' => 'sit'],
    ['amet'  => 'consectetur'],
]);

var_dump($collection->merge());

/*
object(Astronphp\Collection\Collection)[4]
  protected 'content' => 
    array (size=3)
      'lorem' => string 'ipsum' (length=5)
      'dolor' => string 'sit' (length=3)
      'amet' => string 'consectetur' (length=11)
  protected 'length' => int 3
*/

reverse

public function reverse($preserve_keys = null): self

Inverte a coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->reverse());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=5)
      0 => int 5
      1 => int 4
      2 => int 3
      3 => int 2
      4 => int 1
  protected 'length' => int 5
*/

search

public function search($value, bool $strict = null)

Retorna a chave do valor solicitado

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
var_dump($collection->search('ipsum')); // 'lorem'

lower

public function lower(): self

Transforma recursivamente o case de todas as chaves da coleção para minúsculo

use Astronphp\Collection\Collection;

$collection = new Collection(['Lorem' => 'Ipsum', 'Dolor' => 'Amet']);
var_dump($collection->upper());

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=2)
      'LOREM' => string 'Ipsum' (length=5)
      'DOLOR' => string 'Amet' (length=4)
  protected 'length' => int 2
*/

upper

public function upper(): self

Transforma recursivamente o case de todas as chaves da coleção para maiúsculo

use Astronphp\Collection\Collection;

$collection = new Collection(['Lorem' => 'Ipsum', 'Dolor' => 'Amet']);
var_dump($collection->lower());

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=2)
      'lorem' => string 'Ipsum' (length=5)
      'dolor' => string 'Amet' (length=4)
  protected 'length' => int 2
*/

toArray

public function toArray()

Recupera o array interno da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->toArray());

/*
array (size=5)
  0 => int 1
  1 => int 2
  2 => int 3
  3 => int 4
  4 => int 5
*/

toJson

public function toJson()

Retorna o conteúdo da coleção em uma string JSON

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
var_dump($collection->toJson()); // string '{"lorem":"ipsum","dolor":"amet"}'

sort

public function sort()

Ordena os valores da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([3, 4, 8, 7, 1, 5]);
$collection->sort(); //[1,3,4,5,7,8]

rsort

public function rsort()

Ordena os valores da coleção em ordem inversa

use Astronphp\Collection\Collection;

$collection = new Collection([3, 4, 8, 7, 1, 5]);
$collection->rsort(); //[8,7,5,4,3,1]

asort

public function asort()

Ordena os valores da coleção mantendo a associação

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->asort(); //["dolor" => "amet","sit" => "consectetur","lorem" => "ipsum"]

arsort

public function arsort()

Ordena os valores da coleção em ordem inversa mantendo a associação

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->arsort(); // ["lorem" => "ipsum","sit" => "consectetur","dolor" => "amet"]

ksort

public function ksort()

Ordena os valores da coleção pelas chaves

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->ksort(); //["dolor" => "amet","lorem" => "ipsum","sit" => "consectetur"]

krsort

public function krsort()

Ordena os valores da coleção pelas chaves em ordem inversa

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->krsort(); //["sit" => "consectetur","lorem" => "ipsum","dolor" => "amet"]

union

public function union()

Realiza a união entre todos os conjuntos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([
    [1, 2, 3],
    [3, 4, 5],
]);

$collection->union(); // [1, 2, 3, 4, 5]

diff

public function diff()

Realiza a diferença entre todos os conjuntos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3],
   [3, 4, 5],
]);

$collection->diff(); // [1, 2]

$collection = new Collection([
   [3, 4, 5],
   [1, 2, 3],
]);

outer

public function outer()

Realiza a diferença total entre conjuntos

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3],
   [3, 4, 5],
]);

$collection->outer(); // [[1, 2], [4, 5]]

intersect

public function intersect()

Realiza a intersecção entre todos os conjuntos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3, 4],
   [3, 4, 5, 6],
]);

$collection->intersect(); // [3, 4]

cartesian

public function cartesian()

Realiza o produto cartesiano entre todos os conjuntos da coleção

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3],
   [3, 4, 5],
]);

$collection->cartesian(); // [[1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,3], [3,4], [3,5]]

isCollection

public static function isCollection(): bool
use Astronphp\Collection\Collection;

Collection::isCollection(new Collection()); // true

Collection::isCollection(10); // false

combine

public function combine(): self
use Astronphp\Collection\Collection;

$array = ['lorem', 'ipsum', 'dolor'];

$collection = new Collection([1, 2, 3]);

var_dump(Collection::combine($array, $collection));

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=3)
      'lorem' => int 1
      'ipsum' => int 2
      'dolor' => int 3
  protected 'length' => int 3
*/

range

public static function range($start, $end, $step = 1): self
use Astronphp\Collection\Collection;

Collection::range($start = 10, $end = 20, $step = 2); // [10, 12, 14, 16, 18, 20] 

Collection::range($start = 'A', $end = 'F'); // ["A", "B", "C", "D", "E", "F"]