dev/core#1744 - Civi/API - Simplify event naming
[civicrm-core.git] / Civi / API / Subscriber / APIv3SchemaAdapter.php
CommitLineData
dcef11bd
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
dcef11bd 5 | |
41498ac5
TO
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 |
dcef11bd 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
dcef11bd
TO
11
12namespace Civi\API\Subscriber;
8882ff5c 13
dcef11bd
TO
14use Civi\API\Events;
15use 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 */
21class APIv3SchemaAdapter implements EventSubscriberInterface {
34f3bbd9 22
6550386a
EM
23 /**
24 * @return array
25 */
dcef11bd 26 public static function getSubscribedEvents() {
c64f69d9 27 return [
39b870b8 28 'civi.api.prepare' => [
c64f69d9
CW
29 ['onApiPrepare', Events::W_MIDDLE],
30 ['onApiPrepare_validate', Events::W_LATE],
31 ],
32 ];
dcef11bd
TO
33 }
34
6550386a
EM
35 /**
36 * @param \Civi\API\Event\PrepareEvent $event
8882ff5c 37 * API preparation event.
6550386a
EM
38 *
39 * @throws \API_Exception
40 */
dcef11bd
TO
41 public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) {
42 $apiRequest = $event->getApiRequest();
c65db512
TO
43 if ($apiRequest['version'] > 3) {
44 return;
45 }
dcef11bd
TO
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 }
8882ff5c 54 // Note: If 'id' is set then verify_mandatory will only check 'version'.
dcef11bd
TO
55 civicrm_api3_verify_mandatory($apiRequest['params'], NULL, $this->getRequired($apiRequest['fields']));
56 }
57
58 $event->setApiRequest($apiRequest);
59 }
60
6550386a
EM
61 /**
62 * @param \Civi\API\Event\Event $event
8882ff5c 63 * API preparation event.
6550386a
EM
64 *
65 * @throws \Exception
66 */
dcef11bd 67 public function onApiPrepare_validate(\Civi\API\Event\Event $event) {
8882ff5c 68 $apiRequest = $event->getApiRequest();
40557979
CW
69 if ($apiRequest['version'] > 3) {
70 return;
71 }
8882ff5c
TO
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.
dcef11bd
TO
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 /**
8882ff5c 82 * Return array of defaults for the given API (function is a wrapper on getfields).
257e7666
EM
83 * @param $fields
84 * @return array
dcef11bd
TO
85 */
86 public function getDefaults($fields) {
c64f69d9 87 $defaults = [];
dcef11bd
TO
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 /**
8882ff5c 98 * Return array of required fields for the given API (function is a wrapper on getfields).
257e7666
EM
99 * @param $fields
100 * @return array
dcef11bd
TO
101 */
102 public function getRequired($fields) {
c64f69d9 103 $required = ['version'];
dcef11bd
TO
104
105 foreach ($fields as $field => $values) {
106 if (!empty($values['api.required'])) {
107 $required[] = $field;
108 }
109 }
110 return $required;
111 }
96025800 112
6550386a 113}