Commit | Line | Data |
---|---|---|
39e0f675 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 | /** | |
14 | * | |
15 | * @package CRM | |
16 | * @copyright CiviCRM LLC https://civicrm.org/licensing | |
39e0f675 CW |
17 | */ |
18 | ||
19 | ||
20 | namespace Civi\Api4\Utils; | |
21 | ||
22 | class SelectUtil { | |
23 | ||
24 | /** | |
25 | * Checks if a field is in the Select array or matches a wildcard pattern in the Select array | |
26 | * | |
27 | * @param string $field | |
28 | * @param array $selects | |
29 | * @return bool | |
30 | */ | |
31 | public static function isFieldSelected($field, $selects) { | |
32 | if (in_array($field, $selects) || (in_array('*', $selects) && strpos($field, '.') === FALSE)) { | |
33 | return TRUE; | |
34 | } | |
35 | foreach ($selects as $item) { | |
36 | if (strpos($item, '*') !== FALSE && self::getMatchingFields($item, [$field])) { | |
37 | return TRUE; | |
38 | } | |
39 | } | |
40 | return FALSE; | |
41 | } | |
42 | ||
43 | /** | |
54a08b17 CW |
44 | * Filters a list of fieldnames by matching a pattern which may contain * wildcards. |
45 | * | |
46 | * For fieldnames joined with a dot (e.g. email.contact_id), wildcards are only allowed after the last dot. | |
47 | * | |
39e0f675 CW |
48 | * @param string $pattern |
49 | * @param array $fieldNames | |
50 | * @return array | |
51 | */ | |
52 | public static function getMatchingFields($pattern, $fieldNames) { | |
54a08b17 | 53 | // If the pattern is "select all" then we return all base fields (excluding those with a dot) |
39e0f675 | 54 | if ($pattern === '*') { |
54a08b17 CW |
55 | return array_values(array_filter($fieldNames, function($field) { |
56 | return strpos($field, '.') === FALSE; | |
57 | })); | |
39e0f675 | 58 | } |
54a08b17 CW |
59 | $dot = strrpos($pattern, '.'); |
60 | $prefix = $dot === FALSE ? '' : substr($pattern, 0, $dot + 1); | |
61 | $search = $dot === FALSE ? $pattern : substr($pattern, $dot + 1); | |
62 | $search = '/^' . str_replace('\*', '.*', preg_quote($search, '/')) . '$/'; | |
63 | return array_values(array_filter($fieldNames, function($field) use ($search, $prefix) { | |
64 | // Exclude fields that don't have the same join prefix | |
65 | if (($prefix !== '' && strpos($field, $prefix) !== 0) || substr_count($prefix, '.') !== substr_count($field, '.')) { | |
66 | return FALSE; | |
67 | } | |
68 | // Now strip the prefix and compare field name to the pattern | |
69 | return preg_match($search, substr($field, strlen($prefix))); | |
39e0f675 CW |
70 | })); |
71 | } | |
72 | ||
73 | } |