3.4. Sets

As listed above, a set can only have one copy of a value. Let's take the same example from above.

<?php
$fan4 = Xyster_Collection::using(array('mr. fantastic', 'invisible woman', 'human torch', 'thing'));

$heroes = new Xyster_Collection_Set();
$heroes->add( 'superman' ); // set now contains 'superman'
$heroes->add( 'batman' ); // set now contains 'superman', 'batman'
$heroes->addAll( $fan4 ); 
// set now contains 'superman', 'batman', 'mr. fantastic', 'invisible woman', 'human torch', 'thing'
$heroes->addAll( $fan4 );   // does nothing – the set already has the fantastic 4

3.4.1. Sortable Sets and Comparators

The sortable set allows for any class implementing Xyster_Collection_Comparator_Interface to reorder the collection contents. The class must implement one method, compare, which accepts two items. The method should return -1 if the first is less than the second, 0 if they are equal, or 1 if the second is greater than the first (see PHP's strcmp for an example). An instance of this class can be passed into the sort method of a sortable set.

<?php
class CaseInsensitiveComparator implements Xyster_Collection_Comparator_Interface
{
        public function compare( $a, $b )
        { 
                return strcmp($a, $b);
        }
}
$people = array('Frank Castle','Clark Kent','Felicia Hardy','Selina Kyle');
$set = new Xyster_Collection_Set_Sortable();
foreach( $people as $person ) {
        $set->add($person);
}
$set->sort(new CaseInsensitiveComparator());
// $set's items are now ordered 'Clark Kent, 'Felicia Hardy', 'Frank Castle', 'Selina Kyle'