diff --git a/Iterator/CardGame.php b/Iterator/CardGame.php deleted file mode 100644 index 4d5997a..0000000 --- a/Iterator/CardGame.php +++ /dev/null @@ -1,87 +0,0 @@ -number) . ' of ' . current($this->color); - } - - /** - * Return the current key - * - * @return string - */ - public function key() - { - return current($this->color) . current($this->number); - } - - /** - * Go to the next item in the collection - */ - public function next() - { - if (false === next($this->number)) { - if (false !== next($this->color)) { - reset($this->number); - } - } - } - - /** - * Go to the first item in the collection - */ - public function rewind() - { - reset($this->color); - reset($this->number); - } - - /** - * 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); - } -} diff --git a/Iterator/File.php b/Iterator/File.php new file mode 100644 index 0000000..0e1aa3d --- /dev/null +++ b/Iterator/File.php @@ -0,0 +1,51 @@ +rowSet = new Rowset($this); + } + + /** + * processes the rowSet + */ + public function process() + { + // this is the place to show how using an iterator, with foreach + // See the CardGame.php file + $this->rowSet->process(); + } +} diff --git a/Iterator/Row.php b/Iterator/Row.php new file mode 100644 index 0000000..df4002d --- /dev/null +++ b/Iterator/Row.php @@ -0,0 +1,27 @@ +data = $data; + } + + /** + * {@inheritdoc} + */ + public function process() + { + // do some fancy things here ... + } +} diff --git a/Iterator/Iterator.php b/Iterator/RowSet.php similarity index 56% rename from Iterator/Iterator.php rename to Iterator/RowSet.php index d05fcc6..f1ef5d7 100644 --- a/Iterator/Iterator.php +++ b/Iterator/RowSet.php @@ -1,53 +1,11 @@ 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(); - } -} - -class Rowset implements \Iterator +class RowSet implements \Iterator { /** * @var @@ -59,6 +17,11 @@ class Rowset implements \Iterator */ protected $file; + /** + * @var int + */ + protected $lineNumber; + /** * @param string $file */ @@ -79,20 +42,26 @@ class Rowset implements \Iterator * THE key feature of the Iterator Pattern is to provide a *public contract* * to iterate on a collection without knowing how items are handled inside * the collection. It is not just an easy way to use "foreach" - * + * * One cannot see the point of iterator pattern if you iterate on $this. - * This example is unclear and mixed with some Composite pattern ideas. + * This example is unclear and mixed with some Composite pattern ideas. */ foreach ($this as $line => $row) { $row->process(); } } + /** + * {@inheritdoc} + */ public function rewind() { // seek to first line from $this->file } + /** + * {@inheritdoc} + */ public function next() { // read the next line from $this->file @@ -104,34 +73,28 @@ class Rowset implements \Iterator } } + /** + * {@inheritdoc} + */ public function current() { return $this->currentRow; } + /** + * {@inheritdoc} + */ public function valid() { return null !== $this->currentRow; } + /** + * {@inheritdoc} + */ public function key() { // you would want to increment this in next() or whatsoever - return $this->_lineNumber; - } -} - -class Row -{ - protected $_data; - - public function __construct($data) - { - $this->_data = $data; - } - - public function process() - { - // do some fancy things here ... + return $this->lineNumber; } } diff --git a/Multiton/Multiton.php b/Multiton/Multiton.php index 74f9dcd..1dddebb 100644 --- a/Multiton/Multiton.php +++ b/Multiton/Multiton.php @@ -37,7 +37,7 @@ class Multiton * * @var array */ - private static $_instances = array(); + private static $instances = array(); /** * should not be called from outside: private! @@ -53,15 +53,16 @@ class Multiton * uses lazy initialization * * @param string $instanceName + * * @return Multiton */ public static function getInstance($instanceName) { - if ( ! array_key_exists($instanceName, self::$_instances)) { - self::$_instances[$instanceName] = new self(); + if (!array_key_exists($instanceName, self::$instances)) { + self::$instances[$instanceName] = new self(); } - return self::$_instances[$instanceName]; + return self::$instances[$instanceName]; } /** diff --git a/Registry/Registry.php b/Registry/Registry.php index 65509c0..020398a 100644 --- a/Registry/Registry.php +++ b/Registry/Registry.php @@ -52,9 +52,3 @@ abstract class Registry // typically there would be methods to check if a key has already been registered and so on ... } - -// while bootstrapping the application -Registry::set(Registry::LOGGER, new \StdClass()); - -// throughout the application -Registry::get(Registry::LOGGER)->log('foo'); diff --git a/Registry/index.php b/Registry/index.php new file mode 100644 index 0000000..0a20047 --- /dev/null +++ b/Registry/index.php @@ -0,0 +1,9 @@ +log('foo');