ObjectComparator Class Reference

Detailed Description

ObjectComparator is used to compare persistent objects by given criterias.

The following example shows the usage:

$objectList = [...]; // array of PersistentObject instances
// simple sort by creator attribute
$comparator = new ObjectComparator('creator');
usort($objectList, [$comparator, 'compare']);
// sort by creator attribute with direction
$comparator = new ObjectComparator('creator DESC');
usort($objectList, [$comparator, 'compare']);
// sort by multiple attributes with direction
$comparator = new ObjectComparator(['creator DESC', 'created ASC']);
usort($objectList, [$comparator, 'compare']);
// more complex example with different attributes
$sortCriteria = [
$comparator = new ObjectComparator($sortCriteria);
usort($objectList, [$comparator, 'compare']);
ingo herwig ingo@.nosp@m.wemo.nosp@m.ve.co.nosp@m.m

Definition at line 47 of file ObjectComparator.php.

+ Inheritance diagram for ObjectComparator:

Public Member Functions

 __construct ($sortCriteria)
 compare (PersistentObject $a, PersistentObject $b)

Public Attributes

const SORTTYPE_ASC = -1
const SORTTYPE_DESC = -2
const ATTRIB_OID = -3
const ATTRIB_TYPE = -4

Constructor & Destructor Documentation

◆ __construct()

__construct (   $sortCriteria)


$sortCriteriaAn assoziative array of criteria - SORTTYPE constant pairs OR a single criteria string. possible criteria: ObjectComparator::OID, ObjectComparator::TYPE or any value/property name with optionally ASC or DESC appended (e.g. [ObjectComparator::OID => ObjectComparator::SORTTYPE_ASC, 'name' => ObjectComparator::SORTTYPE_DESC] OR 'name')
If criteria is only a string we will sort by this criteria with ObjectComparator::SORTTYPE_ASC

Definition at line 63 of file ObjectComparator.php.

Member Function Documentation

◆ compare()

compare ( PersistentObject  $a,
PersistentObject  $b 

Compare function for sorting PersitentObject instances by the list of criterias.

$aFirst PersitentObject instance
$bFirst PersitentObject instance
-1, 0 or 1 whether a is less, equal or greater than b in respect of the criteria

Definition at line 85 of file ObjectComparator.php.

Member Data Documentation


const SORTTYPE_ASC = -1

Definition at line 49 of file ObjectComparator.php.


const SORTTYPE_DESC = -2

Definition at line 50 of file ObjectComparator.php.


const ATTRIB_OID = -3

Definition at line 51 of file ObjectComparator.php.


const ATTRIB_TYPE = -4

Definition at line 52 of file ObjectComparator.php.