<?php
/**
 * Created by PhpStorm.
 * User: daedeloth
 * Date: 30/11/14
 * Time: 18:49
 */

namespace Neuron\Collections;
use Neuron\Interfaces\Model;

/**
 * Class TokenizedCollection
 *
 * @package Neuron\Collections
 */
class ModelCollection
	extends Collection {

	private $map = array ();

	public function __construct ()
	{
		$this->on ('add', array ($this, 'onAdd'));
		$this->on ('set', array ($this, 'onAdd'));
		$this->on ('unset', array ($this, 'onUnset'));
	}

	protected function onAdd (Model $model, $offset)
	{
		$this->map[$model->getId ()] = $model;
	}

	protected function onUnset (Model $model = null, $offset = null)
	{
		if ($model)
			unset ($this->map[$model->getId ()]);
	}

	/**
	 * Return all ids.
	 * @return array
	 */
	public function getIds () {
		return array_keys ($this->map);
	}

	public function getFromId ($id)
	{
		if (isset ($this->map[$id]))
		{
			return $this->map[$id];
		}
		return null;
	}
}
<?php
/**
 * Created by PhpStorm.
 * User: daedeloth
 * Date: 30/11/14
 * Time: 18:49
 */

namespace Neuron\Collections;
use Neuron\Interfaces\Model;

/**
 * Class TokenizedCollection
 *
 * @package Neuron\Collections
 */
class ModelCollection
	extends Collection {

	private $map = array ();

	public function __construct ()
	{
		$this->on ('add', array ($this, 'onAdd'));
		$this->on ('set', array ($this, 'onAdd'));
		$this->on ('unset', array ($this, 'onUnset'));
	}

    /**
     * @param Model|null $model
     * @param null $offset
     */
	protected function onAdd (Model $model = null, $offset = null)
	{
		$this->map[$model->getId ()] = $model;
	}

    /**
     * @param Model|null $model
     * @param null $offset
     */
	protected function onUnset (Model $model = null, $offset = null)
	{
		if ($model)
			unset ($this->map[$model->getId ()]);
	}

	/**
	 * Return all ids.
	 * @return array
	 */
	public function getIds ()
    {
		return array_keys ($this->map);
	}

    /**
     * @param $id
     * @return mixed|null
     */
	public function getFromId ($id)
	{
		if (isset ($this->map[$id]))
		{
			return $this->map[$id];
		}
		return null;
	}
}
Allow null values in model collection.
Allow null values in model collection.
