Merge pull request #16001 from agileware/CIVICRM-1383
[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.
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 * - an update() function which returns BasicUpdateAction using your custom setter callback
36 * - a delete() function which returns BasicBatchAction using your custom delete callback
37 * - a replace() function which returns BasicReplaceAction (no callback needed but
38 * depends on the existence of get, create, update & delete actions)
39 *
40 * Note that you can use the same setter callback function for update as create -
41 * that function can distinguish between new & existing records by checking if the
42 * unique identifier has been set (identifier field defaults to "id" but you can change
43 * that when constructing BasicUpdateAction)
44 */
45abstract class AbstractEntity {
46
47 /**
48 * @return \Civi\Api4\Action\GetActions
49 */
50 public static function getActions() {
51 return new \Civi\Api4\Action\GetActions(self::getEntityName(), __FUNCTION__);
52 }
53
54 /**
55 * Should return \Civi\Api4\Generic\BasicGetFieldsAction
56 * @todo make this function abstract when we require php 7.
57 * @throws \Civi\API\Exception\NotImplementedException
58 */
59 public static function getFields() {
60 throw new NotImplementedException(self::getEntityName() . ' should implement getFields action.');
61 }
62
63 /**
64 * Returns a list of permissions needed to access the various actions in this api.
65 *
66 * @return array
67 */
68 public static function permissions() {
69 $permissions = \CRM_Core_Permission::getEntityActionPermissions();
70
71 // For legacy reasons the permissions are keyed by lowercase entity name
72 // Note: Convert to camel & back in order to circumvent all the api3 naming oddities
73 $lcentity = _civicrm_api_get_entity_name_from_camel(\CRM_Utils_String::convertStringToCamel(self::getEntityName()));
74 // Merge permissions for this entity with the defaults
75 return \CRM_Utils_Array::value($lcentity, $permissions, []) + $permissions['default'];
76 }
77
78 /**
79 * Get entity name from called class
80 *
81 * @return string
82 */
83 protected static function getEntityName() {
84 return substr(static::class, strrpos(static::class, '\\') + 1);
85 }
86
87 /**
88 * Magic method to return the action object for an api.
89 *
90 * @param string $action
91 * @param null $args
92 * @return AbstractAction
93 * @throws NotImplementedException
94 */
95 public static function __callStatic($action, $args) {
96 $entity = self::getEntityName();
97 // Find class for this action
98 $entityAction = "\\Civi\\Api4\\Action\\$entity\\" . ucfirst($action);
99 if (class_exists($entityAction)) {
100 $actionObject = new $entityAction($entity, $action);
101 }
102 else {
103 throw new NotImplementedException("Api $entity $action version 4 does not exist.");
104 }
105 return $actionObject;
106 }
107
108}