Created
February 28, 2014 07:57
-
-
Save stojg/9267058 to your computer and use it in GitHub Desktop.
happy.php
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
<?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