Civi/API - Code style
[civicrm-core.git] / Civi / API / Subscriber / APIv3SchemaAdapter.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
28 namespace Civi\API\Subscriber;
29
30 use Civi\API\Events;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
33 /**
34 * This class determines what fields are allowed for a request
35 * and validates that the fields are provided correctly.
36 */
37 class APIv3SchemaAdapter implements EventSubscriberInterface {
38 /**
39 * @return array
40 */
41 public static function getSubscribedEvents() {
42 return array(
43 Events::PREPARE => array(
44 array('onApiPrepare', Events::W_MIDDLE),
45 array('onApiPrepare_validate', Events::W_LATE),
46 ),
47 );
48 }
49
50 /**
51 * @param \Civi\API\Event\PrepareEvent $event
52 * API preparation event.
53 *
54 * @throws \API_Exception
55 */
56 public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) {
57 $apiRequest = $event->getApiRequest();
58 if ($apiRequest['version'] > 3) {
59 return;
60 }
61
62 $apiRequest['fields'] = _civicrm_api3_api_getfields($apiRequest);
63
64 _civicrm_api3_swap_out_aliases($apiRequest, $apiRequest['fields']);
65 if (strtolower($apiRequest['action']) != 'getfields') {
66 if (empty($apiRequest['params']['id'])) {
67 $apiRequest['params'] = array_merge($this->getDefaults($apiRequest['fields']), $apiRequest['params']);
68 }
69 // Note: If 'id' is set then verify_mandatory will only check 'version'.
70 civicrm_api3_verify_mandatory($apiRequest['params'], NULL, $this->getRequired($apiRequest['fields']));
71 }
72
73 $event->setApiRequest($apiRequest);
74 }
75
76 /**
77 * @param \Civi\API\Event\Event $event
78 * API preparation event.
79 *
80 * @throws \Exception
81 */
82 public function onApiPrepare_validate(\Civi\API\Event\Event $event) {
83 $apiRequest = $event->getApiRequest();
84 // Not sure why this is omitted for generic actions. It would make sense
85 // to omit 'getfields', but that's only one generic action.
86
87 if (isset($apiRequest['function']) && !$apiRequest['is_generic'] && isset($apiRequest['fields'])) {
88 _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $apiRequest['fields']);
89 $event->setApiRequest($apiRequest);
90 }
91 }
92
93 /**
94 * Return array of defaults for the given API (function is a wrapper on getfields).
95 */
96 public function getDefaults($fields) {
97 $defaults = array();
98
99 foreach ($fields as $field => $values) {
100 if (isset($values['api.default'])) {
101 $defaults[$field] = $values['api.default'];
102 }
103 }
104 return $defaults;
105 }
106
107 /**
108 * Return array of required fields for the given API (function is a wrapper on getfields).
109 */
110 public function getRequired($fields) {
111 $required = array('version');
112
113 foreach ($fields as $field => $values) {
114 if (!empty($values['api.required'])) {
115 $required[] = $field;
116 }
117 }
118 return $required;
119 }
120 }