Commit | Line | Data |
---|---|---|
5c952e51 CW |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | Copyright CiviCRM LLC. All rights reserved. | | |
6 | | | | |
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 | +--------------------------------------------------------------------+ | |
11 | */ | |
12 | ||
13 | namespace Civi\Api4\Generic\Traits; | |
14 | ||
15 | use Civi\Api4\Utils\SelectUtil; | |
16 | ||
17 | /** | |
18 | * @method $this setSelect(array $selects) Set array of fields to be selected (wildcard * allowed) | |
19 | * @method array getSelect() | |
20 | * @package Civi\Api4\Generic | |
21 | */ | |
22 | trait SelectParamTrait { | |
23 | ||
24 | /** | |
25 | * Fields to return for each $ENTITY. Defaults to all fields `[*]`. | |
26 | * | |
27 | * Use the * wildcard by itself to select all available fields, or use it to match similarly-named fields. | |
28 | * E.g. `is_*` will match fields named is_primary, is_active, etc. | |
29 | * | |
30 | * Set to `["row_count"]` to return only the number of $ENTITIES found. | |
31 | * | |
32 | * @var array | |
33 | */ | |
34 | protected $select = []; | |
35 | ||
36 | /** | |
37 | * Add one or more fields to be selected (wildcard * allowed) | |
38 | * @param string ...$fieldNames | |
39 | * @return $this | |
40 | */ | |
41 | public function addSelect(string ...$fieldNames) { | |
42 | $this->select = array_merge($this->select, $fieldNames); | |
43 | return $this; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Adds all standard fields matched by the * wildcard | |
48 | * | |
49 | * Note: this function only deals with simple wildcard expressions. | |
50 | * It ignores those containing special characters like dots or parentheses, | |
51 | * they are handled separately in Api4SelectQuery. | |
52 | * | |
53 | * @throws \API_Exception | |
54 | */ | |
55 | protected function expandSelectClauseWildcards() { | |
56 | if (!$this->select) { | |
57 | $this->select = ['*']; | |
58 | } | |
59 | // Get expressions containing wildcards but no dots or parentheses | |
60 | $wildFields = array_filter($this->select, function($item) { | |
61 | return strpos($item, '*') !== FALSE && strpos($item, '.') === FALSE && strpos($item, '(') === FALSE && strpos($item, ' ') === FALSE; | |
62 | }); | |
63 | if ($wildFields) { | |
64 | // Wildcards should not match "Extra" fields | |
65 | $standardFields = array_filter(array_map(function($field) { | |
66 | return $field['type'] === 'Extra' ? NULL : $field['name']; | |
67 | }, $this->entityFields())); | |
68 | foreach ($wildFields as $item) { | |
69 | $pos = array_search($item, array_values($this->select)); | |
70 | $matches = SelectUtil::getMatchingFields($item, $standardFields); | |
71 | array_splice($this->select, $pos, 1, $matches); | |
72 | } | |
73 | } | |
74 | $this->select = array_unique($this->select); | |
75 | } | |
76 | ||
77 | } |