PHP Classes

File: Collection.php

Recommend this page to a friend!
  Classes of mohammad anzawi   PHP Collection to Array   Collection.php   Download  
File: Collection.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Collection to Array
Manipulate a collection of values using arrays
Author: By
Last change:
Date: 2 years ago
Size: 1,202 bytes
 

Contents

Class file image Download
<?php

class Collection implements Countable, IteratorAggregate
{
    protected
$items = [];

    public function
__construct($items = [])
    {
       
$this->items = $items;
    }

    public function
__toString()
    {
        return
$this->toJson();
    }

    public function
all()
    {
        return
$this->items;
    }

    public function
first()
    {
        return isset(
$this->items[0]) ? $this->items[0] : [];
    }

    public function
last()
    {
       
$results = array_reverse($this->items);
        return isset(
$results[0]) ? $results[0] : [];
    }

    public function
count()
    {
        return
count($this->items);
    }

    public function
each($callback)
    {
        foreach(
$this->items as $key => $value)
        {
           
$callback($value, $key);
        }

        return
$this;
    }

    public function
filter($callback = null)
    {
        if(
$callback)
        {
            return new static(
array_filter($this->items, $callback));
        }

        return new static(
array_filter($this->items));
    }

    public function
map($callback)
    {
       
$keys = array_keys($this->items);

       
$items = array_map($callback, $this->items, $keys);

        return new static(
array_combine($keys, $items));
    }

    public function
toJson()
    {
        return
json_encode($this->items);
    }

    public function
getIterator()
    {
        return new
ArrayIterator($this->items);
    }
}