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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: