Elgg  Version 5.1
Repository.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
12 
16 abstract class Repository {
17 
19 
25  public function __construct(array $options = []) {
26  $this->options = new QueryOptions($options, \ArrayObject::ARRAY_AS_PROPS);
27  }
28 
32  public function __get($name) {
33  if (!isset($this->options->$name)) {
34  return;
35  }
36 
37  $val = &$this->options->$name;
38 
39  return $val;
40  }
41 
45  public function __set($name, $value) {
46  $this->options->$name = $value;
47  }
48 
52  public function __unset($name) {
53  unset($this->options->$name);
54  }
55 
59  public function __isset($name) {
60  return isset($this->options->$name);
61  }
62 
70  public static function with(array $options = []) {
71  return new static($options);
72  }
73 
79  abstract public function count();
80 
90  abstract public function calculate($function, $property, $property_type = null);
91 
101  abstract public function get($limit = null, $offset = null, $callback = null);
102 
110  public static function find(array $options = []) {
111  try {
112  return static::with($options)->execute();
113  } catch (DataFormatException $e) {
114  return elgg_extract('count', $options) ? 0 : false;
115  }
116  }
117 
127  public function batch($limit = null, $offset = null, $callback = null) {
128  $options = $this->options->getArrayCopy();
129 
130  $options['limit'] = (int) $limit;
131  $options['offset'] = (int) $offset;
132  $options['callback'] = $callback;
133  unset($options['count'],
134  $options['batch'],
135  $options['batch_size'],
136  $options['batch_inc_offset']
137  );
138 
139  $batch_size = $this->options->batch_size;
140  $batch_inc_offset = $this->options->batch_inc_offset;
141 
142  return new \ElggBatch([static::class, 'find'], $options, null, $batch_size, $batch_inc_offset);
143  }
144 
150  abstract public function execute();
151 
162  public function filter(\Closure $closure) {
163  $this->options->where(new WhereClause($closure));
164 
165  return $this;
166  }
167 
175  public function select($expression) {
176  $this->options->select(new SelectClause($expression));
177 
178  return $this;
179  }
180 
197  public function join($joined_table, $joined_alias = null, $x = null, $comparison = null, $y = null, $type = null, $case_sensitive = null) {
198  $join = new JoinClause($joined_table, $joined_alias, function (QueryBuilder $qb, $joined_alias) use ($x, $comparison, $y, $type, $case_sensitive) {
199  return $qb->compare("$joined_alias.$x", $comparison, $y, $type, $case_sensitive);
200  });
201  $this->options->join($join);
202 
203  return $this;
204  }
205 
213  public function groupBy($expression) {
214  $this->options->groupBy(new GroupByClause($expression));
215 
216  return $this;
217  }
218 
226  public function having($expression) {
227  $this->options->having(new HavingClause($expression));
228 
229  return $this;
230  }
231 
240  public function orderBy($expression, $direction) {
241  $this->options->orderBy(new OrderByClause($expression, $direction));
242 
243  return $this;
244  }
245 
254  public function expandInto(QueryBuilder $qb, $table_alias = null) {
255  foreach ($this->options->selects as $select_clause) {
256  $select_clause->prepare($qb, $table_alias);
257  }
258 
259  foreach ($this->options->group_by as $group_by_clause) {
260  $group_by_clause->prepare($qb, $table_alias);
261  }
262 
263  foreach ($this->options->having as $having_clause) {
264  $having_clause->prepare($qb, $table_alias);
265  }
266 
267  if (!empty($this->options->order_by)) {
268  foreach ($this->options->order_by as $order_by_clause) {
269  $order_by_clause->prepare($qb, $table_alias);
270  }
271  }
272  }
273 }
batch($limit=null, $offset=null, $callback=null)
Fetch rows as an ElggBatch.
Definition: Repository.php:127
calculate($function, $property, $property_type=null)
Apply numeric calculation to a column.
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:110
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(empty($count)) $offset
Definition: pagination.php:26
Extends QueryBuilder with JOIN clauses.
Definition: JoinClause.php:11
Extends QueryBuilder with SELECT clauses.
Database abstraction query builder.
$type
Definition: delete.php:22
Extends QueryBuilder with GROUP BY statements.
$value
Definition: generic.php:51
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
filter(\Closure $closure)
Filter query prior to execution Callback function will receive QueryBuilder as the first argument and...
Definition: Repository.php:162
Abstract methods for interfacing with the database.
Definition: Repository.php:16
$limit
Definition: pagination.php:28
Extends QueryBuilder with HAVING clauses.
join($joined_table, $joined_alias=null, $x=null, $comparison=null, $y=null, $type=null, $case_sensitive=null)
Add JOIN clause Join a database table on an $x to $y comparison.
Definition: Repository.php:197
expandInto(QueryBuilder $qb, $table_alias=null)
Extend query builder with select, group_by, having and order_by clauses from $options.
Definition: Repository.php:254
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
select($expression)
Add SELECT.
Definition: Repository.php:175
Extends QueryBuilder with ORDER BY clauses.
Builds a clause from closure or composite expression.
Definition: WhereClause.php:11
groupBy($expression)
Add GROUP BY.
Definition: Repository.php:213
An exception thrown when there is a problem in the format of some data.
__set($name, $value)
{}
Definition: Repository.php:45
having($expression)
Add HAVING.
Definition: Repository.php:226
orderBy($expression, $direction)
Add ORDER BY.
Definition: Repository.php:240
__construct(array $options=[])
Constructor.
Definition: Repository.php:25
$qb
Definition: queue.php:11
static with(array $options=[])
Constructs a new.
Definition: Repository.php:70
execute()
Apply correct execution method based on calculation, count or other criteria.