Blame view

sources/3rdparty/rackspace/php-opencloud/lib/OpenCloud/Common/Collection.php 10.6 KB
31b7f2792   Kload   Upgrade to ownclo...
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
3
4
5
6
7
8
  /**
   * PHP OpenCloud library.
   * 
   * @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
   * @license   https://www.apache.org/licenses/LICENSE-2.0
   * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
   */
31b7f2792   Kload   Upgrade to ownclo...
9
10
11
12
  
  namespace OpenCloud\Common;
  
  /**
6d9380f96   Cédric Dupont   Update sources OC...
13
14
   * @deprecated
   * @codeCoverageIgnore
31b7f2792   Kload   Upgrade to ownclo...
15
   */
6d9380f96   Cédric Dupont   Update sources OC...
16
  class Collection extends Base
31b7f2792   Kload   Upgrade to ownclo...
17
18
19
  {
  
      private $service;
6d9380f96   Cédric Dupont   Update sources OC...
20
21
      private $itemClass;
      private $itemList = array();
31b7f2792   Kload   Upgrade to ownclo...
22
      private $pointer = 0;
6d9380f96   Cédric Dupont   Update sources OC...
23
24
25
26
      private $sortKey;
      private $nextPageClass;
      private $nextPageCallback;
      private $nextPageUrl;
31b7f2792   Kload   Upgrade to ownclo...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
      /**
       * 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
       */
6d9380f96   Cédric Dupont   Update sources OC...
44
      public function __construct($service, $class, array $array = array())
31b7f2792   Kload   Upgrade to ownclo...
45
      {
6d9380f96   Cédric Dupont   Update sources OC...
46
          $service->getLogger()->deprecated(__METHOD__, 'OpenCloud\Common\Collection\CollectionBuilder');
31b7f2792   Kload   Upgrade to ownclo...
47

6d9380f96   Cédric Dupont   Update sources OC...
48
          $this->setService($service);
31b7f2792   Kload   Upgrade to ownclo...
49

6d9380f96   Cédric Dupont   Update sources OC...
50
          $this->setNextPageClass($class);
31b7f2792   Kload   Upgrade to ownclo...
51

6d9380f96   Cédric Dupont   Update sources OC...
52
53
54
55
          // If they've supplied a FQCN, only get the last part
          $class = (false !== ($classNamePos = strrpos($class, '\\')))
              ? substr($class, $classNamePos + 1)
              : $class;
31b7f2792   Kload   Upgrade to ownclo...
56

6d9380f96   Cédric Dupont   Update sources OC...
57
          $this->setItemClass($class);
31b7f2792   Kload   Upgrade to ownclo...
58

6d9380f96   Cédric Dupont   Update sources OC...
59
          // Set data
31b7f2792   Kload   Upgrade to ownclo...
60
61
          $this->setItemList($array);
      }
6d9380f96   Cédric Dupont   Update sources OC...
62

31b7f2792   Kload   Upgrade to ownclo...
63
64
      /**
       * Set the entire data array.
6d9380f96   Cédric Dupont   Update sources OC...
65
       *
31b7f2792   Kload   Upgrade to ownclo...
66
67
       * @param array $array
       */
6d9380f96   Cédric Dupont   Update sources OC...
68
      private function setItemList(array $array)
31b7f2792   Kload   Upgrade to ownclo...
69
      {
6d9380f96   Cédric Dupont   Update sources OC...
70
71
          $this->itemList = $array;
          return $this;
31b7f2792   Kload   Upgrade to ownclo...
72
      }
6d9380f96   Cédric Dupont   Update sources OC...
73

31b7f2792   Kload   Upgrade to ownclo...
74
75
      /**
       * Retrieve the entire data array.
6d9380f96   Cédric Dupont   Update sources OC...
76
       *
31b7f2792   Kload   Upgrade to ownclo...
77
78
79
80
       * @return array
       */
      public function getItemList()
      {
6d9380f96   Cédric Dupont   Update sources OC...
81
          return $this->itemList;
31b7f2792   Kload   Upgrade to ownclo...
82
      }
6d9380f96   Cédric Dupont   Update sources OC...
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
  
      /**
       * 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;
      }
31b7f2792   Kload   Upgrade to ownclo...
193
194
195
196
197
198
      /**
       * 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.
6d9380f96   Cédric Dupont   Update sources OC...
199
       *
31b7f2792   Kload   Upgrade to ownclo...
200
201
202
203
       * @return int
       */
      public function count()
      {
6d9380f96   Cédric Dupont   Update sources OC...
204
          return count($this->getItemList());
31b7f2792   Kload   Upgrade to ownclo...
205
      }
6d9380f96   Cédric Dupont   Update sources OC...
206

31b7f2792   Kload   Upgrade to ownclo...
207
208
      /**
       * Pseudonym for count()
6d9380f96   Cédric Dupont   Update sources OC...
209
       *
31b7f2792   Kload   Upgrade to ownclo...
210
211
       * @codeCoverageIgnore
       */
6d9380f96   Cédric Dupont   Update sources OC...
212
      public function size()
31b7f2792   Kload   Upgrade to ownclo...
213
214
215
216
217
      {
          return $this->count();
      }
  
      /**
31b7f2792   Kload   Upgrade to ownclo...
218
219
220
221
222
       * Resets the pointer to the beginning, but does NOT return the first item
       *
       * @api
       * @return void
       */
6d9380f96   Cédric Dupont   Update sources OC...
223
      public function reset()
31b7f2792   Kload   Upgrade to ownclo...
224
225
226
227
228
229
230
231
232
233
234
235
236
      {
          $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
       */
6d9380f96   Cédric Dupont   Update sources OC...
237
      public function first()
31b7f2792   Kload   Upgrade to ownclo...
238
239
240
241
242
243
      {
          $this->reset();
          return $this->next();
      }
  
      /**
6d9380f96   Cédric Dupont   Update sources OC...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
       * 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;
      }
  
      /**
31b7f2792   Kload   Upgrade to ownclo...
265
266
267
268
269
       * Returns the next item in the page
       *
       * @api
       * @return Base the next item or FALSE if at the end of the page
       */
6d9380f96   Cédric Dupont   Update sources OC...
270
      public function next()
31b7f2792   Kload   Upgrade to ownclo...
271
272
273
274
      {
          if ($this->pointer >= $this->count()) {
              return false;
          }
6d9380f96   Cédric Dupont   Update sources OC...
275
276
277
278
279
280
281
282
283
284
  
          $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);
              }
31b7f2792   Kload   Upgrade to ownclo...
285
          }
