/phalcon-collection

PhalconPHP library for work with arrays and collections

Primary LanguagePHP

Todo List

Register service:

    $di = new DI();
    $di->set('collection', function() {
        return new IzicaCollection();
    });

Usage:

    use Phalcon\Mvc\Controller;

    class IndexController extends Controller
    {
        public function indexAction()
        {
            $arCars = Cars::find();
            $jsonResponse = $this->collection->create($arCars)->groupBy('year')->toJson();
        }
    }

Functions

average

    $arr = [
        ['value' => 2, 'value2' => 4, 'value3' => 'foo'],
        ['value' => 6, 'value2' => 1, 'value3' => 'bar']
    ];
    $arResult = $this->collection->create($arr)->average();
    //Array
    //(
    //    [value] => 4
    //    [value2] => 2.5
    //)
    $arResult = $this->collection->create($arr)->average('value');
    //4
    $collection = $this->collection->create($arr)->average(['value']);
    //Array
    //(
    //    [value] => 4
    //)
    $collection = $this->collection->create([1, 5, 6 ,7])->average();
    //4.75

chunk

    $collection = $this->collection->create([1, 2, 3, 4, 5])->chunk(2);
    //[
    //    [1, 2], 
    //    [3, 4]
    //    [5]
    //]

groupBy

    $arResult = $this->collection->create($arr)->groupBy('value');
    $arResult = $this->collection->create($arr)->groupBy(function($arItem){
        return (int)$arItem['value'] * 2;
    });

keyBy

    $arResult = $this->collection->create($arr)->keyBy('id');
    $arResult = $this->collection->create($arr)->keyBy(function($arItem){
        return strtoupper($arItem['code']);
    });

has

    //has(key, value);
    $arr = ['value' => 'bar', 'value2' => 'foo'];
    $result = $this->collection->create($arr)->has('value'); // true
    $result = $this->collection->create($arr)->has('value', 'bar'); // true
    $result = $this->collection->create($arr)->has('value', 'xyz'); // false

toArray

transforms activeRecord to Array

    $arResult = $this->collection->create(Cars::find())->toArray()->groupBy('value'); // return result in Array Object after groupBy
    $arResult = $this->collection->create(Cars::find())->toArray(true) // return result in Array string instant, ~Cars::find()->toArray();

toJson

    $arResult = $this->collection->create($arr)->toJson()->groupBy('value'); // return result in JSON string after groupBy
    $arResult = $this->collection->create($arr)->toJson(true) // return result in JSON string instant