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
<?php
/**
* BEdita, API-first content management framework
* Copyright 2018 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\Core\ORM;
use Cake\Database\Expression\Comparison;
use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query;
/**
* Query Filter trait.
*
* @since 4.0.0
*/
trait QueryFilterTrait
{
/**
* Create query filter using various operators on fields
* Options array must contain fields as keys and operators like
* - 'gt' or '>' (greather than)
* - 'lt' or '<' (less than),
* - 'ge' or '>=' (greater or equal)
* - 'le' or '<=' (less or equal) with a date
*
* It's also possible to specify an expected value or a list of values for a field
*
* Options array examples:
*
* ```
* // field1 greater than 10, field2 less then 5
* ['field1' => ['gt' => 10], 'field2' => ['lt' => 5]];
*
* // field1 in [1, 3, 10]
* ['field1' => [1, 3, 10]];
*
* // a comma separated string has the same effect as a list of values
* ['field1' => '1,3,10'];
*
* // field1 equals 10, field2 equals 1
* ['field1' => 10, 'field1' => ['eq' => 1]];
*
* // field1 greater or equal 5, field2 less or equal 4
* ['field1' => ['>=' => 10], 'field2' => ['<=' => 4]];
*
* // field1 is null, field2 is not null, field3 is null
* ['field1' => ['null' => 1], 'field2' => ['null' => 0], 'field3' => null];
* ```
*
* //
* @param \Cake\ORM\Query $query Query object instance.
* @param array $options Array of acceptable fields and conditions.
* @return \Cake\ORM\Query
*/
public function fieldsFilter(Query $query, array $options)
{
return $query->where(function (QueryExpression $exp) use ($options) {
foreach ($options as $field => $conditions) {
if ($conditions === null) {
$exp = $exp->isNull($field);
continue;
}
if (!is_array($conditions)) {
if (is_string($conditions) && strpos($conditions, ',') !== false) {
$conditions = explode(',', $conditions);
} else {
$exp = $exp->eq($field, $conditions);
continue;
}
}
$in = [];
foreach ($conditions as $operator => $value) {
if (is_numeric($operator)) {
$in[] = $value;
continue;
}
$exp = $this->operatorExpression($exp, $operator, $field, $value);
}
if (!empty($in)) {
$exp = $exp->in($field, $in);
}
}
// return the current expression if not empty
// otherwise a trivial comparison to avoid SQL errors
return $exp->count() > 0 ? $exp : new Comparison('1', '1', 'integer', '=');
});
}
/**
* Get query expression for an operator on a field with a value.
* Unrecognized operators are ignored and have no effect.
*
* @param \Cake\Database\Expression\QueryExpression $exp Current query expression
* @param string $operator Filter operator
* @param string $field Filter field
* @param string $value Filter value
* @return \Cake\Database\Expression\QueryExpression Operator query expression
*/
protected function operatorExpression(QueryExpression $exp, $operator, $field, $value)
{
switch ($operator) {
case 'eq':
case '=':
$exp = $exp->eq($field, $value);
break;
case 'neq':
case 'ne':
case '!=':
case '<>':
$exp = $exp->notEq($field, $value);
break;
case 'lt':
case '<':
$exp = $exp->lt($field, $value);
break;
case 'lte':
case 'le':
case '<=':
$exp = $exp->lte($field, $value);
break;
case 'gt':
case '>':
$exp = $exp->gt($field, $value);
break;
case 'gte':
case 'ge':
case '>=':
$exp = $exp->gte($field, $value);
break;
case 'null':
$op = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($op === false) {
$exp = $exp->isNotNull($field);
} elseif ($op === true) {
$exp = $exp->isNull($field);
}
break;
}
return $exp;
}
}