2014-06-02 18:58:49 +04:00
|
|
|
<?php namespace Majestic\Model;
|
2014-03-06 15:26:59 +04:00
|
|
|
|
2016-01-25 08:52:48 +03:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2015-12-25 06:51:24 +03:00
|
|
|
|
2016-01-25 08:52:48 +03:00
|
|
|
class SqlResultCollection extends \ArrayIterator implements iSqlResultItems, Arrayable
|
2014-03-06 15:26:59 +04:00
|
|
|
{
|
|
|
|
private $items;
|
|
|
|
|
|
|
|
public function __construct($items)
|
|
|
|
{
|
|
|
|
$this->items = $items;
|
|
|
|
foreach ($items as $item) {
|
|
|
|
parent::append($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 12:39:44 +04:00
|
|
|
/**
|
|
|
|
* @return DbStatement[]
|
|
|
|
*/
|
2014-03-06 15:26:59 +04:00
|
|
|
public function fetchAll()
|
|
|
|
{
|
2014-03-13 12:39:44 +04:00
|
|
|
return (array) $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fetchField($field)
|
|
|
|
{
|
|
|
|
$item = $this->offsetGet(0);
|
|
|
|
return $item->{$field};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fetch()
|
|
|
|
{
|
|
|
|
return $this->offsetGet(0);
|
2014-03-06 15:26:59 +04:00
|
|
|
}
|
2014-08-12 14:47:47 +04:00
|
|
|
|
|
|
|
public function assoc($field, $assoc_as_array = false) {
|
|
|
|
$sql_result_provider = new SqlResultProvider($this->items);
|
|
|
|
return $sql_result_provider->assoc($field, $assoc_as_array);
|
|
|
|
}
|
2015-12-25 06:51:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the collection of items as a plain array.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
return array_map(function($value)
|
|
|
|
{
|
|
|
|
return $value instanceof ArrayableInterface ? $value->toArray() : $value;
|
|
|
|
|
|
|
|
}, $this->items);
|
|
|
|
}
|
2014-03-06 15:26:59 +04:00
|
|
|
}
|