3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 namespace Civi\API\Provider
;
33 * A static provider is useful for creating mock API implementations which
34 * manages records in-memory.
36 * TODO Add a static provider to SyntaxConformanceTest to ensure that it's
39 class StaticProvider
extends AdhocProvider
{
46 public static function getSubscribedEvents() {
48 Events
::RESOLVE
=> array(
49 array('onApiResolve', Events
::W_MIDDLE
),
51 Events
::AUTHORIZE
=> array(
52 array('onApiAuthorize', Events
::W_MIDDLE
),
60 * @param string $entity
62 * @param array $fields
63 * List of fields in this fake entity.
65 * Array(string $action => string $perm).
66 * @param array $records
67 * List of mock records to be read/updated by API calls.
69 public function __construct($version, $entity, $fields, $perms = array(), $records = array()) {
70 parent
::__construct($version, $entity);
72 $perms = array_merge(array(
73 'create' => \CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
,
74 'get' => \CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
,
75 'delete' => \CRM_Core_Permission
::ALWAYS_ALLOW_PERMISSION
,
78 $this->records
= \CRM_Utils_Array
::index(array('id'), $records);
79 $this->fields
= $fields;
81 $this->addAction('create', $perms['create'], array($this, 'doCreate'));
82 $this->addAction('get', $perms['get'], array($this, 'doGet'));
83 $this->addAction('delete', $perms['delete'], array($this, 'doDelete'));
89 public function getRecords() {
90 return $this->records
;
94 * @param array $records
95 * List of mock records to be read/updated by API calls.
97 public function setRecords($records) {
98 $this->records
= $records;
102 * @param array $apiRequest
103 * The full description of the API request.
105 * Formatted API result
106 * @throws \API_Exception
108 public function doCreate($apiRequest) {
109 if (isset($apiRequest['params']['id'])) {
110 $id = $apiRequest['params']['id'];
113 $id = max(array_keys($this->records
)) +
1;
114 $this->records
[$id] = array();
117 if (!isset($this->records
[$id])) {
118 throw new \
API_Exception("Invalid ID: $id");
121 foreach ($this->fields
as $field) {
122 if (isset($apiRequest['params'][$field])) {
123 $this->records
[$id][$field] = $apiRequest['params'][$field];
127 return civicrm_api3_create_success($this->records
[$id]);
131 * @param array $apiRequest
132 * The full description of the API request.
134 * Formatted API result
135 * @throws \API_Exception
137 public function doGet($apiRequest) {
138 return _civicrm_api3_basic_array_get($apiRequest['entity'], $apiRequest['params'], $this->records
, 'id', $this->fields
);
142 * @param array $apiRequest
143 * The full description of the API request.
145 * Formatted API result
146 * @throws \API_Exception
148 public function doDelete($apiRequest) {
149 $id = @$apiRequest['params']['id'];
150 if ($id && isset($this->records
[$id])) {
151 unset($this->records
[$id]);
153 return civicrm_api3_create_success(array());