Merge in 5.47
[civicrm-core.git] / Civi / API / Provider / StaticProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\API\Provider;
13
14 use Civi\API\Events;
15
16 /**
17 * A static provider is useful for creating mock API implementations which
18 * manages records in-memory.
19 *
20 * TODO Add a static provider to SyntaxConformanceTest to ensure that it's
21 * representative.
22 */
23 class StaticProvider extends AdhocProvider {
24 protected $records;
25 protected $fields;
26
27 /**
28 * @return array
29 */
30 public static function getSubscribedEvents() {
31 return [
32 'civi.api.resolve' => [
33 ['onApiResolve', Events::W_MIDDLE],
34 ],
35 'civi.api.authorize' => [
36 ['onApiAuthorize', Events::W_MIDDLE],
37 ],
38 ];
39 }
40
41 /**
42 * @param int $version
43 * API version.
44 * @param string $entity
45 * API entity.
46 * @param array $fields
47 * List of fields in this fake entity.
48 * @param array $perms
49 * Array(string $action => string $perm).
50 * @param array $records
51 * List of mock records to be read/updated by API calls.
52 */
53 public function __construct($version, $entity, $fields, $perms = [], $records = []) {
54 parent::__construct($version, $entity);
55
56 $perms = array_merge([
57 'create' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
58 'get' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
59 'delete' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
60 ], $perms);
61
62 $this->records = \CRM_Utils_Array::index(['id'], $records);
63 $this->fields = $fields;
64
65 $this->addAction('create', $perms['create'], [$this, 'doCreate']);
66 $this->addAction('get', $perms['get'], [$this, 'doGet']);
67 $this->addAction('delete', $perms['delete'], [$this, 'doDelete']);
68 }
69
70 /**
71 * @return array
72 */
73 public function getRecords() {
74 return $this->records;
75 }
76
77 /**
78 * @param array $records
79 * List of mock records to be read/updated by API calls.
80 */
81 public function setRecords($records) {
82 $this->records = $records;
83 }
84
85 /**
86 * @param array $apiRequest
87 * The full description of the API request.
88 * @return array
89 * Formatted API result
90 * @throws \API_Exception
91 */
92 public function doCreate($apiRequest) {
93 if (isset($apiRequest['params']['id'])) {
94 $id = $apiRequest['params']['id'];
95 }
96 else {
97 $id = max(array_keys($this->records)) + 1;
98 $this->records[$id] = [];
99 }
100
101 if (!isset($this->records[$id])) {
102 throw new \API_Exception("Invalid ID: $id");
103 }
104
105 foreach ($this->fields as $field) {
106 if (isset($apiRequest['params'][$field])) {
107 $this->records[$id][$field] = $apiRequest['params'][$field];
108 }
109 }
110
111 return civicrm_api3_create_success($this->records[$id]);
112 }
113
114 /**
115 * @param array $apiRequest
116 * The full description of the API request.
117 * @return array
118 * Formatted API result
119 * @throws \API_Exception
120 */
121 public function doGet($apiRequest) {
122 return _civicrm_api3_basic_array_get($apiRequest['entity'], $apiRequest['params'], $this->records, 'id', $this->fields);
123 }
124
125 /**
126 * @param array $apiRequest
127 * The full description of the API request.
128 * @return array
129 * Formatted API result
130 * @throws \API_Exception
131 */
132 public function doDelete($apiRequest) {
133 $id = @$apiRequest['params']['id'];
134 if ($id && isset($this->records[$id])) {
135 unset($this->records[$id]);
136 }
137 return civicrm_api3_create_success([]);
138 }
139
140 }