1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\Facets;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\ActiveRecord;
/**
* A collection of records fetched by a {@link Fetcher} instance.
*
* @property-read Fetcher $fetcher
* @property-read array $conditions The conditions used to fetch the records.
* @property-read int $limit The maximum number of records.
* @property-read int $page The current page.
* @property-read int $total_count The number of records matching the query, without range
* limitation.
* @property-read ActiveRecord\Query $initial_query
* @property-read ActiveRecord\Query $query
* @property-read ActiveRecord $one The first record in the collection.
*/
class RecordCollection implements \IteratorAggregate, \Countable
{
use AccessorTrait;
/**
* Properties forwarded to the {@link Fetcher} instance.
*
* @var array
*/
static private $forwarded_properties = [ 'conditions', 'initial_query', 'limit', 'page', 'query' ];
/**
* @var ActiveRecord[]
*/
private $records;
/**
* @var Fetcher
*/
private $fetcher;
protected function get_fetcher()
{
return $this->fetcher;
}
/**
* Returns the first record in the collection.
*
* @return ActiveRecord
*/
protected function get_one()
{
return reset($this->records);
}
/**
* Returns the number of records matching the query, without range limitation.
*
* @return int
*/
protected function get_total_count()
{
return $this->fetcher->count;
}
public function __construct(array $records, Fetcher $fetcher)
{
$this->records = $records;
$this->fetcher = $fetcher;
}
public function __get($property)
{
if (in_array($property, self::$forwarded_properties))
{
return $this->fetcher->$property;
}
return $this->accessor_get($property);
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->records);
}
/**
* @inheritdoc
*/
public function count()
{
return count($this->records);
}
}