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