From 5967c9e828b4ac65108df898345282dcf781d7ca Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 8 Apr 2020 14:07:29 -0400 Subject: [PATCH] APIv4 Explorer - Get list of SQL functions --- CRM/Api4/Page/Api4Explorer.php | 22 +++++++++++++++++++ Civi/Api4/Query/SqlFunction.php | 7 +++--- .../phpunit/api/v4/Action/SqlFunctionTest.php | 8 +++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CRM/Api4/Page/Api4Explorer.php b/CRM/Api4/Page/Api4Explorer.php index 77eb0d1e53..9460b617c9 100644 --- a/CRM/Api4/Page/Api4Explorer.php +++ b/CRM/Api4/Page/Api4Explorer.php @@ -28,6 +28,7 @@ class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page { 'schema' => (array) \Civi\Api4\Entity::get()->setChain(['fields' => ['$name', 'getFields']])->execute(), 'links' => (array) \Civi\Api4\Entity::getLinks()->execute(), 'docs' => \Civi\Api4\Utils\ReflectionUtils::parseDocBlock($apiDoc->getDocComment()), + 'functions' => self::getSqlFunctions(), 'groupOptions' => array_column((array) $groupOptions, 'options', 'name'), ]; Civi::resources() @@ -49,4 +50,25 @@ class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page { parent::run(); } + /** + * Gets info about all available sql functions + * @return array + */ + public static function getSqlFunctions() { + $fns = []; + foreach (glob(Civi::paths()->getPath('[civicrm.root]/Civi/Api4/Query/SqlFunction*.php')) as $file) { + $matches = []; + if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) { + $className = '\Civi\Api4\Query\\' . $matches[1]; + if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) { + $fns[] = [ + 'name' => $className::getName(), + 'params' => $className::getParams(), + ]; + } + } + } + return $fns; + } + } diff --git a/Civi/Api4/Query/SqlFunction.php b/Civi/Api4/Query/SqlFunction.php index e10bbcb23b..19753d91ac 100644 --- a/Civi/Api4/Query/SqlFunction.php +++ b/Civi/Api4/Query/SqlFunction.php @@ -161,10 +161,9 @@ abstract class SqlFunction extends SqlExpression { * Get the name of this sql function. * @return string */ - public function getName(): string { - $className = get_class($this); - $pos = strrpos($className, 'SqlFunction'); - return substr($className, $pos + 11); + public static function getName(): string { + $className = static::class; + return substr($className, strrpos($className, 'SqlFunction') + 11); } /** diff --git a/tests/phpunit/api/v4/Action/SqlFunctionTest.php b/tests/phpunit/api/v4/Action/SqlFunctionTest.php index ab9b09dd8b..f948b22db4 100644 --- a/tests/phpunit/api/v4/Action/SqlFunctionTest.php +++ b/tests/phpunit/api/v4/Action/SqlFunctionTest.php @@ -29,6 +29,14 @@ use Civi\Api4\Contribution; */ class SqlFunctionTest extends UnitTestCase { + public function testGetFunctions() { + $functions = array_column(\CRM_Api4_Page_Api4Explorer::getSqlFunctions(), NULL, 'name'); + $this->assertArrayHasKey('SUM', $functions); + $this->assertArrayNotHasKey('', $functions); + $this->assertArrayNotHasKey('SqlFunction', $functions); + $this->assertEquals(1, $functions['MAX']['params'][0]['expr']); + } + public function testGroupAggregates() { $cid = Contact::create()->setCheckPermissions(FALSE)->addValue('first_name', 'bill')->execute()->first()['id']; Contribution::save() -- 2.25.1