3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 namespace Civi\API\Subscriber
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * This class determines what fields are allowed for a request
35 * and validates that the fields are provided correctly.
37 class APIv3SchemaAdapter
implements EventSubscriberInterface
{
42 public static function getSubscribedEvents() {
45 ['onApiPrepare', Events
::W_MIDDLE
],
46 ['onApiPrepare_validate', Events
::W_LATE
],
52 * @param \Civi\API\Event\PrepareEvent $event
53 * API preparation event.
55 * @throws \API_Exception
57 public function onApiPrepare(\Civi\API\Event\PrepareEvent
$event) {
58 $apiRequest = $event->getApiRequest();
59 if ($apiRequest['version'] > 3) {
63 $apiRequest['fields'] = _civicrm_api3_api_getfields($apiRequest);
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']);
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']));
74 $event->setApiRequest($apiRequest);
78 * @param \Civi\API\Event\Event $event
79 * API preparation event.
83 public function onApiPrepare_validate(\Civi\API\Event\Event
$event) {
84 $apiRequest = $event->getApiRequest();
85 if ($apiRequest['version'] > 3) {
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.
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);
98 * Return array of defaults for the given API (function is a wrapper on getfields).
102 public function getDefaults($fields) {
105 foreach ($fields as $field => $values) {
106 if (isset($values['api.default'])) {
107 $defaults[$field] = $values['api.default'];
114 * Return array of required fields for the given API (function is a wrapper on getfields).
118 public function getRequired($fields) {
119 $required = ['version'];
121 foreach ($fields as $field => $values) {
122 if (!empty($values['api.required'])) {
123 $required[] = $field;