Provides functionality for Object Collections.

Any objects used with this class must support Object::getID(). Extension of this class allows collections of objects to be collated, used and manipulated in a convenient and consistent manner. The child class is responsible for loading the objects and will often provide function(s) for loading and deleting objects. An example function for loading all records as objects into the collection:

public function loadAll() {
parent::clear(); // clears any objects in collection
$rs = DataManager::loadObjects(); // load id's from the database to a var
foreach ($rs as $row) {
    parent::add(ObjectClass::loadByID($row[0])); // load objects into this class. This may vary with the actual class method being called.
}
}


package dstruct_common

 Methods

Add an object to the collection.

add(object $obj) : boolean

If the object exists, a DStructGeneralException will be thrown

Parameters

$obj

object

Exceptions

\DStructGeneralException

Returns

boolean

Number of objects currently in collection

count() : integer

Returns

integer

(non-PHPdoc)

current() 

Test whether an object exists in the collection.

exists(mixed $obj) : boolean

Parameters

$obj

mixed

Object, or it's ID

Returns

boolean

Array of objects.

getArray() : array

Returns

array

Returns an object from the collection based on its ID.

getByID(integer $id) : object | false

Parameters

$id

integer

Returns

objectfalse

Returns the first object in the collection.

getFirst() : object | false

Returns

objectfalse

(non-PHPdoc)

key() 

(non-PHPdoc)

next() 

Get previous object in collection.

prev() : mixed

Returns

mixedobject or false.

Remove an object from the collection.

remove(mixed $obj) : boolean

If the object is not in the collection, a DStructGeneralException will be thrown

Parameters

$obj

mixed

Object, or its ID

Exceptions

\DStructGeneralException

Returns

boolean

(non-PHPdoc)

rewind() 

Sorts objects by attribute or return from a method.

sortObjects(string $element, integer $sort_direction, boolean $caseinsensitive, array $params, boolean $astime) 

WARNING: This method may be slow! It is advisable to use another method if one is available, for instance, using ORDER BY in an SQL statement to create and add the objects in order.
The $element parameter can specify either an attribute or a method of the objects in the collection. Methods should be named in full e.g. "method()".
Although this uses the PHP function usort(), the array keys are preserved.
The constants SORT_OBJECTS_ASC and SORT_OBJECTS_DESC are provided to set the $sort_direction parameter.
If you set $astime to true, the method will try to use PHP's strtotime() function to attempt to convert strings into time, allowing you to sort dates in the expected order. You must ensure that all data will convert to strtotime, or that data will be evalutated as false!
Example:

class Employee {
public $name;
public function __construct($name) {$this->name = $name;}
public function getName() {return $this->name;}
}

$employees = new Employees; // create collection object
$employees->add(new Employee('neil'));
$employees->add(new Employee('David'));
$employees->add(new Employee('Shane'));

// order by name attribute
$employees->sortObjects('name');
// new order: David, neil, Shane

// order by method return, and case sensitive, and passing a single parameter of true to the method
$employees->sortObjects('getName()', Employees::SORT_ORDERS_ASC, false, array(true));
// new order: David, Shane, neil
todo If $element = getID(), use different sort

Parameters

$element

string

Element of the objects to sort by

$sort_direction

integer

1 (ascending) or -1 (descending). See above.

$caseinsensitive

boolean

Case sensitivity of sort.

$params

array

Parameters to be passed on if calling a method

$astime

boolean

Try to use strtotime() to sort by date

(non-PHPdoc)

valid() 

Clear the collection.

clear() 

Callback used by usort in {@link sortObjects()} to sort by objects attributes

csort_cmp_attribute(object $a, object $b) : integer
Static

Parameters

$a

object

$b

object

Returns

integer0 or 1

Callback used by usort in {@link sortObjects()} to sort by the return from an objects method.

csort_cmp_method(object $a, object $b) : integer
Static

Parameters

$a

object

$b

object

Returns

integer0 or 1

 Properties

 

$objs : array

 Constants

 

Sort ascending.

SORT_OBJECTS_ASC : integer
 

Sort descending.

SORT_OBJECTS_DESC : integer