Skip to content

Commit

Permalink
cs Iterator and Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Liebler committed Sep 21, 2013
1 parent 4b66417 commit 9484868
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 160 deletions.
87 changes: 0 additions & 87 deletions Iterator/CardGame.php

This file was deleted.

51 changes: 51 additions & 0 deletions Iterator/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace DesignPatterns\Iterator;

/**
* iterator pattern
*
* Purpose:
* to make an object iterable
*
* Examples:
* - to process a file line by line by just running over all lines (which have an object representation) for a file
* (which of course is an object, too)
*
* Note:
* Standard PHP Library (SPL) defines an interface Iterator which is best suited for this!
* Often you would want to implement the Countable interface too, to allow count($object) on your iterable object
*
* THIS EXAMPLE ALSO APPLIES THE COMPOSITE PATTERN
*
*/
class File
{
/**
* @var RowSet
*/
protected $rowSet;

/**
* @var string
*/
protected $pathName;

/**
* @param string $pathName
*/
public function __construct($pathName)
{
$this->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();
}
}
27 changes: 27 additions & 0 deletions Iterator/Row.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace DesignPatterns\Iterator;

/**
* Class Row
*/
class Row
{
protected $data;

/**
* {@inheritdoc}
*/
public function __construct($data)
{
$this->data = $data;
}

/**
* {@inheritdoc}
*/
public function process()
{
// do some fancy things here ...
}
}
89 changes: 26 additions & 63 deletions Iterator/Iterator.php → Iterator/RowSet.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
<?php

namespace DesignPatterns;
namespace DesignPatterns\Iterator;

/**
* iterator pattern
*
* Purpose:
* to make an object iterable
*
* Examples:
* - to process a file line by line by just running over all lines (which have an object representation) for a file
* (which of course is an object, too)
*
* Note:
* Standard PHP Library (SPL) defines an interface Iterator which is best suited for this!
* Often you would want to implement the Countable interface too, to allow count($object) on your iterable object
*
* THIS EXAMPLE ALSO APPLIES THE COMPOSITE PATTERN
*
* Class RowSet
*/
class File
{
/**
* @var RowSet
*/
protected $rowSet;

/**
* @var string
*/
protected $pathName;

/**
* @param string $pathName
*/
public function __construct($pathName)
{
$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();
}
}

class Rowset implements \Iterator
class RowSet implements \Iterator
{
/**
* @var
Expand All @@ -59,6 +17,11 @@ class Rowset implements \Iterator
*/
protected $file;

/**
* @var int
*/
protected $lineNumber;

/**
* @param string $file
*/
Expand All @@ -79,20 +42,26 @@ public function process()
* 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
Expand All @@ -104,34 +73,28 @@ public function next()
}
}

/**
* {@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;
}
}
9 changes: 5 additions & 4 deletions Multiton/Multiton.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Multiton
*
* @var array
*/
private static $_instances = array();
private static $instances = array();

/**
* should not be called from outside: private!
Expand All @@ -53,15 +53,16 @@ private function __construct()
* 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];
}

/**
Expand Down
6 changes: 0 additions & 6 deletions Registry/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,3 @@ public static function get($key)

// 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');
9 changes: 9 additions & 0 deletions Registry/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use DesignPatterns\Registry;

// while bootstrapping the application
Registry::set(Registry::LOGGER, new \StdClass());

// throughout the application
Registry::get(Registry::LOGGER)->log('foo');

0 comments on commit 9484868

Please sign in to comment.