Cygnite - A Modern Toolkit For Web Developers

The Elegant Way of Building Full-Featured Web Applications

Cygnite Dynamic Routing

Collections


Documentation

Introduction

Working with arrays of data is now more convenient and fun. Cygnite provides a fluent wrapper class for array data manipulation. Collection class provides various methods. By default, Cyrus ActiveRecord models uses Collection class and returns Collection instance. However, feel free to use Collection class wherever applicable in your application.

Creating Collections

Creating a new Collection is very simple. You can create a Collection different ways, using helper function or create method or using new keyword as below.



 use Cygnite\Foundation\Collection; 

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

 $collection = Collection::create([1,2,4]);
 
 $collection = collection([1,2,4]);


Using helper function or create method allows you method chaining.

List of Collections Methods

Most of the Collection method returns a new Collection instance. Below are list of methods available in Collection class.

  • all
  • asArray
  • asJson
  • count
  • getIterator
  • serialize
  • unserialize
  • filter
  • flip
  • remove
  • get
  • each
  • keys
  • map
  • getCachingIterator
  • has
  • isEmpty
  • merge
  • unique
  • sort
  • shift
  • prepend
  • first
  • firstKey
  • last
  • search
  • reverse

We will discuss all available methods separately.

all()

The all method simply returns the given collection array.



 $collection = Collection::create(['1', '2','4']);
 $collection->all(); // [1, 2, 3]

 collection(['1', '2','4'])->all(); // [1, 2, 3]


asArray()

The asArray method simply returns the given array of collection.



 collection(['1', '2','4'])->asArray(); // [1, 2, 3]


asJson()

The asJson method returns the given array as json object.



 collection(['id' => '1', 'name' => 'Joe'])->asJson(); // '{"id": 1,"name":"Joe"}'


count()

The count method returns the total number of element in the Collection.



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

 $collection->count(); // 3


getIterator()

The getIterator method helps to iterate over ArrayIterator result set object.



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

 foreach ($collection->getIterator() as $key => $value) {


 }


serialize()

The serialize method helps to serialize data.



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

 $serializedData = $collection->serialize();


unserialize()

The unserialize method helps to unserialize serialized data.



 $collection = collection();

 $collection->unserialize($serializedData);


filter()

The filter method helps to .



 $collection = collection(['admin', 'guest']);

 $data = $collection->filter(function ($value) {
    return $value == 'admin';
 });

 $data->all(); // [0 => 'admin']


flip()

The flip method use to swaps the Collection's keys with their values.



 $collection = Collection::create(['Language' => 'PHP', 'Framework' => 'Cygnite']);

 $flip = $collection->flip();

 $flip->all(); // ['PHP' => 'Language', 'Cygnite' => 'Framework']


remove()

The remove method removes a element from the Collection using its key.



 $collection = Collection::create(['Language' => 'PHP', 'Framework' => 'Cygnite']);

 $collection->remove('Language');

 $collection->all(); // ['Framework' => 'Cygnite']


get()

The get method returns a row from the collection by key. If key doesn't found will return default value as null.



 $collection = Collection::create(['Language' => 'PHP', 'Framework' => 'Cygnite']);

 $collection->remove('Framework');

 $collection->all(); // Cygnite


each()

The each method iterate over each elements of collection and apply callback over each elements.



 $collection = Collection::create(['PHP', 'Cygnite']);

 $collection = $collection->each(function ($element)
 {
    if ($element == 'Cygnite') {
       return true;
    }
     
 });


keys()

The keys method returns all keys of the collection.



 $collection = Collection::create(['PHP', 'Cygnite']);

 $keys = $collection->keys();

 $keys->all(); // [0, 1] 


map()

The map method iterate over collection items and passes each values to given callback. It doesn't modify collection, however it can modify the collection items and returns new collection instance.



 $collection = Collection::create([3, 5, 7]);

 $data = $collection->map(function ($element, $key)
 {
    if ($element > 5) {
        return $element * 2;
    }

    return $element;
 });

 $data->all(); // [3,5,14]


getCachingIterator()

The map method iterate over collection items and returns CachingIterator instance.



 $collection = Collection::create([3, 5, 7]);

 $collection->getCachingIterator(); //CachingIterator Object()

 // 3, 5,7
 foreach ($collection->getCachingIterator() as $key => $value) {
 
     show($value);
 }
 

has()

The has method checks key exists in the collection and returns true/false.



 $collection = Collection::create(['name' => 'cygnite', 'email' => 'sanjoy@cygniteframework.com']);

 $collection->has('name'); // true


isEmpty()

The isEmpty method determine is collection empty. It returns true if collection empty otherwise false.



 $collection = Collection::create([]);

 $collection->isEmpty(); // true


merge()

The merge method merge given array with the collection.



 $collection = Collection::create(['product' => 'Apple Iphone', 'price' => 1200]);

 $merged = $collection->merge(['manufactured_by' => 'Apple Inc.']);

 $merged->all(); // ['product' => 'Apple Iphone', 'price' => 1200, 'manufactured_by' => 'Apple Inc.']


unique()

The unique method returns unique values from the collection.



 $collection = Collection::create([12, 65, 12, 75, 75]);

 $unique = $collection->unique();

 $unique->all(); // [12,65,75]


sort()

The sort method sorts the collection.



 $collection = Collection::create([3, 2, 5, 6, 1]);

 $sorted = $collection->sort(function ($a, $b)
 {
    if ($a == $b) {
        return 0;
    }

    return ($a < $b) ? -1 : 1;    
 });

 $sorted->all(); // [1,2,3,5,6]


shift()

The shift method remove the first element from the collection and returns collection instance.



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

 $shifted = $collection->shift();

 $shifted->all(); // [3, 2, 5, 4]


prepend()

The prepend method prepend one or more elements to the beginning of an array collection.



 $collection = Collection::create([2, 3, 4]);

 $prepended = $collection->prepend(1);

 $prepended->all(); // [1,2, 3, 4]


first()

The first method return first key of array if collection not empty.



 $collection = Collection::create([2, 3, 4]);

 $collection->first(); // 2


The first method returns default value if empty array passes into collection.



 $collection = Collection::create([]);

 $collection->first(5); // 5


firstKey()

The firstKey method returns first key of array if collection not empty.



 $collection = Collection::create([2, 3, 4]);

 $collection->firstKey(); // 0


last()

The last method returns last element of array if collection not empty.



 $collection = Collection::create([2, 3, 4]);

 $collection->last(); // 4


search()

The search method searches the collection for a given value and return the key if found, otherwise returns false.



 $collection = Collection::create([2, 3, 4]);

 $collection->search(3); // 1


To use strict comparison, pass true as the second argument to the search method.



 $collection = Collection::create([2, 3, 4]);

 $collection->search('3', true); // false


reverse()

The reverse method reverses the order of the collection.



 $collection = Collection::create([2, 3, 4]);

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


Follow Us On Facebook Twitter Google+ Linkedin
Released Under The MIT Public License. Copyrights @2012-2017. Powered by- Sanjoy Dey Productions.