<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.4 |
+ | CiviCRM version 4.6 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013 |
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
-*/
+ */
namespace Civi\API;
+/**
+ * Class Request
+ * @package Civi\API
+ */
class Request {
private static $nextId = 1;
* Create a formatted/normalized request object.
*
* @param string $entity
+ * API entity name.
* @param string $action
+ * API action name.
* @param array $params
+ * API parameters.
* @param mixed $extra
+ * Who knows? ...
*
* @throws \API_Exception
- * @return array the request descriptor; keys:
+ * @return array
+ * the request descriptor; keys:
* - version: int
* - entity: string
* - action: string
$apiRequest['extra'] = $extra;
$apiRequest['fields'] = NULL;
- if ($apiRequest['version'] <= 3) {
- // APIv1-v3 munges entity/action names, which means that the same name can be written
- // multiple ways. That makes it harder to work with.
- $apiRequest['entity'] = \CRM_Utils_String::munge($entity);
- $action = \CRM_Utils_String::munge($action);
- $apiRequest['action'] = strtolower($action{0}) . substr($action, 1);
- }
- else {
- // APIv4 requires exact entity/action name; deviations should cause errors
- if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $entity)) {
- throw new \API_Exception("Malformed entity");
- }
- if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $action)) {
- throw new \API_Exception("Malformed action");
- }
- $apiRequest['entity'] = $entity;
- $apiRequest['action'] = strtolower($action{0}) . substr($action, 1);
- }
+ $apiRequest['entity'] = $entity = self::normalizeEntityName($entity, $apiRequest['version']);
+ $apiRequest['action'] = $action = self::normalizeActionName($action, $apiRequest['version']);
// APIv1-v3 mix data+options in $params which means that each API callback is responsible
// for splitting the two. In APIv4, the split is done systematically so that we don't
return $apiRequest;
}
+ /**
+ * Normalize/validate entity and action names
+ *
+ * @param string $entity
+ * @param int $version
+ * @return string
+ * @throws \API_Exception
+ */
+ public static function normalizeEntityName($entity, $version) {
+ if ($version <= 3) {
+ // APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
+ // We normalize entity to be CamelCase.
+ return \CRM_Utils_String::convertStringToCamel(\CRM_Utils_String::munge($entity));
+ }
+ else {
+ // APIv4 requires exact spelling & capitalization of entity/action name; deviations should cause errors
+ if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $entity)) {
+ throw new \API_Exception("Malformed entity");
+ }
+ return $entity;
+ }
+ }
+
+ public static function normalizeActionName($action, $version) {
+ if ($version <= 3) {
+ // APIv1-v3 munges entity/action names, and accepts any mixture of case and underscores.
+ // We normalize action to be lowercase.
+ return strtolower(\CRM_Utils_String::munge($action));
+ }
+ else {
+ // APIv4 requires exact spelling & capitalization of entity/action name; deviations should cause errors
+ if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]*$/', $action)) {
+ throw new \API_Exception("Malformed action");
+ }
+ // TODO: Not sure about camelCase actions - in v3 they are all lowercase.
+ return strtolower($action{0}) . substr($action, 1);
+ }
+ }
+
/**
* We must be sure that every request uses only one version of the API.
*
* @param array $params
+ * API parameters.
* @return int
*/
protected static function parseVersion($params) {
$desired_version = empty($params['version']) ? NULL : (int) $params['version'];
- if (isset($desired_version) && is_integer($desired_version)) {
+ if (isset($desired_version) && is_int($desired_version)) {
return $desired_version;
}
else {