Elgg  Version 6.3
Repository.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
11 
15 abstract class Repository {
16 
18 
24  public function __construct(array $options = []) {
25  $this->options = new QueryOptions($options, \ArrayObject::STD_PROP_LIST);
26  }
27 
31  public function __get($name) {
32  if (!isset($this->options->$name)) {
33  return;
34  }
35 
36  return $this->options->$name;
37  }
38 
42  public function __set($name, $value) {
43  $this->options->$name = $value;
44  }
45 
49  public function __unset($name) {
50  unset($this->options->$name);
51  }
52 
56  public function __isset($name) {
57  return isset($this->options->$name);
58  }
59 
67  public static function with(array $options = []) {
68  return new static($options);
69  }
70 
76  abstract public function count();
77 
87  abstract public function calculate($function, $property, $property_type = null);
88 
98  abstract public function get($limit = null, $offset = null, $callback = null);
99 
107  public static function find(array $options = []) {
108  return static::with($options)->execute();
109  }
110 
120  public function batch($limit = null, $offset = null, $callback = null) {
121  $options = $this->options->getArrayCopy();
122 
123  $options['limit'] = (int) $limit;
124  $options['offset'] = (int) $offset;
125  $options['callback'] = $callback;
126  unset($options['count'],
127  $options['batch'],
128  $options['batch_size'],
129  $options['batch_inc_offset']
130  );
131 
132  $batch_size = $this->options->batch_size;
133  $batch_inc_offset = $this->options->batch_inc_offset;
134 
135  return new \ElggBatch([static::class, 'find'], $options, null, $batch_size, $batch_inc_offset);
136  }
137 
143  abstract public function execute();
144 
155  public function filter(\Closure $closure) {
156  $this->options->where(new WhereClause($closure));
157 
158  return $this;
159  }
160 
168  public function select($expression) {
169  $this->options->select(new SelectClause($expression));
170 
171  return $this;
172  }
173 
190  public function join($joined_table, $joined_alias = null, $join_column = null, $comparison = null, $values = null, $type = null, $case_sensitive = null) {
191  $join = new JoinClause($joined_table, $joined_alias, function (QueryBuilder $qb, $joined_alias) use ($join_column, $comparison, $values, $type, $case_sensitive) {
192  return $qb->compare("{$joined_alias}.{$join_column}", $comparison, $values, $type, $case_sensitive);
193  });
194  $this->options->join($join);
195 
196  return $this;
197  }
198 
206  public function groupBy($expression) {
207  $this->options->groupBy(new GroupByClause($expression));
208 
209  return $this;
210  }
211 
219  public function having($expression) {
220  $this->options->having(new HavingClause($expression));
221 
222  return $this;
223  }
224 
233  public function orderBy($expression, $direction) {
234  $this->options->orderBy(new OrderByClause($expression, $direction));
235 
236  return $this;
237  }
238 
247  public function expandInto(QueryBuilder $qb, $table_alias = null) {
248  foreach ($this->options->selects as $select_clause) {
249  $select_clause->prepare($qb, $table_alias);
250  }
251 
252  foreach ($this->options->group_by as $group_by_clause) {
253  $group_by_clause->prepare($qb, $table_alias);
254  }
255 
256  foreach ($this->options->having as $having_clause) {
257  $having_clause->prepare($qb, $table_alias);
258  }
259 
260  if (!empty($this->options->order_by)) {
261  foreach ($this->options->order_by as $order_by_clause) {
262  $order_by_clause->prepare($qb, $table_alias);
263  }
264  }
265  }
266 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
$type
Definition: delete.php:21
Extends QueryBuilder with GROUP BY statements.
Extends QueryBuilder with HAVING clauses.
Extends QueryBuilder with JOIN clauses.
Definition: JoinClause.php:11
Extends QueryBuilder with ORDER BY clauses.
Extends QueryBuilder with SELECT clauses.
Builds a clause from closure or composite expression.
Definition: WhereClause.php:11
Database abstraction query builder.
Abstract methods for interfacing with the database.
Definition: Repository.php:15
execute()
Apply correct execution method based on calculation, count or other criteria.
__set($name, $value)
{}
Definition: Repository.php:42
__construct(array $options=[])
Constructor.
Definition: Repository.php:24
filter(\Closure $closure)
Filter query prior to execution Callback function will receive QueryBuilder as the first argument and...
Definition: Repository.php:155
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
Definition: Repository.php:247
orderBy($expression, $direction)
Add ORDER BY.
Definition: Repository.php:233
having($expression)
Add HAVING.
Definition: Repository.php:219
groupBy($expression)
Add GROUP BY.
Definition: Repository.php:206
select($expression)
Add SELECT.
Definition: Repository.php:168
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
Definition: Repository.php:120
calculate($function, $property, $property_type=null)
Apply numeric calculation to a column.
static with(array $options=[])
Constructs a new.
Definition: Repository.php:67
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:107
join($joined_table, $joined_alias=null, $join_column=null, $comparison=null, $values=null, $type=null, $case_sensitive=null)
Add JOIN clause Join a database table on an $x to $y comparison.
Definition: Repository.php:190
$value
Definition: generic.php:51
if(empty($count)) $offset
Definition: pagination.php:26
$limit
Definition: pagination.php:28
$qb
Definition: queue.php:14