Merge pull request #21850 from mariav0/patch-1
[civicrm-core.git] / Civi / Api4 / Generic / AbstractAction.php
index cd6283c8fa4987fb796f1032c800e8e952198d34..08daf23954f7bd9336a4249e019380ad35ea0113 100644 (file)
@@ -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.
    *
@@ -254,8 +281,13 @@ abstract class AbstractAction implements \ArrayAccess {
       foreach ($this->reflect()->getProperties(\ReflectionProperty::IS_PROTECTED) as $property) {
         $name = $property->getName();
         if ($name != 'version' && $name[0] != '_') {
-          $this->_paramInfo[$name] = ReflectionUtils::getCodeDocs($property, 'Property', $vars);
-          $this->_paramInfo[$name]['default'] = $defaults[$name];
+          $docs = ReflectionUtils::getCodeDocs($property, 'Property', $vars);
+          $docs['default'] = $defaults[$name];
+          if (!empty($docs['optionsCallback'])) {
+            $docs['options'] = $this->{$docs['optionsCallback']}();
+            unset($docs['optionsCallback']);
+          }
+          $this->_paramInfo[$name] = $docs;
         }
       }
     }