Civi/API - Code style
[civicrm-core.git] / Civi / API / Provider / StaticProvider.php
CommitLineData
17b9f7be
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
17b9f7be 5 +--------------------------------------------------------------------+
39de6fd5 6 | Copyright CiviCRM LLC (c) 2004-2014 |
17b9f7be
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28namespace Civi\API\Provider;
29
30use Civi\API\Events;
31
32/**
8882ff5c
TO
33 * A static provider is useful for creating mock API implementations which
34 * manages records in-memory.
17b9f7be 35 *
8882ff5c
TO
36 * TODO Add a static provider to SyntaxConformanceTest to ensure that it's
37 * representative.
17b9f7be
TO
38 */
39class StaticProvider extends AdhocProvider {
40 protected $records;
41 protected $fields;
42
8882ff5c
TO
43 /**
44 * @return array
45 */
17b9f7be
TO
46 public static function getSubscribedEvents() {
47 return array(
48 Events::RESOLVE => array(
49 array('onApiResolve', Events::W_MIDDLE),
50 ),
51 Events::AUTHORIZE => array(
52 array('onApiAuthorize', Events::W_MIDDLE),
53 ),
54 );
55 }
56
8882ff5c
TO
57 /**
58 * @param int $version
59 * API version.
60 * @param string $entity
61 * API entity.
62 * @param array $fields
63 * List of fields in this fake entity.
64 * @param array $perms
65 * Array(string $action => string $perm).
66 * @param array $records
67 * List of mock records to be read/updated by API calls.
68 */
17b9f7be
TO
69 public function __construct($version, $entity, $fields, $perms = array(), $records = array()) {
70 parent::__construct($version, $entity);
71
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,
76 ), $perms);
77
78 $this->records = \CRM_Utils_Array::index(array('id'), $records);
79 $this->fields = $fields;
80
8882ff5c
TO
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'));
17b9f7be
TO
84 }
85
86 /**
87 * @return array
88 */
89 public function getRecords() {
90 return $this->records;
91 }
92
93 /**
94 * @param array $records
8882ff5c 95 * List of mock records to be read/updated by API calls.
17b9f7be
TO
96 */
97 public function setRecords($records) {
98 $this->records = $records;
99 }
100
8882ff5c
TO
101 /**
102 * @param array $apiRequest
103 * The full description of the API request.
104 * @return array
105 * Formatted API result
106 * @throws \API_Exception
107 */
108 public function doCreate($apiRequest) {
17b9f7be
TO
109 if (isset($apiRequest['params']['id'])) {
110 $id = $apiRequest['params']['id'];
111 }
112 else {
113 $id = max(array_keys($this->records)) + 1;
114 $this->records[$id] = array();
115 }
116
117 if (!isset($this->records[$id])) {
118 throw new \API_Exception("Invalid ID: $id");
119 }
120
121 foreach ($this->fields as $field) {
122 if (isset($apiRequest['params'][$field])) {
123 $this->records[$id][$field] = $apiRequest['params'][$field];
124 }
125 }
126
127 return civicrm_api3_create_success($this->records[$id]);
128 }
129
8882ff5c
TO
130 /**
131 * @param array $apiRequest
132 * The full description of the API request.
133 * @return array
134 * Formatted API result
135 * @throws \API_Exception
136 */
137 public function doGet($apiRequest) {
17b9f7be
TO
138 $id = @$apiRequest['params']['id'];
139 if ($id && isset($this->records[$id])) {
140 return civicrm_api3_create_success(array($id => $this->records[$id]));
141 }
142 else {
143 return civicrm_api3_create_success(array());
144 }
145 }
146
8882ff5c
TO
147 /**
148 * @param array $apiRequest
149 * The full description of the API request.
150 * @return array
151 * Formatted API result
152 * @throws \API_Exception
153 */
154 public function doDelete($apiRequest) {
17b9f7be
TO
155 $id = @$apiRequest['params']['id'];
156 if ($id && isset($this->records[$id])) {
157 unset($this->records[$id]);
158 }
159 return civicrm_api3_create_success(array());
160 }
161}