Message api change for spec 0.9 patch by Jinhui Du
[shindig.git] / php / src / social / service / RequestItem.php
blob500a540fa68690520d7a298da399e9f7004103aa
1 <?php
2 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
21 /**
22 * Abstract base type for social API requests.
24 abstract class RequestItem {
26 // Common OpenSocial API fields
27 public static $APP_ID = "appId";
29 public static $USER_ID = "userId";
31 public static $GROUP_ID = "groupId";
33 public static $START_INDEX = "startIndex";
35 public static $COUNT = "count";
37 public static $SORT_BY = "sortBy";
38 public static $SORT_ORDER = "sortOrder";
40 public static $FILTER_BY = "filterBy";
41 public static $FILTER_OPERATION = "filterOp";
42 public static $FILTER_VALUE = "filterValue";
44 public static $FIELDS = "fields";
46 // Opensocial defaults
47 public static $DEFAULT_START_INDEX = 0;
49 public static $DEFAULT_COUNT = 20;
51 public static $APP_SUBSTITUTION_TOKEN = "@app";
53 /**
54 * @var SecurityToken
56 protected $token;
58 protected $operation;
60 protected $service;
62 public function __construct($service, $operation, SecurityToken $token) {
63 $this->service = $service;
64 $this->operation = $operation;
65 $this->token = $token;
68 public function getAppId() {
69 $appId = $this->getParameter(self::$APP_ID);
70 if ($appId != null && $appId == self::$APP_SUBSTITUTION_TOKEN) {
71 return $this->token->getAppId();
72 } else {
73 return $appId;
77 public function getUsers() {
78 $ids = $this->getListParameter(self::$USER_ID);
79 if (empty($ids)) {
80 if ($this->token->getViewerId() != null) {
81 // Assume @me
82 $ids = array("@me");
83 } else {
84 throw new IllegalArgumentException("No userId provided and viewer not available");
87 $userIds = array();
88 foreach ($ids as $id) {
89 $userIds[] = UserId::fromJson($id);
91 return $userIds;
94 public function getGroup() {
95 return GroupId::fromJson($this->getParameter(self::$GROUP_ID, "@self"));
98 public function getStartIndex() {
99 $startIndex = $this->getParameter(self::$START_INDEX);
100 if ($startIndex == null) {
101 return self::$DEFAULT_START_INDEX;
102 } elseif (is_numeric($startIndex)) {
103 return intval($startIndex);
104 } else {
105 throw new SocialSpiException("Parameter " . self::$START_INDEX . " (" . $startIndex . ") is not a number.", ResponseError::$BAD_REQUEST);
109 public function getCount() {
110 $count = $this->getParameter(self::$COUNT);
111 if ($count == null) {
112 return self::$DEFAULT_COUNT;
113 } elseif (is_numeric($count)) {
114 return intval($count);
115 } else {
116 throw new SocialSpiException("Parameter " . self::$COUNT . " (" . $count . ") is not a number.", ResponseError::$BAD_REQUEST);
120 public function getSortBy() {
121 $sortBy = $this->getParameter(self::$SORT_BY);
122 return $sortBy == null ? CollectionOptions::TOP_FRIENDS_SORT : $sortBy;
125 public function getSortOrder() {
126 $sortOrder = $this->getParameter(self::$SORT_ORDER);
127 if (empty($sortOrder)) {
128 return CollectionOptions::SORT_ORDER_ASCENDING;
129 } elseif ($sortOrder == CollectionOptions::SORT_ORDER_ASCENDING || $sortOrder == CollectionOptions::SORT_ORDER_DESCENDING) {
130 return $sortOrder;
131 } else {
132 throw new SocialSpiException("Parameter " . sef::$SORT_ORDER . " (" . $sortOrder . ") is not valid.", ResponseError::$BAD_REQUEST);
136 public function getFilterBy() {
137 return $this->getParameter(self::$FILTER_BY);
140 public function getFilterOperation() {
141 $filterOp = $this->getParameter(self::$FILTER_OPERATION);
142 if (empty($filterOp)) {
143 return CollectionOptions::FILTER_OP_CONTAINS;
144 } elseif ($filterOp == CollectionOptions::FILTER_OP_EQUALS || $filterOp == CollectionOptions::FILTER_OP_CONTAINS || $filterOp == CollectionOptions::FILTER_OP_STARTSWITH || $filterOp == CollectionOptions::FILTER_OP_PRESENT) {
145 return $filterOp;
146 } else {
147 throw new SocialSpiException("Parameter " . self::$FILTER_OPERATION . " (" . $filterOp . ") is not valid.", ResponseError::$BAD_REQUEST);
151 public function getFilterValue() {
152 $filterValue = $this->getParameter(self::$FILTER_VALUE);
153 return empty($filterValue) ? "" : $filterValue;
156 public function getFields(Array $defaultValue = array()) {
157 $result = array();
158 $fields = $this->getListParameter(self::$FIELDS);
159 if (is_array($fields)) {
160 $result = $fields;
162 if (! count($result)) {
163 return $defaultValue;
164 } else {
165 // often we get duplicate fields, remove'm
166 $cleanResult = array();
167 foreach ($result as $field) {
168 if (! in_array($field, $cleanResult)) {
169 $cleanResult[urldecode($field)] = urldecode($field);
172 $result = $cleanResult;
174 return $result;
177 public function getOperation($rpcMethod = null) {
178 return $this->operation;
181 public function getService($rpcMethod = null) {
182 return $this->service;
186 * @return SecurityToken
188 public function getToken() {
189 return $this->token;
192 public abstract function applyUrlTemplate($urlTemplate);
194 public abstract function getParameter($paramName, $defaultValue = null);
196 public abstract function getListParameter($paramName);