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