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