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
19 namespace Civi\Api4\Generic
;
21 use Civi\API\Exception\NotImplementedException
;
22 use Civi\Api4\Utils\ReflectionUtils
;
25 * Base class for all api entities.
27 * This is the most generic of 3 possible base classes for an APIv4 Entity
28 * (the other 2, which extend this class, are `BasicEntity` and `DAOEntity`).
30 * Implementing an API by extending this class directly is appropriate when it does not implement
31 * all of the CRUD actions, or only a subset like `get` without `create`, `update` or `delete`;
32 * for example the RelationshipCache entity.
34 * For all other APIs that do implement CRUD it is recommended to use:
35 * 1. `DAOEntity` for all entities with a DAO (sql table).
36 * 2. `BasicEntity` for all others, e.g. file-based entities.
38 * An entity which extends this class directly must, at minimum, implement the `getFields` action.
40 * @see https://lab.civicrm.org/extensions/api4example
42 abstract class AbstractEntity
{
45 * @param bool $checkPermissions
46 * @return \Civi\Api4\Action\GetActions
48 public static function getActions($checkPermissions = TRUE) {
49 return (new \Civi\Api4\Action\
GetActions(self
::getEntityName(), __FUNCTION__
))
50 ->setCheckPermissions($checkPermissions);
54 * @return \Civi\Api4\Generic\BasicGetFieldsAction
56 abstract public static function getFields();
59 * Returns a list of permissions needed to access the various actions in this api.
63 public static function permissions() {
64 $permissions = \CRM_Core_Permission
::getEntityActionPermissions();
66 // For legacy reasons the permissions are keyed by lowercase entity name
67 $lcentity = \CRM_Core_DAO_AllCoreTables
::convertEntityNameToLower(self
::getEntityName());
68 // Merge permissions for this entity with the defaults
69 return ($permissions[$lcentity] ??
[]) +
$permissions['default'];
73 * Get entity name from called class
77 protected static function getEntityName() {
78 return self
::stripNamespace(static::class);
82 * Overridable function to return a localized title for this entity.
85 * Whether to return a plural title.
88 protected static function getEntityTitle($plural = FALSE) {
89 return static::getEntityName();
93 * Magic method to return the action object for an api.
95 * @param string $action
97 * @return AbstractAction
98 * @throws NotImplementedException
100 public static function __callStatic($action, $args) {
101 $entity = self
::getEntityName();
102 // Find class for this action
103 $entityAction = "\\Civi\\Api4\\Action\\$entity\\" . ucfirst($action);
104 if (class_exists($entityAction)) {
105 $actionObject = new $entityAction($entity, $action);
106 if (isset($args[0]) && $args[0] === FALSE) {
107 $actionObject->setCheckPermissions(FALSE);
111 throw new NotImplementedException("Api $entity $action version 4 does not exist.");
113 return $actionObject;
117 * Reflection function called by Entity::get()
119 * @see \Civi\Api4\Action\Entity\Get
122 public static function getInfo() {
124 'name' => static::getEntityName(),
125 'title' => static::getEntityTitle(),
126 'titlePlural' => static::getEntityTitle(TRUE),
127 'type' => self
::stripNamespace(get_parent_class(static::class)),
129 $reflection = new \
ReflectionClass(static::class);
130 $info +
= ReflectionUtils
::getCodeDocs($reflection, NULL, ['entity' => $info['name']]);
131 unset($info['package'], $info['method']);
136 * Remove namespace prefix from a class name
138 * @param string $className
141 private static function stripNamespace($className) {
142 return substr($className, strrpos($className, '\\') +
1);