In most cases the implementation of external iterators in PHP takes place when it becomes necessary to build wrappers around database result sets and plain arrays. However, they can be of great help when it comes to sticking with the DRY (Don’t Repeat Yourself) and the Single Responsibility principles as well. By using an outer element that encapsulates an intuitive API and all the functionality required for iterating over common data structures like the ones mentioned above, it is possible to build different types of traversable classes without having to pollute them with unnecessary responsibilities.
To demonstrate how to translate these seemingly-complex theoretical concepts into concrete and functional code, in earlier chapters of this series I developed a few examples which showed how to use external iterators; first for traversing the protected fields of an entity modeling class and secondly for constructing a file helper, which had the ability for lazy-loading data from a sample text file. This last example is especially interesting, as its driving logic can be adapted to work with other types of persistence mechanisms, such a RDBMS, a web service or a CDN.
In addition, it’s valid to point out that the aforementioned file helper made use of a custom array collection to fetch (on request) data from a specified target file. Considering that the data was converted to an array prior to being processed, it’d be really easy to refactor the helper and make it work with the native ArrayIterator SPL class. That would mean writing less code, while maintaining the same functionality. A hard-to-beat deal, indeed.
In line with this idea, in this final installment of the series I’m going to show you how simple is to refactor the pertinent file helper, so that it can employ the built-in ArrayIterator class for lazy-loading file data, instead of appealing to a custom array collection.
Recap Time: Lazy-loading File Data Using a Custom External Iterator
In case that you still haven’t had the chance to read the preceding tutorials in this series, I will recap the source code corresponding to this example, so that you can dissect it and grasp its driving logic.
With that being said, here’s an implementation of the pertaining external iterator. Take a look:
/** * Constructor */ public function __construct(array $data = array()) { if (!empty($data)) { $this->_data = $data; } $this->rewind(); }
/** * Count the number of elements in the collection (implementation required by Countable interface) */ public function count() { return count($this->_data); }
/** * Reset the collection (implementation required by Iterator interface) */ public function rewind() { reset($this->_data); }
/** * Get the key of the current element in the collection (implementation required by Iterator interface) */ public function key() { return key($this->_data); }
/** * Get the current element in the collection (implementation required by Iterator interface) */ public function current() { return current($this->_data); }
/** * Check if there're more elements in the collection (implementation required by Iterator Interface) */
public function valid() { return ($this->current() !== false); }
/** * Move to the next element in the collection (implementation required by Iterator interface) */ public function next() { next($this->_data); }
/** * Add an element to the collection (implementation required by ArrayAccess interface) */ public function offsetSet($key, $value) { if (!isset($key)) { $this->_data[] = $value; } else { $this->_data[$key] = $value; } return true; }
/** * Remove an element from the collection (implementation required by ArrayAccess interface) */ public function offsetUnset($key) { if (isset($this->_data[$key])) { unset($this->_data[$key]); return true; } return false; }
/** * Get the specified element in the collection (implementation required by ArrayAccess interface) */ public function offsetGet($key) { return isset($this->_data[$key]) ? $this->_data[$key] : null; }
/** * Check if the specified element exists in the collection (implementation required by ArrayAccess interface) */ public function offsetExists($key) { return isset($this->_data[$key]); }
/** * Return the elements in the collection */ public function toArray() { return $this->_data; } }
As you can see, the “ArrayCollection” class is nothing but a generic iterator, which enables it to traverse, count and access the elements stored in its protected $_data field by using an array-like notation.