mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 06:57:25 +02:00
cs Iterator & Mediator
This commit is contained in:
@@ -22,9 +22,9 @@ namespace DesignPatterns;
|
|||||||
class File
|
class File
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Rowset
|
* @var RowSet
|
||||||
*/
|
*/
|
||||||
protected $rowset;
|
protected $rowSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@@ -36,26 +36,35 @@ class File
|
|||||||
*/
|
*/
|
||||||
public function __construct($pathName)
|
public function __construct($pathName)
|
||||||
{
|
{
|
||||||
$this->rowset = new Rowset($this);
|
$this->rowSet = new Rowset($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function process()
|
public function process()
|
||||||
{
|
{
|
||||||
// this is the place to show how using an iterator, with foreach
|
// this is the place to show how using an iterator, with foreach
|
||||||
// See the CardGame.php file
|
// See the CardGame.php file
|
||||||
$this->rowset->process();
|
$this->rowSet->process();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Rowset implements \Iterator
|
class Rowset implements \Iterator
|
||||||
{
|
{
|
||||||
protected $_currentRow;
|
/**
|
||||||
|
* @var
|
||||||
|
*/
|
||||||
|
protected $currentRow;
|
||||||
|
|
||||||
protected $_file;
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $file;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $file
|
||||||
|
*/
|
||||||
public function __construct($file)
|
public function __construct($file)
|
||||||
{
|
{
|
||||||
$this->_file = $file;
|
$this->file = $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,28 +90,28 @@ class Rowset implements \Iterator
|
|||||||
|
|
||||||
public function rewind()
|
public function rewind()
|
||||||
{
|
{
|
||||||
// seek to first line from $this->_file
|
// seek to first line from $this->file
|
||||||
}
|
}
|
||||||
|
|
||||||
public function next()
|
public function next()
|
||||||
{
|
{
|
||||||
// read the next line from $this->_file
|
// read the next line from $this->file
|
||||||
if (!$eof) {
|
if (!$eof) {
|
||||||
$data = ''; // get the line
|
$data = ''; // get the line
|
||||||
$this->_currentRow = new Row($data);
|
$this->currentRow = new Row($data);
|
||||||
} else {
|
} else {
|
||||||
$this->_currentRow = null;
|
$this->currentRow = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function current()
|
public function current()
|
||||||
{
|
{
|
||||||
return $this->_currentRow;
|
return $this->currentRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function valid()
|
public function valid()
|
||||||
{
|
{
|
||||||
return null !== $this->_currentRow;
|
return null !== $this->currentRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function key()
|
public function key()
|
||||||
|
@@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
|
||||||
* DesignPatternPHP
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace DesignPatterns\Mediator;
|
namespace DesignPatterns\Mediator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,10 +8,20 @@ namespace DesignPatterns\Mediator;
|
|||||||
*/
|
*/
|
||||||
interface MediatorInterface
|
interface MediatorInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* sends the response
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
*/
|
||||||
function sendResponse($content);
|
function sendResponse($content);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* makes a request
|
||||||
|
*/
|
||||||
function makeRequest();
|
function makeRequest();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* queries the DB
|
||||||
|
*/
|
||||||
function queryDb();
|
function queryDb();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user