OpenCloud/Common/Collection.php

Classes

Collection

Properties

 
 
 
No 
No 

Description

Provides an abstraction for working with ordered sets of objects
Collection objects are used whenever there are multiples; for example, multiple objects in a container, or multiple servers in a service.

Methods

First, Next, NextPage, Reset, Select, Service, SetNextPageCallback, Size, Sort, __construct, count, getAllItems, getItemList, sortCompare,

First( ) : \OpenCloud\Common\Base

Description

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.

Return value

Type Description
\OpenCloud\Common\Base the first item in the set

Tags

Name Description
api

Next( ) : \OpenCloud\Common\Base

Description

Returns the next item in the page

Return value

Type Description
\OpenCloud\Common\Base the next item or FALSE if at the end of the page

Tags

Name Description
api

NextPage( ) : \OpenCloud\Common\Collection

Description

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());

Return value

Type Description
\OpenCloud\Common\Collection if there are more pages of results, otherwise FALSE

Tags

Name Description
api

Reset( ) : void

Description

Resets the pointer to the beginning, but does NOT return the first item

Return value

Type Description
void

Tags

Name Description
api

Select( callable   $testfunc, ) : n/a

Description

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: $services = $connection->ServiceList(); $services->Select(function($item){ return $item->region=='ORD';}); // now the $services Collection only has items from the ORD region `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.

Arguments

Name Type Description Default
$testfunc callable

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).

Return value

Type Description
n/a n/a

Tags

Name Description
api
returns void
throws if callback doesn't return a boolean value

Service( ) : \OpenCloud\Common\Service

Description

Retrieves the service associated with the Collection

Return value

Type Description
\OpenCloud\Common\Service

SetNextPageCallback( callable   $callback, string   $url, ) : void

Description

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

Arguments

Name Type Description Default
$callback callable

the name of the function (or array of object, function name)

$url string

the URL of the next page of results

Return value

Type Description
void

Size( ) : integer

Description

Returns the number of items in the page
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 value

Type Description
integer The number of items in the set

Tags

Name Description
api

Sort( string   $keyname = 'id', ) : void

Description

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).

Arguments

Name Type Description Default
$keyname string

the name of the field to use as the sort key

'id'

Return value

Type Description
void

Tags

Name Description
api

__construct( \OpenCloud\Common\Service   $service, string   $itemclass, array   $arr, ) : n/a

Description

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.

Arguments

Name Type Description Default
$service \OpenCloud\Common\Service
  • the service associated with the collection
$itemclass string
  • the Class of each item in the collection (assumed to be the name of the factory method)
$arr array
  • the input array

Return value

Type Description
n/a n/a

count( ) : n/a

Return value

Type Description
n/a n/a

getAllItems( ) : n/a

Return value

Type Description
n/a n/a

getItemList( ) : n/a

Return value

Type Description
n/a n/a

sortCompare(   $a,   $b, ) : n/a

Description

Compares two values of sort keys

Arguments

Name Type Description Default
$a n/a
$b n/a

Return value

Type Description
n/a n/a

Properties

$itemclass, $itemlist, $next_page_callback, $next_page_class, $next_page_url, $pointer, $service, $sortkey,

  private  $itemclass =




  private  $itemlist = array()




  private  $next_page_callback =




  private  $next_page_class =




  private  $next_page_url =




  private  $pointer = 0




  private  $service =




  private  $sortkey =




Documentation was generated by phpDocumentor 2.0.0b6 .

Namespaces

Packages