/usr/share/php/OpenCloud/Common/Collection.php is in php-opencloud 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | <?php
/**
* Copyright 2012-2014 Rackspace US, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace OpenCloud\Common;
/**
* @deprecated
* @codeCoverageIgnore
*/
class Collection extends Base
{
private $service;
private $itemClass;
private $itemList = array();
private $pointer = 0;
private $sortKey;
private $nextPageClass;
private $nextPageCallback;
private $nextPageUrl;
/**
* A Collection is an array of objects
*
* Some assumptions:
* * The `Collection` class assumes that there exists on its service
* a factory method with the same name of the class. For example, if
* you create a Collection of class `Foobar`, it will attempt to call
* the method `parent::Foobar()` to create instances of that class.
* * It assumes that the factory method can take an array of values, and
* it passes that to the method.
*
* @param Service $service - the service associated with the collection
* @param string $itemclass - the Class of each item in the collection
* (assumed to be the name of the factory method)
* @param array $arr - the input array
*/
public function __construct($service, $class, array $array = array())
{
$service->getLogger()->deprecated(__METHOD__, 'OpenCloud\Common\Collection\CollectionBuilder');
$this->setService($service);
$this->setNextPageClass($class);
// If they've supplied a FQCN, only get the last part
$class = (false !== ($classNamePos = strrpos($class, '\\')))
? substr($class, $classNamePos + 1)
: $class;
$this->setItemClass($class);
// Set data
$this->setItemList($array);
}
/**
* Set the entire data array.
*
* @param array $array
*/
private function setItemList(array $array)
{
$this->itemList = $array;
return $this;
}
/**
* Retrieve the entire data array.
*
* @return array
*/
public function getItemList()
{
return $this->itemList;
}
/**
* Set the service.
*
* @param Service|PersistentObject $service
*/
public function setService($service)
{
$this->service = $service;
return $this;
}
/**
* Retrieves the service associated with the Collection
*
* @return Service
*/
public function getService()
{
return $this->service;
}
/**
* Set the resource class name.
*/
private function setItemClass($itemClass)
{
$this->itemClass = $itemClass;
return $this;
}
/**
* Get item class.
*/
private function getItemClass()
{
return $this->itemClass;
}
/**
* Set the key that will be used for sorting.
*/
private function setSortKey($sortKey)
{
$this->sortKey = $sortKey;
return $this;
}
/**
* Get the key that will be used for sorting.
*/
private function getSortKey()
{
return $this->sortKey;
}
/**
* Set next page class.
*/
private function setNextPageClass($nextPageClass)
{
$this->nextPageClass = $nextPageClass;
return $this;
}
/**
* Get next page class.
*/
private function getNextPageClass()
{
return $this->nextPageClass;
}
/**
* for paginated collection, sets the callback function and URL for
* the next page
*
* The callback function should have the signature:
*
* function Whatever($class, $url, $parent)
*
* and the `$url` should be the URL of the next page of results
*
* @param callable $callback the name of the function (or array of
* object, function name)
* @param string $url the URL of the next page of results
* @return void
*/
public function setNextPageCallback($callback, $url)
{
$this->nextPageCallback = $callback;
$this->nextPageUrl = $url;
return $this;
}
/**
* Get next page callback.
*/
private function getNextPageCallback()
{
return $this->nextPageCallback;
}
/**
* Get next page URL.
*/
private function getNextPageUrl()
{
return $this->nextPageUrl;
}
/**
* Returns the number of items in the collection
*
* For most services, this is the total number of items. If the Collection
* is paginated, however, this only returns the count of items in the
* current page of data.
*
* @return int
*/
public function count()
{
return count($this->getItemList());
}
/**
* Pseudonym for count()
*
* @codeCoverageIgnore
*/
public function size()
{
return $this->count();
}
/**
* Resets the pointer to the beginning, but does NOT return the first item
*
* @api
* @return void
*/
public function reset()
{
$this->pointer = 0;
}
/**
* Resets the collection pointer back to the first item in the page
* and returns it
*
* This is useful if you're only interested in the first item in the page.
*
* @api
* @return Base the first item in the set
*/
public function first()
{
$this->reset();
return $this->next();
}
/**
* Return the item at a particular point of the array.
*
* @param mixed $offset
* @return mixed
*/
public function getItem($pointer)
{
return (isset($this->itemList[$pointer])) ? $this->itemList[$pointer] : false;
}
/**
* Add an item to this collection
*
* @param mixed $item
*/
public function addItem($item)
{
$this->itemList[] = $item;
}
/**
* Returns the next item in the page
*
* @api
* @return Base the next item or FALSE if at the end of the page
*/
public function next()
{
if ($this->pointer >= $this->count()) {
return false;
}
$data = $this->getItem($this->pointer++);
$class = $this->getItemClass();
// Are there specific methods in the parent/service that can be used to
// instantiate the resource? Currently supported: getResource(), resource()
foreach (array($class, 'get' . ucfirst($class)) as $method) {
if (method_exists($this->service, $method)) {
return call_user_func(array($this->service, $method), $data);
}
}
// Backup method
if (method_exists($this->service, 'resource')) {
return $this->service->resource($class, $data);
}
return false;
}
/**
* sorts the collection on a specified key
*
* Note: only top-level keys can be used as the sort key. Note that this
* only sorts the data in the current page of the Collection (for
* multi-page data).
*
* @api
* @param string $keyname the name of the field to use as the sort key
* @return void
*/
public function sort($keyname = 'id')
{
$this->setSortKey($keyname);
usort($this->itemList, array($this, 'sortCompare'));
}
/**
* selects only specified items from the Collection
*
* This provides a simple form of filtering on Collections. For each item
* in the collection, it calls the callback function, passing it the item.
* If the callback returns `TRUE`, then the item is retained; if it returns
* `FALSE`, then the item is deleted from the collection.
*
* Note that this should not supersede server-side filtering; the
* `Collection::Select()` method requires that *all* of the data for the
* Collection be retrieved from the server before the filtering is
* performed; this can be very inefficient, especially for large data
* sets. This method is mostly useful on smaller-sized sets.
*
* Example:
* <code>
* $services = $connection->ServiceList();
* $services->Select(function ($item) { return $item->region=='ORD';});
* // now the $services Collection only has items from the ORD region
* </code>
*
* `Select()` is *destructive*; that is, it actually removes entries from
* the collection. For example, if you use `Select()` to find items with
* the ID > 10, then use it again to find items that are <= 10, it will
* return an empty list.
*
* @api
* @param callable $testfunc a callback function that is passed each item
* in turn. Note that `Select()` performs an explicit test for
* `FALSE`, so functions like `strpos()` need to be cast into a
* boolean value (and not just return the integer).
* @returns void
* @throws DomainError if callback doesn't return a boolean value
*/
public function select($testfunc)
{
foreach ($this->getItemList() as $index => $item) {
$test = call_user_func($testfunc, $item);
if (!is_bool($test)) {
throw new Exceptions\DomainError(
Lang::translate('Callback function for Collection::Select() did not return boolean')
);
}
if ($test === false) {
unset($this->itemList[$index]);
}
}
}
/**
* returns the Collection object for the next page of results, or
* FALSE if there are no more pages
*
* Generally, the structure for a multi-page collection will look like
* this:
*
* $coll = $obj->Collection();
* do {
* while ($item = $coll->Next()) {
* // do something with the item
* }
* } while ($coll = $coll->NextPage());
*
* @api
* @return Collection if there are more pages of results, otherwise FALSE
*/
public function nextPage()
{
return ($this->getNextPageUrl() !== null)
? call_user_func($this->getNextPageCallback(), $this->getNextPageClass(), $this->getNextPageUrl())
: false;
}
/**
* Compares two values of sort keys
*/
private function sortCompare($a, $b)
{
$key = $this->getSortKey();
// Handle strings
if (is_string($a->$key)) {
return strcmp($a->$key, $b->$key);
}
// Handle others with logical comparisons
if ($a->$key == $b->$key) {
return 0;
} elseif ($a->$key < $b->$key) {
return -1;
} else {
return 1;
}
}
}
|