Skip to content
Browse files

Initial state

  • Loading branch information...
1 parent 485da44 commit baca3db73de9b3dbd09dac4aa481e19a3138017e @giorgiosironi committed Nov 24, 2011
Showing with 70 additions and 0 deletions.
  1. +70 −0 ReplaceParameterWithMethod.php
View
70 ReplaceParameterWithMethod.php
@@ -0,0 +1,70 @@
+<?php
+class ReplaceParameterWithMethod extends PHPUnit_Framework_TestCase
+{
+ public function test()
+ {
+ $thread = new Thread();
+ $thread->add(new Post('Hello...'));
+ $thread->add(new Post('Hi!'));
+ $this->assertEquals("Hello...\n\n> In reply to: Hello...\nHi!\n\n", $thread->__toString());
+ }
+}
+
+class Thread
+{
+ private $posts = array();
+ private $lastPost = null;
+
+ /**
+ * Sets up a linked list of Post objects in addition to the array.
+ * We assume this kind of traversal is already in place for other reasons
+ * or collaborations between Post objects.
+ */
+ public function add(Post $post)
+ {
+ $this->posts[] = $post;
+ $post->setOrigin($this->lastPost);
+ $this->lastPost = $post;
+ }
+
+ /**
+ * This method contains mechanics that duplicate the linked list.
+ */
+ public function __toString()
+ {
+ $previousPost = null;
+ $text = '';
+ foreach ($this->posts as $post) {
+ $text .= $post->toString($previousPost);
+ $text .= "\n";
+ $previousPost = $post;
+ }
+ return $text;
+ }
+}
+
+class Post
+{
+ private $text;
+ private $origin;
+
+ public function __construct($text)
+ {
+ $this->text = $text;
+ }
+
+ public function setOrigin(Post $post = null)
+ {
+ $this->origin = $post;
+ }
+
+ public function toString(Post $previousPost = null) {
+ if ($previousPost) {
+ $text = '> In reply to: ' . $previousPost->text . "\n";
+ } else {
+ $text = '';
+ }
+ $text .= $this->text . "\n";
+ return $text;
+ }
+}

0 comments on commit baca3db

Please sign in to comment.
Something went wrong with that request. Please try again.