6d9380f96   Cédric Dupont   Update sources OC...
286
287
288
289
290
  
          // Backup method
          if (method_exists($this->service, 'resource')) {
              return $this->service->resource($class, $data);
          }
31b7f2792   Kload   Upgrade to ownclo...
291
          return false;
31b7f2792   Kload   Upgrade to ownclo...
292
293
294
295
296
297
298
299
300
301
302
303
304
      }
  
      /**
       * 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
       */
6d9380f96   Cédric Dupont   Update sources OC...
305
      public function sort($keyname = 'id')
31b7f2792   Kload   Upgrade to ownclo...
306
      {
6d9380f96   Cédric Dupont   Update sources OC...
307
308
          $this->setSortKey($keyname);
          usort($this->itemList, array($this, 'sortCompare'));
31b7f2792   Kload   Upgrade to ownclo...
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
      }
  
      /**
       * 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
       */
6d9380f96   Cédric Dupont   Update sources OC...
345
      public function select($testfunc)
31b7f2792   Kload   Upgrade to ownclo...
346
347
348
349
350
351
352
353
354
      {
          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) {
6d9380f96   Cédric Dupont   Update sources OC...
355
                  unset($this->itemList[$index]);
31b7f2792   Kload   Upgrade to ownclo...
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
              }
          }
      }
  
      /**
       * 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
       */
6d9380f96   Cédric Dupont   Update sources OC...
377
      public function nextPage()
31b7f2792   Kload   Upgrade to ownclo...
378
      {
6d9380f96   Cédric Dupont   Update sources OC...
379
380
381
          return ($this->getNextPageUrl() !== null)
              ? call_user_func($this->getNextPageCallback(), $this->getNextPageClass(), $this->getNextPageUrl())
              : false;
31b7f2792   Kload   Upgrade to ownclo...
382
383
384
385
386
      }
  
      /**
       * Compares two values of sort keys
       */
6d9380f96   Cédric Dupont   Update sources OC...
387
      private function sortCompare($a, $b)
31b7f2792   Kload   Upgrade to ownclo...
388
      {
6d9380f96   Cédric Dupont   Update sources OC...
389
          $key = $this->getSortKey();
31b7f2792   Kload   Upgrade to ownclo...
390

6d9380f96   Cédric Dupont   Update sources OC...
391
          // Handle strings
31b7f2792   Kload   Upgrade to ownclo...
392
393
394
          if (is_string($a->$key)) {
              return strcmp($a->$key, $b->$key);
          }
6d9380f96   Cédric Dupont   Update sources OC...
395
          // Handle others with logical comparisons
31b7f2792   Kload   Upgrade to ownclo...
396
397
          if ($a->$key == $b->$key) {
              return 0;
6d9380f96   Cédric Dupont   Update sources OC...
398
          } elseif ($a->$key < $b->$key) {
31b7f2792   Kload   Upgrade to ownclo...
399
400
401
402
403
              return -1;
          } else {
              return 1;
          }
      }
6d9380f96   Cédric Dupont   Update sources OC...
404
  }