<commit_before><?php namespace Halaei\Presenter;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Contracts\JsonableInterface;
use ArrayAccess;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use ArrayIterator;
class PresenterCollection implements ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface, JsonSerializable
{
    /**
     * @var Collection
     */
    protected $collection;
    protected $callables;
    protected $friend;


    function __construct(Collection $collection, $callables = [], $friend = null)
    {
        $this->collection = $collection;
        $this->callables = $callables;
        $this->friend = $friend;
    }

    function toPresenterArray()
    {
        $array = [];
        foreach ($this->collection as $key => $model) {
            array_push($array, $model->present($this->callables, $this->friend));
        }
        return $array;
    }

    function __call($name, $arguments)
    {
        $result = call_user_func_array([$this->collection, $name], $arguments);
        if($result === $this->collection)
            return $this;
        elseif($result instanceof Collection)
            return new static($result, $this->callables, $this->friend);
        elseif($result instanceof Model)
            return $result->present($this->callables, $this->friend);
        elseif(is_array($result))
        {
            foreach ($result as &$item) {
                if($item instanceof Model)
                    $item = $item->present($this->callables, $this->friend);
            }
            unset($item);
            return $result;
        }
        return $result;
    }

    //Methods of ArrayAccess
    public function offsetExists($offset)
    {
        return $this->collection->offsetExists($offset);
    }

    public function offsetGet($offset)
    {
        return $this->collection->offsetGet($offset)->present();
    }

    public function offsetSet($offset, $value)
    {
        return $this->collection->offsetSet($offset, $value);
    }

    public function offsetUnset($offset)
    {
        return $this->collection->offsetUnset($offset);
    }

    //Method of Countable
    public function count()
    {
        return $this->collection->count();
    }

    //Method of IteratorAggregate
    public function getIterator()
    {
        return new ArrayIterator($this->toPresenterArray());
    }

    //Method of ArrayableInterface
    public function toArray()
    {
        return $this->toArray();
    }

    //Method of JsonableInterface
    public function toJson($options = 0)
    {
        return $this->collection->toJson();
    }

    //Method of JsonSerializable
    function jsonSerialize()
    {<commit_msg>fix a bug in PresenterCollection::toArray()<commit_after><?php namespace Halaei\Presenter;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Support\Contracts\JsonableInterface;
use ArrayAccess;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use ArrayIterator;
class PresenterCollection implements ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface, JsonSerializable
{
    /**
     * @var Collection
     */
    protected $collection;
    protected $callables;
    protected $friend;


    function __construct(Collection $collection, $callables = [], $friend = null)
    {
        $this->collection = $collection;
        $this->callables = $callables;
        $this->friend = $friend;
    }

    function toPresenterArray()
    {
        $array = [];
        foreach ($this->collection as $key => $model) {
            array_push($array, $model->present($this->callables, $this->friend));
        }
        return $array;
    }

    function __call($name, $arguments)
    {
        $result = call_user_func_array([$this->collection, $name], $arguments);
        if($result === $this->collection)
            return $this;
        elseif($result instanceof Collection)
            return new static($result, $this->callables, $this->friend);
        elseif($result instanceof Model)
            return $result->present($this->callables, $this->friend);
        elseif(is_array($result))
        {
            foreach ($result as &$item) {
                if($item instanceof Model)
                    $item = $item->present($this->callables, $this->friend);
            }
            unset($item);
            return $result;
        }
        return $result;
    }

    //Methods of ArrayAccess
    public function offsetExists($offset)
    {
        return $this->collection->offsetExists($offset);
    }

    public function offsetGet($offset)
    {
        return $this->collection->offsetGet($offset)->present();
    }

    public function offsetSet($offset, $value)
    {
        return $this->collection->offsetSet($offset, $value);
    }

    public function offsetUnset($offset)
    {
        return $this->collection->offsetUnset($offset);
    }

    //Method of Countable
    public function count()
    {
        return $this->collection->count();
    }

    //Method of IteratorAggregate
    public function getIterator()
    {
        return new ArrayIterator($this->toPresenterArray());
    }

    //Method of ArrayableInterface
    public function toArray()
    {
        return $this->collection->toArray();
    }

    //Method of JsonableInterface
    public function toJson($options = 0)
    {
        return $this->collection->toJson();
    }

    //Method of JsonSerializable
    function jsonSerialize()
    {
{'fraction_non_alphanumeric': 0.12242042672263029, 'fraction_numerical': 0.0003497726477789437, 'mean_word_length': 2.59685534591195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3510923', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1540, 'n_words': 340}