Skip to content

Latest commit

 

History

History
219 lines (142 loc) · 3.29 KB

README.md

File metadata and controls

219 lines (142 loc) · 3.29 KB

Steein Collections

Collection library for working with data.

Using Collection

use Steein\Common\Collections\Collection;

//Generated default array
$array = [
    'name'      =>  'Shamsudin',
    'project'   =>  'Steein',
    'text'      =>  'default text',

    'Cars'  =>  [
        'car_audi' =>  'Audi',
        'car_bwm'  =>  'Bmw'
    ]
];

//Default
$collection = new Collection($array);

//Or Static implementation
$collection = Collection::instance($array);

Available methods and implementation methods

In this documentation we will describe the most necessary methods, and all the rest, and you can see a lot of them in the code, everything is described there


The magic method for creating and retrieving a new item for the collection

$collection->new_array = 'value';

toArray()

Getting the collections of elements in the Array format.

$collection->toArray()

toJson()

Getting the collections of elements in the Json format.

$collection->toJson();

toXml()

Getting the collections of elements in the Json format.

$collection->toXml();

set()

New item for collection

$collection->set('key', 'value');

get()

Get the item from the collection by key.

$collection->get('key');

slice()

Create the main array of the collection.

$collection->slice(1);

nth()

Create a new collection of each n-th attribute.

$collection->nth(2);

union()

Union of collections with these elements.

$new_collection = Collection::instance([0 => ['a'], 1 => ['b'], 2 => ['c']]);
$new_collection->union([3 => ['b'], 2 => ['c']]);

combine()

Create a collection using this collection for keys, and the other for its values.

$new_collection = Collection::instance(['firstname','lastname','age']);
$new_collection->combine(['Shamsudin','Serderov', 22]);

merge()

Combine the collection with these elements.

$new_collection = Collection::instance(['id_goods' => 100,'price' => 10000, 'instock' => 0]);
$new_collection->merge(['price' => 9550, 'count' => 1]);

isEmpty()

Determine whether the collection is empty or not.

$collection->isEmpty();

isNotEmpty()

Determine if the collection is empty.

$collection->isNotEmpty();

has()

Determine if an element exists in the collection by key.

$collection->has('project');

forget()

Remove an item from the collection by key.

$collection->forget('name');

archive()

Archive the collection with one or more arrays.

$new_collection = Collection::instance(['test', 'test1']);
$new_collection->archive([50, 22]);

clear()

Clearing Attributes in the Collection

$collection->clear();

count()

Count the number of items in the collection.

$collection->count()

flip()

We turn the elements into collections.

$flip = $collection->flip();
$flip->all();

values()

Get values from items in the collection

$collection->values();

keys()

Get the keys to the attributes of the collection.

$collection->keys();

indexOf()

We search for a specific value in the collection

$collection->indexOf('Shamsudin');

all()

Get all attributes

$collection->all();