<?php
namespace ModulesAbstract\PluginReports\Report\Model\Exporter\RowsStorage;

/**
 * Store the rows in memory, temporarily
 * @author Lucian Sirbu
 */
class LocalMemory extends RowsStorageAbstract
{
	protected $rows= [];

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Exporter\RowsStorage\RowsStorageAbstract::appendRow()
	 */
	public function appendRow($row)
	{
		$report = $row['_report'];
		unset($row['_report']);

		$this->rows[$report][] = $row;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Exporter\RowsStorage\RowsStorageAbstract::getTypes()
	 */
	public function getTypes()
	{
		return array_keys((array)$this->rows);
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Exporter\RowsStorage\RowsStorageAbstract::countByType()
	 */
	public function countByType($report)
	{
		return count((array)$this->rows[$report]);
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Exporter\RowsStorage\RowsStorageAbstract::getAllByType()
	 */
	public function getAllByType($report)
	{
		return (array)$this->rows[$report];
	}

	/**
	 * (non-PHPdoc)
	 * @see \ModulesAbstract\PluginReports\Report\Model\Exporter\RowsStorage\RowsStorageAbstract::getFirstRowByType()
	 */
	public function getFirstRowByType($report)
	{
		if ($this->rows[$report]) {
			return reset($this->rows[$report]);
		}
		return [];
	}
}
