forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dominik Liebler
committed
Sep 21, 2013
1 parent
4b66417
commit 9484868
Showing
7 changed files
with
118 additions
and
160 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |