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