Skip to content

Instantly share code, notes, and snippets.

@stojg
Created February 28, 2014 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stojg/9267058 to your computer and use it in GitHub Desktop.
Save stojg/9267058 to your computer and use it in GitHub Desktop.
happy.php
<?php
interface Text {
public function __toString();
}
class HappyBirthday implements Text {
private $restOfLine = '';
public function __construct($restOfLine) {
$this->restOfLine = $restOfLine;
}
public function __toString() {
return 'Happy birthday '.$this->restOfLine;
}
}
class To implements Text {
private $who = null;
public function __construct($who) {
$this->who = $who;
}
public function __toString() {
return 'to '.$this->who;
}
}
class Dear implements Text {
private $prefix = '';
public function __construct($prefix) {
$this->prefix = $prefix;
}
public function __toString() {
return 'dear '.(string)$this->prefix;
}
}
class Who implements Text {
private $who = '';
public function __construct($who) {
$this->who = $who;
}
public function __toString() {
return (string)$this->who;
}
}
class Song implements Iterator {
private $position = 0;
private $lines = null;
public function __construct(ArrayObject $lines) {
$this->lines = $lines;
}
public function current (){
return $this->lines[$this->position].'!'.PHP_EOL;
}
public function next () {
$this->position = $this->position + 1;
}
public function key () {
return $this->position;
}
public function valid () {
return isset($this->lines[$this->position]);
}
public function rewind () {
$this->posistion = 0;
}
}
$lines = new Song(
new ArrayObject(array(
new HappyBirthday(new To(new Who('you'))),
new HappyBirthday(new To(new Who('you'))),
new HappyBirthday(new To(new Dear(new Who('SilverStripe')))),
new HappyBirthday(new To(new Who('you')))
))
);
foreach($lines as $line) {
echo $line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment