mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 23:16:33 +02:00
cs Iterator and Proxy & Status
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Iterator;
|
||||
|
||||
/**
|
||||
* Iterator provides a standard way to iterate over a collection without knowing
|
||||
* how it is implemented. All you need to know is : you can traverse it
|
||||
* how it is implemented. All you need to know is that you can traverse it
|
||||
* with current, valid, next, rewind and key.
|
||||
*
|
||||
* That's the key feature of this pattern :
|
||||
@@ -27,12 +23,20 @@ namespace DesignPatterns\Iterator;
|
||||
*/
|
||||
class CardGame implements \Iterator
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $color = array('D', 'S', 'C', 'H');
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $number = array(7, 8, 9, 10, 'J', 'Q', 'K', 'A');
|
||||
|
||||
/**
|
||||
* Return the current value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
@@ -41,6 +45,8 @@ class CardGame implements \Iterator
|
||||
|
||||
/**
|
||||
* Return the current key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
@@ -71,10 +77,11 @@ class CardGame implements \Iterator
|
||||
/**
|
||||
* Is the current position a valid item (true)
|
||||
* or do we reach the end (false) ?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return current($this->number) || current($this->color);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -21,20 +21,29 @@ namespace DesignPatterns;
|
||||
*/
|
||||
class File
|
||||
{
|
||||
protected $_rowset;
|
||||
/**
|
||||
* @var Rowset
|
||||
*/
|
||||
protected $rowset;
|
||||
|
||||
protected $_pathName;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $pathName;
|
||||
|
||||
/**
|
||||
* @param string $pathName
|
||||
*/
|
||||
public function __construct($pathName)
|
||||
{
|
||||
$this->_rowset = new Rowset($this);
|
||||
$this->rowset = new Rowset($this);
|
||||
}
|
||||
|
||||
public function process()
|
||||
{
|
||||
// this is the place to show how using an iterator, with foreach
|
||||
// See the CardGame.php file
|
||||
$this->_rowset->process();
|
||||
$this->rowset->process();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user