INFRA-132 - Drupal.Classes.ClassDeclaration
[civicrm-core.git] / Civi / API / Request.php
CommitLineData
d3159a21
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
d3159a21 5 +--------------------------------------------------------------------+
39de6fd5 6 | Copyright CiviCRM LLC (c) 2004-2014 |
d3159a21
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 +--------------------------------------------------------------------+
26*/
27namespace Civi\API;
28
6550386a
EM
29/**
30 * Class Request
31 * @package Civi\API
32 */
d3159a21 33class Request {
5558f278
TO
34 private static $nextId = 1;
35
d3159a21
TO
36 /**
37 * Create a formatted/normalized request object.
38 *
39 * @param string $entity
8882ff5c 40 * API entity name.
d3159a21 41 * @param string $action
8882ff5c 42 * API action name.
d3159a21 43 * @param array $params
8882ff5c 44 * API parameters.
d3159a21 45 * @param mixed $extra
8882ff5c 46 * Who knows? ...
89750f35
EM
47 *
48 * @throws \API_Exception
a6c01b45
CW
49 * @return array
50 * the request descriptor; keys:
d3159a21
TO
51 * - version: int
52 * - entity: string
53 * - action: string
54 * - params: array (string $key => mixed $value) [deprecated in v4]
55 * - extra: unspecified
56 * - fields: NULL|array (string $key => array $fieldSpec)
57 * - options: \CRM_Utils_OptionBag derived from params [v4-only]
58 * - data: \CRM_Utils_OptionBag derived from params [v4-only]
59 * - chains: unspecified derived from params [v4-only]
60 */
61 public static function create($entity, $action, $params, $extra) {
62 $apiRequest = array(); // new \Civi\API\Request();
5558f278 63 $apiRequest['id'] = self::$nextId++;
d3159a21
TO
64 $apiRequest['version'] = self::parseVersion($params);
65 $apiRequest['params'] = $params;
66 $apiRequest['extra'] = $extra;
67 $apiRequest['fields'] = NULL;
68
69 if ($apiRequest['version'] <= 3) {
70 // APIv1-v3 munges entity/action names, which means that the same name can be written
71 // multiple ways. That makes it harder to work with.
72 $apiRequest['entity'] = \CRM_Utils_String::munge($entity);
73 $action = \CRM_Utils_String::munge($action);
74 $apiRequest['action'] = strtolower($action{0}) . substr($action, 1);
75 }
76 else {
77 // APIv4 requires exact entity/action name; deviations should cause errors
78 if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $entity)) {
79 throw new \API_Exception("Malformed entity");
80 }
81 if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $action)) {
82 throw new \API_Exception("Malformed action");
83 }
84 $apiRequest['entity'] = $entity;
85 $apiRequest['action'] = strtolower($action{0}) . substr($action, 1);
86 }
87
88 // APIv1-v3 mix data+options in $params which means that each API callback is responsible
89 // for splitting the two. In APIv4, the split is done systematically so that we don't
90 // so much parsing logic spread around.
91 if ($apiRequest['version'] >= 4) {
92 $options = array();
93 $data = array();
94 $chains = array();
95 foreach ($params as $key => $value) {
96 if ($key == 'options') {
97 $options = array_merge($options, $value);
98 }
99 elseif ($key == 'return') {
100 if (!isset($options['return'])) {
101 $options['return'] = array();
102 }
103 $options['return'] = array_merge($options['return'], $value);
104 }
105 elseif (preg_match('/^option\.(.*)$/', $key, $matches)) {
106 $options[$matches[1]] = $value;
107 }
108 elseif (preg_match('/^return\.(.*)$/', $key, $matches)) {
109 if ($value) {
110 if (!isset($options['return'])) {
111 $options['return'] = array();
112 }
113 $options['return'][] = $matches[1];
114 }
115 }
116 elseif (preg_match('/^format\.(.*)$/', $key, $matches)) {
117 if ($value) {
118 if (!isset($options['format'])) {
119 $options['format'] = $matches[1];
120 }
121 else {
122 throw new \API_Exception("Too many API formats specified");
123 }
124 }
125 }
126 elseif (preg_match('/^api\./', $key)) {
127 // FIXME: represent subrequests as instances of "Request"
128 $chains[$key] = $value;
129 }
130 elseif ($key == 'debug') {
131 $options['debug'] = $value;
132 }
133 elseif ($key == 'version') {
134 // ignore
135 }
136 else {
137 $data[$key] = $value;
138
139 }
140 }
141 $apiRequest['options'] = new \CRM_Utils_OptionBag($options);
142 $apiRequest['data'] = new \CRM_Utils_OptionBag($data);
143 $apiRequest['chains'] = $chains;
144 }
145
146 return $apiRequest;
147 }
148
149 /**
150 * We must be sure that every request uses only one version of the API.
151 *
152 * @param array $params
8882ff5c 153 * API parameters.
d3159a21
TO
154 * @return int
155 */
156 protected static function parseVersion($params) {
157 $desired_version = empty($params['version']) ? NULL : (int) $params['version'];
8882ff5c 158 if (isset($desired_version) && is_int($desired_version)) {
d3159a21
TO
159 return $desired_version;
160 }
161 else {
162 // we will set the default to version 3 as soon as we find that it works.
163 return 3;
164 }
165 }
166
89750f35 167}