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\Utils
;
27 * Checks if a field is in the Select array or matches a wildcard pattern in the Select array
29 * @param string $field
30 * @param array $selects
33 public static function isFieldSelected($field, $selects) {
34 if (in_array($field, $selects) ||
(in_array('*', $selects) && strpos($field, '.') === FALSE)) {
37 foreach ($selects as $item) {
38 if (strpos($item, '*') !== FALSE && self
::getMatchingFields($item, [$field])) {
46 * @param string $pattern
47 * @param array $fieldNames
50 public static function getMatchingFields($pattern, $fieldNames) {
51 if ($pattern === '*') {
54 $pattern = '/^' . str_replace('\*', '.*', preg_quote($pattern, '/')) . '$/';
55 return array_values(array_filter($fieldNames, function($field) use ($pattern) {
56 return preg_match($pattern, $field);