From 34abd70cdebfc1fb9bb95ad1ae678de589cee42e Mon Sep 17 00:00:00 2001 From: alrik11es Date: Thu, 25 Aug 2016 00:00:47 +0200 Subject: [PATCH] Major refactor on the cowsayphp library --- bin/cowsayphp | 4 +- composer.json | 2 +- src/AbstractAnimal.php | 78 +++++++++++++++++++ src/AnimalInterface.php | 9 +++ src/Cow.php | 163 ++-------------------------------------- src/Farm.php | 19 +++++ src/Farm/Cow.php | 22 ++++++ src/Farm/Dragon.php | 31 ++++++++ src/Farm/Tux.php | 22 ++++++ src/Farm/Whale.php | 24 ++++++ tests/CowTest.php | 5 +- 11 files changed, 218 insertions(+), 161 deletions(-) mode change 100644 => 100755 bin/cowsayphp create mode 100644 src/AbstractAnimal.php create mode 100644 src/AnimalInterface.php create mode 100644 src/Farm.php create mode 100644 src/Farm/Cow.php create mode 100644 src/Farm/Dragon.php create mode 100644 src/Farm/Tux.php create mode 100644 src/Farm/Whale.php diff --git a/bin/cowsayphp b/bin/cowsayphp old mode 100644 new mode 100755 index 358b714..f408e48 --- a/bin/cowsayphp +++ b/bin/cowsayphp @@ -9,8 +9,8 @@ if (extension_loaded('phar') && ($uri = Phar::running())) { } if(count($argv) > 1){ - $cow = new \Cowsayphp\Cow(); - echo $cow->speak($argv[1]); + $cow = \Cowsayphp\Farm::create(\Cowsayphp\Farm\Cow::class); + echo $cow->say($argv[1]); } /** diff --git a/composer.json b/composer.json index 8dfe377..25c2d5f 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php" : ">=5.3.0" }, "require-dev": { - "phpunit/phpunit" : "4.*", + "phpunit/phpunit" : "5.*", "scrutinizer/ocular": "~1.1" }, "autoload": { diff --git a/src/AbstractAnimal.php b/src/AbstractAnimal.php new file mode 100644 index 0000000..b808113 --- /dev/null +++ b/src/AbstractAnimal.php @@ -0,0 +1,78 @@ +getSpeechBubble($text); + $animal = str_replace('{{bubble}}', $message, $this->character); + return $animal; + } + + /** + * Obtain the message as array wrapping the text + * @param $text + * @return array + */ + public function getMessageLines($text) + { + $message = $text; + $wrapLength = 40; + // wrap the message to max chars + $message = wordwrap($message, $wrapLength - 2); + // split into array of lines + return explode("\n", $message); + } + + /** + * Find the longest line and get the line length + * @param array $lines + * @return int + */ + public function getMaxLineLength(array $lines) + { + $lineLength = 0; + // find the longest line + foreach ($lines as $line) { + $currentLineLength = strlen($line); + if ($currentLineLength > $lineLength) { + $lineLength = $currentLineLength; + } + } + return $lineLength; + } + + /** + * Obtain the speech bubble. + * @param $text + * @return string + */ + public function getSpeechBubble($text) + { + $lines = $this->getMessageLines($text); + $lineLength = $this->getMaxLineLength($lines); + $text = ''; + $numberOfLines = count($lines); + $firstLine = str_pad(array_shift($lines), $lineLength); + if ($numberOfLines === 1) { + $text = "< {$firstLine} >"; + } else { + $lastLine = str_pad(array_pop($lines), $lineLength); + $text = "/ {$firstLine} \\\n"; + foreach ($lines as $line) { + $line = str_pad($line, $lineLength); + $text .= "| {$line} |\n"; + } + $text .= "\\ {$lastLine} /"; + } + return $text; + } +} \ No newline at end of file diff --git a/src/AnimalInterface.php b/src/AnimalInterface.php new file mode 100644 index 0000000..7344164 --- /dev/null +++ b/src/AnimalInterface.php @@ -0,0 +1,9 @@ + \ _ -~ `. ^-` ^-_ - ///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~ - /.-~ - -DOC; - - const WHALE = <<default_character = $character; - } - - /** - * Make the cow speak from static context. - * @param $text string A string you want the cow says - * @return string The cow speaks... - */ public static function say($text) { - $cow = new self(); - return $cow->speak($text); - } - - /** - * Make the cow speak. - * @param $text string A string you want the cow says - * @return string The cow speaks... - */ - public function speak($text) - { - $message = $this->getSpeechBubble($text); - $cow = str_replace('{{bubble}}', $message, $this->default_character); - return $cow; - } - - /** - * Obtain the message as array wrapping the text - * @param $text - * @return array - */ - public function getMessageLines($text) - { - $message = $text; - $wrapLength = 40; - // wrap the message to max chars - $message = wordwrap($message, $wrapLength - 2); - // split into array of lines - return explode("\n", $message); - } - - /** - * Find the longest line and get the line length - * @param array $lines - * @return int - */ - public function getMaxLineLength(array $lines) - { - $lineLength = 0; - // find the longest line - foreach ($lines as $line) { - $currentLineLength = strlen($line); - if ($currentLineLength > $lineLength) { - $lineLength = $currentLineLength; - } - } - return $lineLength; - } - - /** - * Obtain the speech bubble. - * @param $text - * @return string - */ - public function getSpeechBubble($text) - { - $lines = $this->getMessageLines($text); - $lineLength = $this->getMaxLineLength($lines); - $text = ''; - $numberOfLines = count($lines); - $firstLine = str_pad(array_shift($lines), $lineLength); - if ($numberOfLines === 1) { - $text = "< {$firstLine} >"; - } else { - $lastLine = str_pad(array_pop($lines), $lineLength); - $text = "/ {$firstLine} \\\n"; - foreach ($lines as $line) { - $line = str_pad($line, $lineLength); - $text .= "| {$line} |\n"; - } - $text .= "\\ {$lastLine} /"; - } - return $text; + $cow = Farm::create(\Cowsayphp\Farm\Cow::class); + return $cow->say($text); } } \ No newline at end of file diff --git a/src/Farm.php b/src/Farm.php new file mode 100644 index 0000000..9a89cc5 --- /dev/null +++ b/src/Farm.php @@ -0,0 +1,19 @@ + \ _ -~ `. ^-` ^-_ + ///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~ + /.-~ + +DOC; + +} \ No newline at end of file diff --git a/src/Farm/Tux.php b/src/Farm/Tux.php new file mode 100644 index 0000000..652bc6a --- /dev/null +++ b/src/Farm/Tux.php @@ -0,0 +1,22 @@ +getSpeechBubble($text); $this->assertEquals($message, $bubble); }