PHP
  Home arrow PHP arrow The Iterator Pattern, concluded
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

The Iterator Pattern, concluded
By: php|architect
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2005-12-01


    Table of Contents:
  • The Iterator Pattern, concluded
  • Sorting Iterator
  • SPL Iterator
  • Issues

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The Iterator Pattern, concluded
    ( Page 1 of 4 )

    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).

     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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT