From 9b69b3a1ff4743ff1b064b02898571cdccb6623f Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 24 Aug 2021 10:12:12 +1000 Subject: [PATCH] Resolve dev/core#2768 Reinstate code into APIv4 that handled magic functions Add in test to assert Exception type and exception message that is thrown --- Civi/Api4/Generic/AbstractAction.php | 27 +++++++++++++++ .../v4/Action/AbstractActionFunctionTest.php | 34 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php diff --git a/Civi/Api4/Generic/AbstractAction.php b/Civi/Api4/Generic/AbstractAction.php index cd6283c8fa..fcc6001ab7 100644 --- a/Civi/Api4/Generic/AbstractAction.php +++ b/Civi/Api4/Generic/AbstractAction.php @@ -191,6 +191,33 @@ abstract class AbstractAction implements \ArrayAccess { return $this; } + /** + * Magic function to provide automatic getter/setter for params. + * + * @param $name + * @param $arguments + * @return static|mixed + * @throws \API_Exception + */ + public function __call($name, $arguments) { + $param = lcfirst(substr($name, 3)); + if (!$param || $param[0] == '_') { + throw new \API_Exception('Unknown api parameter: ' . $name); + } + $mode = substr($name, 0, 3); + if ($this->paramExists($param)) { + switch ($mode) { + case 'get': + return $this->$param; + + case 'set': + $this->$param = $arguments[0]; + return $this; + } + } + throw new \API_Exception('Unknown api parameter: ' . $name); + } + /** * Invoke api call. * diff --git a/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php b/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php new file mode 100644 index 0000000000..3eaae0130f --- /dev/null +++ b/tests/phpunit/api/v4/Action/AbstractActionFunctionTest.php @@ -0,0 +1,34 @@ +expectException('API_Exception'); + $this->expectExceptionMessage('Unknown api parameter: getLanguage'); + \Civi\Api4\System::flush(FALSE)->getLanguage(); + } + +} -- 2.25.1