From 9d2afe25e2566c1fede83ba9e61fd74f49cd86dc Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 28 Aug 2020 13:07:08 -0400 Subject: [PATCH] APIv4 - Move list of accepted query operators to CoreUtil --- CRM/Api4/Page/Api4Explorer.php | 3 ++- CRM/Core/DAO.php | 2 +- Civi/Api4/Generic/AbstractQueryAction.php | 6 ++++-- Civi/Api4/Generic/DAOGetAction.php | 3 ++- Civi/Api4/Query/Api4SelectQuery.php | 2 +- Civi/Api4/Utils/CoreUtil.php | 7 +++++++ ext/search/CRM/Search/Page/Ang.php | 2 +- 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CRM/Api4/Page/Api4Explorer.php b/CRM/Api4/Page/Api4Explorer.php index 3daba07338..6de079bb8f 100644 --- a/CRM/Api4/Page/Api4Explorer.php +++ b/CRM/Api4/Page/Api4Explorer.php @@ -11,6 +11,7 @@ */ use Civi\Api4\Service\Schema\Joinable\Joinable; +use Civi\Api4\Utils\CoreUtil; /** * @@ -30,7 +31,7 @@ class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page { }); } $vars = [ - 'operators' => \CRM_Core_DAO::acceptedSQLOperators(), + 'operators' => CoreUtil::getOperators(), 'basePath' => Civi::resources()->getUrl('civicrm'), 'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(), 'links' => $entityLinks, diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 4c33f8002e..d62fba1401 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -2830,7 +2830,7 @@ SELECT contact_id /** * @see http://issues.civicrm.org/jira/browse/CRM-9150 * support for other syntaxes is discussed in ticket but being put off for now - * @return array + * @return string[] */ public static function acceptedSQLOperators() { return [ diff --git a/Civi/Api4/Generic/AbstractQueryAction.php b/Civi/Api4/Generic/AbstractQueryAction.php index ed7ac9bf37..85ebcfb5ed 100644 --- a/Civi/Api4/Generic/AbstractQueryAction.php +++ b/Civi/Api4/Generic/AbstractQueryAction.php @@ -19,6 +19,8 @@ namespace Civi\Api4\Generic; +use Civi\Api4\Utils\CoreUtil; + /** * Base class for all actions that need to fetch records (`Get`, `Update`, `Delete`, etc.). * @@ -86,7 +88,7 @@ abstract class AbstractQueryAction extends AbstractAction { * @throws \API_Exception */ public function addWhere(string $fieldName, string $op, $value = NULL) { - if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) { + if (!in_array($op, CoreUtil::getOperators())) { throw new \API_Exception('Unsupported operator'); } $this->where[] = [$fieldName, $op, $value]; @@ -145,7 +147,7 @@ abstract class AbstractQueryAction extends AbstractAction { } return $output . '(' . $this->whereClauseToString($whereClause, $op) . ')'; } - elseif (isset($whereClause[1]) && in_array($whereClause[1], \CRM_Core_DAO::acceptedSQLOperators())) { + elseif (isset($whereClause[1]) && in_array($whereClause[1], CoreUtil::getOperators())) { $output = $whereClause[0] . ' ' . $whereClause[1] . ' '; if (isset($whereClause[2])) { $output .= is_array($whereClause[2]) ? '[' . implode(', ', $whereClause[2]) . ']' : $whereClause[2]; diff --git a/Civi/Api4/Generic/DAOGetAction.php b/Civi/Api4/Generic/DAOGetAction.php index d6a3f10eea..852a333ccf 100644 --- a/Civi/Api4/Generic/DAOGetAction.php +++ b/Civi/Api4/Generic/DAOGetAction.php @@ -20,6 +20,7 @@ namespace Civi\Api4\Generic; use Civi\Api4\Query\Api4SelectQuery; +use Civi\Api4\Utils\CoreUtil; /** * Retrieve $ENTITIES based on criteria specified in the `where` parameter. @@ -154,7 +155,7 @@ class DAOGetAction extends AbstractGetAction { * @throws \API_Exception */ public function addHaving(string $expr, string $op, $value = NULL) { - if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) { + if (!in_array($op, CoreUtil::getOperators())) { throw new \API_Exception('Unsupported operator'); } $this->having[] = [$expr, $op, $value]; diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 5721e91968..6198b93603 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -362,7 +362,7 @@ class Api4SelectQuery { protected function composeClause(array $clause, string $type) { // Pad array for unary operators list($expr, $operator, $value) = array_pad($clause, 3, NULL); - if (!in_array($operator, \CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { + if (!in_array($operator, CoreUtil::getOperators(), TRUE)) { throw new \API_Exception('Illegal operator'); } diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index 8d06ce9678..9bc7ec9b6a 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -71,4 +71,11 @@ class CoreUtil { return $entityName; } + /** + * @return string[] + */ + public static function getOperators() { + return \CRM_Core_DAO::acceptedSQLOperators(); + } + } diff --git a/ext/search/CRM/Search/Page/Ang.php b/ext/search/CRM/Search/Page/Ang.php index 069dc0ee97..936f92edd6 100644 --- a/ext/search/CRM/Search/Page/Ang.php +++ b/ext/search/CRM/Search/Page/Ang.php @@ -32,7 +32,7 @@ class CRM_Search_Page_Ang extends CRM_Core_Page { // Add client-side vars for the search UI $vars = [ - 'operators' => \CRM_Core_DAO::acceptedSQLOperators(), + 'operators' => \Civi\Api4\Utils\CoreUtil::getOperators(), 'schema' => $this->schema, 'links' => $this->getLinks(), 'loadOptions' => $this->loadOptions, -- 2.25.1