Home arrow PHP arrow The Iterator Pattern, concluded

The Iterator Pattern, concluded

This article, the second of two parts, explains how to use the Iterator pattern to manipulate any collection of objects. It is excerpted from chapter eight of the book PHP|architect's Guide to PHP Design Patterns, written by Jason E. Sweat (PHP|architect, 2005; ISBN: 0973589825).

TABLE OF CONTENTS:
  1. The Iterator Pattern, concluded
  2. Sorting Iterator
  3. SPL Iterator
  4. Issues
By: php|architect
Rating: starstarstarstarstar / 5
December 01, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

 Filtering Iterator

With Iterators,you can do more than just present each item of the collection. You can also select what items are presented. Let's modify the Library::getIterator() to allow two additional iterator types.

  class Library {
   
// ...
   
function getIterator($type=false) {
     
switch (strtolower($type)) {
     
case 'media':
       
$iterator_class = 'LibraryIterator';
       
break;
     
case 'available':
       
$iterator_class = 'LibraryAvailableIterator';
       
break;
     
case 'released':
       
$iterator_class = 'LibraryReleasedIterator';
       
break;
     
default:
       
$iterator_class = 'LibraryGofIterator';
     
}
     
return new $iterator_class($this->collection);
   
}
 
}

The class LibraryAvailableIterator should only iterate over items that have a status of "library" (recall that the checkOut() method changes the status to "borrowed").

  class IteratorTestCase extends UnitTestCase {
   
// ...
   
function TestAvailableIteratorUsage() {
     
$this->lib->add($dvd = new Media('test', 1999));
     
$this->lib->add(new Media('name4', 1999));
     
$this->assertIsA(
       
$it = $this->lib->getIterator('available')
       
,'LibraryAvailableIterator');
     
$output = '';
    
  while ($item = $it->next()) {
       
$output .= $item->name;
     
}
     
$this->assertEqual('name1name2name3testname4', $output);
      $dvd->checkOut(‘Jason’);
     
$it = $this->lib->getIterator('available');
     
$output = '';
     
while ($item = $it->next()) {
       
$output .= $item->name;
     
}
     
$this->assertEqual('name1name2name3name4', $output);
   
}
 
}

This test creates a new Media instance and stores it in the variable $dvd. The first highlighted assertEqual()assertion verifies that the new item is present when iterating with LibraryAvailableIterator. Next, the test uses the checkOut() method and verifies that the new item is missing from the display.

The code to implement filtering is very similar to LibraryIterator::next(), except filtering is done prior to returning the item. If the current item does not match the filter criteria, the code returns $this->next() instead.

  class LibraryAvailableIterator {
   
protected $collection = array();
   
protected $first=true;
   
function __construct($collection) {
     
$this->collection = $collection;
    }
    
function next() {
      
if ($this->first) {
       
$this->first = false;
       
$ret = current($this->collection);
      
} else {
        
$ret = next($this->collection);
     
}
      
if ($ret && 'library' != $ret->status) {
        
return $this->next();
      
}
      
return $ret;
   
}
 
}



 
 
>>> More PHP Articles          >>> More By php|architect
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: