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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
<?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\Model;
use ICanBoogie\ActiveRecord\Query;
/**
* Fetch records from a model.
*
* @property-read Model $model The model from which record are fetched.
* @property-read CriterionList $criterion_list List of criterion.
* @property-read array $modifiers An array of key/value used to filter/order/qualify the records.
* @property-read Query $initial_query The initial query, before it is altered by the criteria,
* conditions, order or limit.
* @property-read Query $query The query used to fetch the records.
* @property-read QueryString $query_string A {@link QueryString} instance resolved from the `q`
* modifier.
* @property-read array $conditions An array of conditions used to filter the fetched records.
* @property-read string $order The order in which records are fetched, as defined by the `order`
* modifier.
* @property-read int $count The number of records matching the query before the offset and limit
* is applied.
* @property-read int $limit The maximum number of records that can be fetched, as defined by the
* `limit` modifier.
*/
class Fetcher implements FetcherInterface
{
use AccessorTrait;
use FetcherTrait;
/**
* The model from witch records are fetched.
*
* @var Model
*/
protected $model;
/**
* Return the {@link $model} property.
*
* @return Model
*/
protected function get_model()
{
return $this->model;
}
/**
* Fetch modifiers.
*
* @var array
*/
protected $modifiers;
/**
* Return the {@link $modifiers} property.
*
* @return array
*/
protected function get_modifiers()
{
return $this->modifiers;
}
/**
* Options.
*
* @var array
*/
protected $options = [];
/**
* Initial query.
*
* @var Query
*/
private $initial_query;
/**
* Return the {@link $initial_query} property.
*
* @return Query
*/
protected function get_initial_query()
{
if (empty($this->initial_query))
{
$this->initial_query = $this->create_initial_query();
}
return $this->initial_query;
}
/**
* The query used to fetch the records.
*
* @var Query
*/
private $query;
/**
* Return the query used to fetch the records.
*
* @return Query
*/
protected function get_query()
{
return $this->query;
}
/**
* Query string resolved from the modifiers.
*
* @var QueryString
*/
protected $query_string;
/**
* Return the {@link $query_string} property.
*
* @return QueryString
*/
protected function get_query_string()
{
return $this->query_string;
}
/**
* Conditions resolved from the modifiers.
*
* @var array
*/
protected $conditions = [];
/**
* Return the {@link $conditions} property.
*
* @return array
*/
protected function get_conditions()
{
return $this->conditions;
}
/**
* Order of the records, as found in the modifiers.
*
* @var string|null
*/
protected $order;
/**
* Return the {@link $order} property.
*
* @return string|null
*/
protected function get_order()
{
return $this->order;
}
/**
* Limit of the number of records to fetch, as found in the modifiers.
*
* @var string|null
*/
protected $limit;
/**
* Return the {@link $limit} property.
*
* @return int|null
*/
protected function get_limit()
{
return $this->limit;
}
protected $offset;
protected function get_offset()
{
return $this->offset;
}
protected function get_page()
{
$limit = $this->limit;
if (!$limit)
{
return 0;
}
return (int) ($this->offset / $this->limit);
}
/**
* Number of records matching the query, before they are limited.
*
* @var int
*/
protected $count;
/**
* Return the {@link $count} property.
*
* @return int
*/
protected function get_count()
{
return $this->count;
}
/**
* Initializes the {@link $model}, {@link $options} and {@link $criterion_list} properties.
*
* @param Model|ModelBindings $model
* @param array $options
*/
public function __construct(Model $model, array $options = [])
{
$this->model = $model;
$this->options = $options;
$this->criterion_list = $this->alter_criterion_list($model->criterion_list);
}
/**
* Clones the {@link initial_query}, {@link query}, and {@link query_string} properties.
*/
public function __clone()
{
$this->initial_query = clone $this->initial_query;
$this->query = clone $this->query;
$this->query_string = clone $this->query_string;
}
/**
* Fetch records according to the specified modifiers.
*
* The method updates the following properties:
*
* - {@link $conditions}
* - {@link $count}
* - {@link $initial_query}
* - {@link $limit}
* - {@link $modifiers}
* - {@link $offset}
* - {@link $order}
* - {@link $query_string}
*
* @param array $modifiers
*
* @return array The records matching the query.
*/
public function __invoke(array $modifiers)
{
$this->modifiers = $modifiers;
list($conditions, $properties) = $this->parse_modifiers($modifiers);
$this->conditions = $conditions;
foreach ($properties as $property => $value)
{
$this->$property = $value;
}
#
$query = clone $this->get_initial_query();
$query = $this->alter_query($query);
$query = $this->alter_query_with_conditions($query, $conditions);
$this->count = $this->count_records($query);
$query = $this->alter_query_with_order($query, $this->order);
$query = $this->alter_query_with_limit($query, $this->offset, $this->limit);
$this->query = $query;
$records = $this->fetch_records($query);
$this->alter_records($records);
return new RecordCollection($records, clone $this);
}
/**
* Create the initial query.
*
* @return Query
*/
protected function create_initial_query()
{
return new Query($this->model);
}
/**
* Parse modifiers to extract conditions, and qualifiers.
*
* @param array $modifiers
*
* @return array
*/
protected function parse_modifiers(array $modifiers)
{
$modifiers += [
'order' => null,
'limit' => null,
'page' => null,
'q' => null
];
$order = $modifiers['order'];
$limit = $modifiers['limit'];
$offset = $limit ? $modifiers['page'] * $limit : null;
$query_string = $this->parse_query_string($modifiers['q']);
$matches = $query_string->matches;
if ($matches)
{
$modifiers += array_map(function($v) { return implode('|', $v); }, $matches);
}
$conditions = [];
$this->alter_conditions($conditions, $modifiers);
return [ $conditions, [
'order' => $order,
'limit' => $limit,
'offset' => $offset,
'query_string' => $query_string
] ];
}
}