Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

Elgg/Database/Clauses/TypeSubtypeWhereClause.php (1 issue)

1
<?php
2
3
namespace Elgg\Database\Clauses;
4
5
use Elgg\Database\QueryBuilder;
6
7
/**
8
 * Builds clauses for filtering entities by their type and subtype
9
 */
10
class TypeSubtypeWhereClause extends WhereClause {
11
12
	/**
13
	 * @var string
14
	 */
15
	public $type_column = 'type';
16
17
	/**
18
	 * @var string
19
	 */
20
	public $subtype_column = 'subtype';
21
22
	/**
23
	 * @var array
24
	 */
25
	public $type_subtype_pairs = [];
26
27
	/**
28
	 * {@inheritdoc}
29
	 */
30
	public function prepare(QueryBuilder $qb, $table_alias = null) {
31
32 1258
		$alias = function ($column) use ($table_alias) {
33 1165
			return $table_alias ? "{$table_alias}.{$column}" : $column;
34 1258
		};
35
36
		$types_where = [];
37
38
		if (!empty($this->type_subtype_pairs)) {
39
			foreach ($this->type_subtype_pairs as $type => $subtypes) {
40
				if (is_array($subtypes) && !empty($subtypes)) {
41
					$types_where[] = $qb->merge([
42
						$qb->compare($alias($this->type_column), '=', $type, ELGG_VALUE_STRING),
43
						$qb->compare($alias($this->subtype_column), '=', $subtypes, ELGG_VALUE_STRING),
44
					]);
45
				} else {
46
					$types_where[] = $qb->compare($alias($this->type_column), '=', $type, ELGG_VALUE_STRING);
47
				}
48
			}
49
		}
50
51
		if (!empty($types_where)) {
52
			$wheres[] = $qb->merge($types_where, 'OR');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$wheres was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wheres = array(); before regardless.
Loading history...
53
		}
54
55
		$wheres[] = parent::prepare($qb, $table_alias);
56
57
		return $qb->merge($wheres);
58
	}
59
60
}
61