Commit | Line | Data |
---|---|---|
17b9f7be TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
17b9f7be | 5 | | | |
41498ac5 TO |
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 | | |
17b9f7be | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
17b9f7be TO |
11 | |
12 | namespace Civi\API\Provider; | |
13 | ||
14 | use Civi\API\Events; | |
15 | ||
16 | /** | |
8882ff5c TO |
17 | * A static provider is useful for creating mock API implementations which |
18 | * manages records in-memory. | |
17b9f7be | 19 | * |
8882ff5c TO |
20 | * TODO Add a static provider to SyntaxConformanceTest to ensure that it's |
21 | * representative. | |
17b9f7be TO |
22 | */ |
23 | class StaticProvider extends AdhocProvider { | |
24 | protected $records; | |
25 | protected $fields; | |
26 | ||
8882ff5c TO |
27 | /** |
28 | * @return array | |
29 | */ | |
17b9f7be | 30 | public static function getSubscribedEvents() { |
c64f69d9 | 31 | return [ |
39b870b8 | 32 | 'civi.api.resolve' => [ |
c64f69d9 CW |
33 | ['onApiResolve', Events::W_MIDDLE], |
34 | ], | |
39b870b8 | 35 | 'civi.api.authorize' => [ |
c64f69d9 CW |
36 | ['onApiAuthorize', Events::W_MIDDLE], |
37 | ], | |
38 | ]; | |
17b9f7be TO |
39 | } |
40 | ||
8882ff5c TO |
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 | */ | |
c64f69d9 | 53 | public function __construct($version, $entity, $fields, $perms = [], $records = []) { |
17b9f7be TO |
54 | parent::__construct($version, $entity); |
55 | ||
c64f69d9 | 56 | $perms = array_merge([ |
17b9f7be TO |
57 | 'create' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, |
58 | 'get' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, | |
59 | 'delete' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, | |
c64f69d9 | 60 | ], $perms); |
17b9f7be | 61 | |
c64f69d9 | 62 | $this->records = \CRM_Utils_Array::index(['id'], $records); |
17b9f7be TO |
63 | $this->fields = $fields; |
64 | ||
c64f69d9 CW |
65 | $this->addAction('create', $perms['create'], [$this, 'doCreate']); |
66 | $this->addAction('get', $perms['get'], [$this, 'doGet']); | |
67 | $this->addAction('delete', $perms['delete'], [$this, 'doDelete']); | |
17b9f7be TO |
68 | } |
69 | ||
70 | /** | |
71 | * @return array | |
72 | */ | |
73 | public function getRecords() { | |
74 | return $this->records; | |
75 | } | |
76 | ||
77 | /** | |
78 | * @param array $records | |
8882ff5c | 79 | * List of mock records to be read/updated by API calls. |
17b9f7be TO |
80 | */ |
81 | public function setRecords($records) { | |
82 | $this->records = $records; | |
83 | } | |
84 | ||
8882ff5c TO |
85 | /** |
86 | * @param array $apiRequest | |
87 | * The full description of the API request. | |
88 | * @return array | |
34f3bbd9 | 89 | * Formatted API result |
8882ff5c TO |
90 | * @throws \API_Exception |
91 | */ | |
92 | public function doCreate($apiRequest) { | |
17b9f7be TO |
93 | if (isset($apiRequest['params']['id'])) { |
94 | $id = $apiRequest['params']['id']; | |
95 | } | |
96 | else { | |
97 | $id = max(array_keys($this->records)) + 1; | |
c64f69d9 | 98 | $this->records[$id] = []; |
17b9f7be TO |
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 | ||
8882ff5c TO |
114 | /** |
115 | * @param array $apiRequest | |
116 | * The full description of the API request. | |
117 | * @return array | |
34f3bbd9 | 118 | * Formatted API result |
8882ff5c TO |
119 | * @throws \API_Exception |
120 | */ | |
121 | public function doGet($apiRequest) { | |
66ea2662 | 122 | return _civicrm_api3_basic_array_get($apiRequest['entity'], $apiRequest['params'], $this->records, 'id', $this->fields); |
17b9f7be TO |
123 | } |
124 | ||
8882ff5c TO |
125 | /** |
126 | * @param array $apiRequest | |
127 | * The full description of the API request. | |
128 | * @return array | |
34f3bbd9 | 129 | * Formatted API result |
8882ff5c TO |
130 | * @throws \API_Exception |
131 | */ | |
132 | public function doDelete($apiRequest) { | |
17b9f7be TO |
133 | $id = @$apiRequest['params']['id']; |
134 | if ($id && isset($this->records[$id])) { | |
135 | unset($this->records[$id]); | |
136 | } | |
c64f69d9 | 137 | return civicrm_api3_create_success([]); |
17b9f7be | 138 | } |
96025800 | 139 | |
17b9f7be | 140 | } |