Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added Iterator example, slightly modified to better show the workings
  • Loading branch information
Bert Van de Casteele committed Feb 26, 2017
commit a52e9ca7a4b3beb8d48bb4e4b7a3b5d8fd85da8b
32 changes: 32 additions & 0 deletions examples-per-language/php/behavioral/Iterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use designPatternsForHumans\behavioral\Iterator\RadioStation;
use designPatternsForHumans\behavioral\Iterator\StationList;

require_once __DIR__ . '/autoload.php';

$stationList = new StationList();

$stationList->addStation(new RadioStation(89));
$stationList->addStation(new RadioStation(101));
$stationList->addStation(new RadioStation(102));
$stationList->addStation(new RadioStation(103.2));

/** @var RadioStation $station */
echo 'These are the known Radio Stations' . PHP_EOL;
foreach ($stationList as $station) {
echo $station->getFrequency(). PHP_EOL;
}

$stationList->removeStation(new RadioStation(89));

// To prove the station was indeed removed, we iterate through the list again.
// Small caveat : Iterators need to be rewinded after beeing Iterated!
$stationList->rewind();

/** @var RadioStation $station */
echo 'These are the known Radio Stations' . PHP_EOL;
foreach ($stationList as $station) {
echo $station->getFrequency(). PHP_EOL;
}

19 changes: 19 additions & 0 deletions examples-per-language/php/behavioral/src/Iterator/RadioStation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace designPatternsForHumans\behavioral\Iterator;


class RadioStation
{
protected $frequency;

public function __construct($frequency)
{
$this->frequency = $frequency;
}

public function getFrequency()
{
return $this->frequency;
}
}
61 changes: 61 additions & 0 deletions examples-per-language/php/behavioral/src/Iterator/StationList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace designPatternsForHumans\behavioral\Iterator;

use Countable;
use Iterator;

class StationList implements Countable, Iterator
{

protected $stations = [];
protected $counter;

public function addStation(RadioStation $station)
{
$this->stations[] = $station;
}

public function removeStation(RadioStation $toRemove)
{
$toRemoveFrequency = $toRemove->getFrequency();
$this->stations = array_filter($this->stations, function (RadioStation $station) use ($toRemoveFrequency) {
return $station->getFrequency() !== $toRemoveFrequency;
});
}

public function current()
{
return $this->stations[$this->counter];
}

public function next()
{
$this->counter++;
}

public function key()
{
return $this->counter;
}

public function valid()
{
return isset($this->stations[$this->counter]);
}

public function rewind()
{
// When using a numbered array like we do with $counter, we need to reset
// the array as well, otherwise the removed station will wreak havoc with
// our iteration, because the value ox removed station at position X will
// be NULL;
$this->stations = array_values($this->stations);
return $this->counter = 0;
}

public function count()
{
return count($this->stations);
}
}