4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Generic\Traits
;
24 use Civi\API\Exception\NotImplementedException
;
27 * Helper functions for performing api queries on arrays of data.
29 * @package Civi\Api4\Generic
31 trait ArrayQueryActionTrait
{
34 * @param array $values
35 * List of all rows to be filtered
36 * @param \Civi\Api4\Generic\Result $result
37 * Object to store result
39 protected function queryArray($values, $result) {
40 $values = $this->filterArray($values);
41 $values = $this->sortArray($values);
42 // Set total count before applying limit
43 $result->rowCount
= count($values);
44 $values = $this->limitArray($values);
45 $values = $this->selectArray($values);
46 $result->exchangeArray($values);
50 * @param array $values
53 protected function filterArray($values) {
54 if ($this->getWhere()) {
55 $values = array_filter($values, [$this, 'evaluateFilters']);
57 return array_values($values);
64 private function evaluateFilters($row) {
65 $where = $this->getWhere();
66 $allConditions = in_array($where[0], ['AND', 'OR', 'NOT']) ?
$where : ['AND', $where];
67 return $this->walkFilters($row, $allConditions);
72 * @param array $filters
74 * @throws \Civi\API\Exception\NotImplementedException
76 private function walkFilters($row, $filters) {
77 switch ($filters[0]) {
81 foreach ($filters[1] as $filter) {
82 if (!$this->walkFilters($row, $filter)) {
87 return $result == ($filters[0] == 'AND');
90 $result = !count($filters[1]);
91 foreach ($filters[1] as $filter) {
92 if ($this->walkFilters($row, $filter)) {
99 return $this->filterCompare($row, $filters);
105 * @param array $condition
107 * @throws \Civi\API\Exception\NotImplementedException
109 private function filterCompare($row, $condition) {
110 if (!is_array($condition)) {
111 throw new NotImplementedException('Unexpected where syntax; expecting array.');
113 $value = $row[$condition[0]] ??
NULL;
114 $operator = $condition[1];
115 $expected = $condition[2] ??
NULL;
120 $equal = $value == $expected;
121 // PHP is too imprecise about comparing the number 0
122 if ($expected === 0 ||
$expected === '0') {
123 $equal = ($value === 0 ||
$value === '0');
125 // PHP is too imprecise about comparing empty strings
126 if ($expected === '') {
127 $equal = ($value === '');
129 return $equal == ($operator == '=');
133 return is_null($value) == ($operator == 'IS NULL');
136 return $value > $expected;
139 return $value >= $expected;
142 return $value < $expected;
145 return $value <= $expected;
149 $between = ($value >= $expected[0] && $value <= $expected[1]);
150 return $between == ($operator == 'BETWEEN');
154 $pattern = '/^' . str_replace('%', '.*', preg_quote($expected, '/')) . '$/i';
155 return !preg_match($pattern, $value) == ($operator != 'LIKE');
158 return in_array($value, $expected);
161 return !in_array($value, $expected);
164 throw new NotImplementedException("Unsupported operator: '$operator' cannot be used with array data");
172 protected function sortArray($values) {
173 if ($this->getOrderBy()) {
174 usort($values, [$this, 'sortCompare']);
179 private function sortCompare($a, $b) {
180 foreach ($this->getOrderBy() as $field => $dir) {
181 $modifier = $dir == 'ASC' ?
1 : -1;
182 if (isset($a[$field]) && isset($b[$field])) {
183 if ($a[$field] == $b[$field]) {
186 return (strnatcasecmp($a[$field], $b[$field]) * $modifier);
188 elseif (isset($a[$field]) ||
isset($b[$field])) {
189 return ((isset($a[$field]) ?
1 : -1) * $modifier);
199 protected function selectArray($values) {
200 if ($this->getSelect() === ['row_count']) {
201 $values = [['row_count' => count($values)]];
203 elseif ($this->getSelect()) {
204 // Return only fields specified by SELECT
205 foreach ($values as &$value) {
206 $value = array_intersect_key($value, array_flip($this->getSelect()));
210 // With no SELECT specified, return all values that are keyed by plain field name; omit those with :pseudoconstant suffixes
211 foreach ($values as &$value) {
212 $value = array_filter($value, function($key) {
213 return strpos($key, ':') === FALSE;
214 }, ARRAY_FILTER_USE_KEY
);
224 protected function limitArray($values) {
225 if ($this->getOffset() ||
$this->getLimit()) {
226 $values = array_slice($values, $this->getOffset() ?
: 0, $this->getLimit() ?
: NULL);