3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 namespace Civi\API\Provider
;
17 * A static provider is useful for creating mock API implementations which
18 * manages records in-memory.
20 * TODO Add a static provider to SyntaxConformanceTest to ensure that it's
23 class StaticProvider
extends AdhocProvider
{
30 public static function getSubscribedEvents() {
32 'civi.api.resolve' => [
33 ['onApiResolve', Events
::W_MIDDLE
],
35 'civi.api.authorize' => [
36 ['onApiAuthorize', Events
::W_MIDDLE
],
44 * @param string $entity
46 * @param array $fields
47 * List of fields in this fake entity.
49 * Array(string $action => string $perm).
50 * @param array $records
51 * List of mock records to be read/updated by API calls.
53 public function __construct($version, $entity, $fields, $perms = [], $records = []) {
54 parent
::__construct($version, $entity);
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
,
62 $this->records
= \CRM_Utils_Array
::index(['id'], $records);
63 $this->fields
= $fields;
65 $this->addAction('create', $perms['create'], [$this, 'doCreate']);
66 $this->addAction('get', $perms['get'], [$this, 'doGet']);
67 $this->addAction('delete', $perms['delete'], [$this, 'doDelete']);
73 public function getRecords() {
74 return $this->records
;
78 * @param array $records
79 * List of mock records to be read/updated by API calls.
81 public function setRecords($records) {
82 $this->records
= $records;
86 * @param array $apiRequest
87 * The full description of the API request.
89 * Formatted API result
90 * @throws \API_Exception
92 public function doCreate($apiRequest) {
93 if (isset($apiRequest['params']['id'])) {
94 $id = $apiRequest['params']['id'];
97 $id = max(array_keys($this->records
)) +
1;
98 $this->records
[$id] = [];
101 if (!isset($this->records
[$id])) {
102 throw new \
API_Exception("Invalid ID: $id");
105 foreach ($this->fields
as $field) {
106 if (isset($apiRequest['params'][$field])) {
107 $this->records
[$id][$field] = $apiRequest['params'][$field];
111 return civicrm_api3_create_success($this->records
[$id]);
115 * @param array $apiRequest
116 * The full description of the API request.
118 * Formatted API result
119 * @throws \API_Exception
121 public function doGet($apiRequest) {
122 return _civicrm_api3_basic_array_get($apiRequest['entity'], $apiRequest['params'], $this->records
, 'id', $this->fields
);
126 * @param array $apiRequest
127 * The full description of the API request.
129 * Formatted API result
130 * @throws \API_Exception
132 public function doDelete($apiRequest) {
133 $id = @$apiRequest['params']['id'];
134 if ($id && isset($this->records
[$id])) {
135 unset($this->records
[$id]);
137 return civicrm_api3_create_success([]);