APIv4 - Support pseudoconstant lookups
[civicrm-core.git] / Civi / Api4 / Generic / AbstractEntity.php
CommitLineData
19b53e5b 1<?php
380f3545
TO
2
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
19b53e5b
C
21namespace Civi\Api4\Generic;
22
23use Civi\API\Exception\NotImplementedException;
24
25/**
26 * Base class for all api entities.
27 *
28 * When adding your own api from an extension, extend this class only
29 * if your entity does not have an associated DAO. Otherwise extend DAOEntity.
30 *
31 * The recommended way to create a non-DAO-based api is to extend this class
32 * and then add a getFields function and any other actions you wish, e.g.
c2adedc1
CW
33 * - a get() function which returns BasicGetAction using your custom getter callback.
34 * - a create() function which returns BasicCreateAction using your custom setter callback.
35 * - a save() function which returns BasicSaveAction using your custom setter callback.
36 * - an update() function which returns BasicUpdateAction using your custom setter callback.
37 * - a delete() function which returns BasicBatchAction using your custom delete callback.
19b53e5b 38 * - a replace() function which returns BasicReplaceAction (no callback needed but
c2adedc1 39 * depends on the existence of get, save & delete actions).
19b53e5b
C
40 *
41 * Note that you can use the same setter callback function for update as create -
42 * that function can distinguish between new & existing records by checking if the
43 * unique identifier has been set (identifier field defaults to "id" but you can change
c2adedc1
CW
44 * that when constructing BasicUpdateAction).
45 *
46 * @see https://lab.civicrm.org/extensions/api4example
19b53e5b
C
47 */
48abstract class AbstractEntity {
49
50 /**
51 * @return \Civi\Api4\Action\GetActions
52 */
53 public static function getActions() {
54 return new \Civi\Api4\Action\GetActions(self::getEntityName(), __FUNCTION__);
55 }
56
57 /**
c0c668ce 58 * @return \Civi\Api4\Generic\BasicGetFieldsAction
19b53e5b 59 */
c0c668ce 60 abstract public static function getFields();
19b53e5b
C
61
62 /**
63 * Returns a list of permissions needed to access the various actions in this api.
64 *
65 * @return array
66 */
67 public static function permissions() {
68 $permissions = \CRM_Core_Permission::getEntityActionPermissions();
69
70 // For legacy reasons the permissions are keyed by lowercase entity name
71 // Note: Convert to camel & back in order to circumvent all the api3 naming oddities
72 $lcentity = _civicrm_api_get_entity_name_from_camel(\CRM_Utils_String::convertStringToCamel(self::getEntityName()));
73 // Merge permissions for this entity with the defaults
74 return \CRM_Utils_Array::value($lcentity, $permissions, []) + $permissions['default'];
75 }
76
77 /**
78 * Get entity name from called class
79 *
80 * @return string
81 */
82 protected static function getEntityName() {
83 return substr(static::class, strrpos(static::class, '\\') + 1);
84 }
85
86 /**
87 * Magic method to return the action object for an api.
88 *
89 * @param string $action
90 * @param null $args
91 * @return AbstractAction
92 * @throws NotImplementedException
93 */
94 public static function __callStatic($action, $args) {
95 $entity = self::getEntityName();
96 // Find class for this action
97 $entityAction = "\\Civi\\Api4\\Action\\$entity\\" . ucfirst($action);
98 if (class_exists($entityAction)) {
99 $actionObject = new $entityAction($entity, $action);
100 }
101 else {
102 throw new NotImplementedException("Api $entity $action version 4 does not exist.");
103 }
104 return $actionObject;
105 }
106
107}