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
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";
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();
77 public function getUsers() {
78 $ids = $this->getListParameter(self
::$USER_ID);
80 if ($this->token
->getViewerId() != null) {
84 throw new IllegalArgumentException("No userId provided and viewer not available");
88 foreach ($ids as $id) {
89 $userIds[] = UserId
::fromJson($id);
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);
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);
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
) {
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
) {
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()) {
158 $fields = $this->getListParameter(self
::$FIELDS);
159 if (is_array($fields)) {
162 if (! count($result)) {
163 return $defaultValue;
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;
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() {
192 public abstract function applyUrlTemplate($urlTemplate);
194 public abstract function getParameter($paramName, $defaultValue = null);
196 public abstract function getListParameter($paramName);