PK u]AV33"H H phpunit.xml.distnu W+A
./tests
.
vendor/
PK u]AV~Q Q lib/Row.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DTL\DataTable;
use DTL\DataTable\Cell;
/**
* Represents a table row.
*
* @author Daniel Leech
*/
class Row extends Aggregated
{
/**
* @var Row[]
*/
private $cells;
/**
* @var string[]
*/
private $groups;
/**
* @param array $cells
*/
public function __construct(array $cells = array(), array $groups = array())
{
$this->cells = $cells;
$this->groups = $groups;
}
/**
* {@inheritDoc}
*/
public function getGroups()
{
return $this->groups;
}
/**
* Return all column names.
*
* @return array
*/
public function getColumnNames(array $groups = array())
{
return array_keys($this->getCells($groups));
}
/**
* Return the cell at the given column.
*
* @param int $column
*
* @throws \OutOfBoundsException
*
* @return Cell
*/
public function getCell($column)
{
if (!array_key_exists($column, $this->cells)) {
throw new \OutOfBoundsException(sprintf(
'No cell exists at column "%s", known columns: "%s"',
$column, implode('", "', array_keys($this->cells))
));
}
return $this->cells[$column];
}
/**
* {@inheritDoc}
*/
public function getCells(array $groups = array())
{
if (empty($groups)) {
return $this->cells;
}
return array_filter($this->cells, function (Cell $cell) use ($groups) {
foreach ($groups as $group) {
if (in_array($group, $cell->getGroups())) {
return true;
}
}
return false;
});
}
/**
* Return an array representation of this row.
*
* @param array $groups
*
* @return array
*/
public function toArray(array $groups = array())
{
$result = array();
foreach ($this->values($groups) as $column => $value) {
$result[$column] = $value;
}
return $result;
}
}
PK u]AV,jrU. . lib/Cell.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DTL\DataTable;
/**
* Represents a data table cell.
*
* @author Daniel Leech
*/
class Cell implements AggregateableInterface
{
/**
* @var array
*/
private $groups;
/**
* @var mixed
*/
private $value;
/**
* @param mixed $value
* @param array $groups
*/
public function __construct($value, array $groups = array())
{
$this->value = $value;
$this->groups = $groups;
}
/**
* {@inheritDoc}
*/
public function getGroups()
{
return $this->groups;
}
/**
* Set the value for this cell.
*
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
}
/**
* Return this cell instances value.
*
* @return mixed
*/
public function value()
{
return $this->value;
}
/**
* {@inheritDoc}
*/
public function sum(array $groups = array())
{
return $this->value;
}
/**
* {@inheritDoc}
*/
public function min(array $groups = array())
{
return $this->value;
}
/**
* {@inheritDoc}
*/
public function max(array $groups = array())
{
return $this->value;
}
/**
* {@inheritDoc}
*/
public function avg(array $groups = array())
{
return $this->value;
}
/**
* {@inheritDoc}
*/
public function median(array $groups = array(), $ceil = false)
{
return $this->value;
}
/**
* {@inheritDoc}
*/
public function values(array $groups = array())
{
return array($this->value);
}
/**
* {@inheritDoc}
*/
public function fill($value, array $groups = array())
{
$this->value = $value;
}
/**
* {@inheritDpc}.
*/
public function map(\Closure $closure, array $groups = array())
{
$closure($this);
}
/**
* {@inheritDoc}
*/
public function getCells()
{
return array($this);
}
}
PK u]AV\3A A lib/Aggregated.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DTL\DataTable;
/**
* Abstract class for Table and Row and Column instances.
*
* @author Daniel Leech
*/
abstract class Aggregated implements AggregateableInterface
{
/**
* {@inheritDoc}
*/
public function sum(array $groups = array())
{
return AggregateHelper::sum($this->values($groups));
}
/**
* {@inheritDoc}
*/
public function min(array $groups = array())
{
return AggregateHelper::min($this->values($groups));
}
/**
* {@inheritDoc}
*/
public function max(array $groups = array())
{
return AggregateHelper::max($this->values($groups));
}
/**
* {@inheritDoc}
*/
public function avg(array $groups = array())
{
return AggregateHelper::avg($this->values($groups));
}
/**
* {@inheritDoc}
*/
public function median(array $groups = array(), $ceil = false)
{
return AggregateHelper::median($this->values($groups), $ceil);
}
/**
* {@inheritDoc}
*/
public function values(array $groups = array())
{
$values = array();
foreach ($this->getCells($groups) as $column => $cell) {
$values[$column] = $cell->value();
}
return $values;
}
/**
* {@inheritDoc}
*/
public function fill($value, array $groups = array())
{
foreach ($this->getCells($groups) as $cell) {
$cell->setValue($value);
}
}
/**
* {@inheritDoc}
*/
public function map(\Closure $closure, array $groups = array())
{
foreach ($this->getCells($groups) as $cell) {
$cell->setValue($closure($cell));
}
}
abstract public function getCells(array $groups = array());
abstract public function toArray(array $groups = array());
}
PK u]AVt_ _ lib/Builder/TableBuilder.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DTL\DataTable\Builder;
use DTL\DataTable\Table;
use DTL\DataTable\Row;
class TableBuilder
{
/**
* @var RowBuilder[]
*/
private $rows = array();
/**
* @param Row[] $rows
*/
public function __construct(array $rows = array())
{
foreach ($rows as $row) {
$this->rows[] = RowBuilder::create($this, $row->getCells(), $row->getGroups());
}
}
/**
* Create a new table builder.
*
* An array of Row instances can be passed, these will be converted into
* RowBuilder instances and the cells will be cloned.
*
* @param Row[] $rows
*
* @return TableBuilder
*/
public static function create(array $rows = array())
{
return new self($rows);
}
/**
* Create a new RowBuilder and return it.
*
* @return RowBuilder
*/
public function row(array $groups = array())
{
$builder = new RowBuilder($this, array(), $groups);
$this->rows[] = $builder;
return $builder;
}
/**
* Add a row builder, returns this instance.
*
* @return TableBuilder
*/
public function addRow(RowBuilder $row)
{
$this->rows[] = $row;
return $this;
}
/**
* Return all of the row builders.
*
* @return RowBuilder[]
*/
public function getRows()
{
return $this->rows;
}
/**
* Create a new Table.
*
* @return Table
*/
public function getTable()
{
$this->finalize();
$rows = array();
foreach ($this->rows as $row) {
$rows[] = $row->getRow();
}
return new Table($rows);
}
/**
* Fill in any empty cells with NULL.
*/
protected function finalize()
{
$columnNames = array();
foreach ($this->rows as $rowBuilder) {
foreach ($rowBuilder->getColumnNames() as $columnName) {
$columnNames[$columnName] = $columnName;
}
}
foreach ($this->rows as $rowBuilder) {
$rowBuilder->order($columnNames);
}
}
}
PK u]AV