ArrayDatasource: Add missing and fix existing documentation

This commit is contained in:
Johannes Meyer 2015-05-06 09:12:48 +02:00
parent 9c799dca22
commit d63381c002

View File

@ -8,8 +8,18 @@ use Icinga\Data\SimpleQuery;
class ArrayDatasource implements Selectable class ArrayDatasource implements Selectable
{ {
/**
* The array being used as data source
*
* @var array
*/
protected $data; protected $data;
/**
* The current result
*
* @var array
*/
protected $result; protected $result;
/** /**
@ -23,13 +33,13 @@ class ArrayDatasource implements Selectable
protected $keyColumn; protected $keyColumn;
/** /**
* Constructor, create a new Datasource for the given Array * Create a new data source for the given array
* *
* @param array $array The array you're going to use as a data source * @param array $data The array you're going to use as a data source
*/ */
public function __construct(array $array) public function __construct(array $data)
{ {
$this->data = (array) $array; $this->data = $data;
} }
/** /**
@ -56,7 +66,7 @@ class ArrayDatasource implements Selectable
} }
/** /**
* Instantiate a Query object * Provide a query for this data source
* *
* @return SimpleQuery * @return SimpleQuery
*/ */
@ -65,6 +75,13 @@ class ArrayDatasource implements Selectable
return new SimpleQuery($this); return new SimpleQuery($this);
} }
/**
* Fetch and return a column of all rows of the result set as an array
*
* @param SimpleQuery $query
*
* @return array
*/
public function fetchColumn(SimpleQuery $query) public function fetchColumn(SimpleQuery $query)
{ {
$result = array(); $result = array();
@ -75,6 +92,13 @@ class ArrayDatasource implements Selectable
return $result; return $result;
} }
/**
* Fetch and return all rows of the given query's result as a flattened key/value based array
*
* @param SimpleQuery $query
*
* @return array
*/
public function fetchPairs(SimpleQuery $query) public function fetchPairs(SimpleQuery $query)
{ {
$result = array(); $result = array();
@ -91,6 +115,13 @@ class ArrayDatasource implements Selectable
return $result; return $result;
} }
/**
* Fetch and return the first row of the given query's result
*
* @param SimpleQuery $query
*
* @return object|false The row or false in case the result is empty
*/
public function fetchRow(SimpleQuery $query) public function fetchRow(SimpleQuery $query)
{ {
$result = $this->getResult($query); $result = $this->getResult($query);
@ -100,17 +131,38 @@ class ArrayDatasource implements Selectable
return $result[0]; return $result[0];
} }
/**
* Fetch and return all rows of the given query's result as an array
*
* @param SimpleQuery $query
*
* @return array
*/
public function fetchAll(SimpleQuery $query) public function fetchAll(SimpleQuery $query)
{ {
return $this->getResult($query); return $this->getResult($query);
} }
/**
* Count all rows of the given query's result
*
* @param SimpleQuery $query
*
* @return int
*/
public function count(SimpleQuery $query) public function count(SimpleQuery $query)
{ {
$this->createResult($query); $this->createResult($query);
return count($this->result); return count($this->result);
} }
/**
* Create the result for the given query, in case there is none yet
*
* @param SimpleQuery $query
*
* @return $this
*/
protected function createResult(SimpleQuery $query) protected function createResult(SimpleQuery $query)
{ {
if ($this->hasResult()) { if ($this->hasResult()) {
@ -168,7 +220,14 @@ class ArrayDatasource implements Selectable
return $this; return $this;
} }
protected function getLimitedResult($query) /**
* Apply the limit, if any, of the given query to the current result and return the result
*
* @param SimpleQuery $query
*
* @return array
*/
protected function getLimitedResult(SimpleQuery $query)
{ {
if ($query->hasLimit()) { if ($query->hasLimit()) {
if ($query->hasOffset()) { if ($query->hasOffset()) {
@ -182,16 +241,36 @@ class ArrayDatasource implements Selectable
} }
} }
/**
* Return whether a query result exists
*
* @return bool
*/
protected function hasResult() protected function hasResult()
{ {
return $this->result !== null; return $this->result !== null;
} }
protected function setResult($result) /**
* Set the current result
*
* @param array $result
*
* @return $this
*/
protected function setResult(array $result)
{ {
return $this->result = $result; $this->result = $result;
return $this;
} }
/**
* Return the result for the given query
*
* @param SimpleQuery $query
*
* @return array
*/
protected function getResult(SimpleQuery $query) protected function getResult(SimpleQuery $query)
{ {
if (! $this->hasResult()) { if (! $this->hasResult()) {