Skip to content

Commit

Permalink
Major refactor on the cowsayphp library
Browse files Browse the repository at this point in the history
  • Loading branch information
alrik11es committed Aug 24, 2016
1 parent 06fafec commit 34abd70
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 161 deletions.
4 changes: 2 additions & 2 deletions bin/cowsayphp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php" : ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit" : "4.*",
"phpunit/phpunit" : "5.*",
"scrutinizer/ocular": "~1.1"
},
"autoload": {
Expand Down
78 changes: 78 additions & 0 deletions src/AbstractAnimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Cowsayphp;

abstract class AbstractAnimal implements AnimalInterface
{
protected $character;

/**
* Make the animal speak.
* @param $text string A string you want the animal says
* @return string The animal speaks...
*/
public function say($text)
{
$message = $this->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;
}
}
9 changes: 9 additions & 0 deletions src/AnimalInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Cowsayphp;


interface AnimalInterface
{
public function say($text);
}
163 changes: 6 additions & 157 deletions src/Cow.php
Original file line number Diff line number Diff line change
@@ -1,166 +1,15 @@
<?php

namespace Cowsayphp;

/**
* Class Cow FOR RETRO-COMPATIBILITY PURPOSES ONLY
* @package Cowsayphp
*/
class Cow
{
/**
* The fantastic cow string
*/
const COW = <<<DOC
{{bubble}}
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
DOC;

const TUX = <<<DOC
{{bubble}}
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
DOC;

const DRAGON = <<<DOC
{{bubble}}
\ / \ //\
\ |\___/| / \// \\\\
/0 0 \__ / // | \ \
/ / \/_/ // | \ \
@_^_@'/ \/_ // | \ \
//_^_/ \/_ // | \ \
( //) | \/// | \ \
( / /) _|_ / ) // | \ _\
( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.
(( / / )) ,-{ _ `-.|.-~-. .~ `.
(( // / )) '/\ / ~-. _ .-~ .-~^-. \
(( /// )) `. { } / \ \
(( / )) .----~-.\ \-' .~ \ `. \^-.
///.----..> \ _ -~ `. ^-` ^-_
///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~
/.-~
DOC;

const WHALE = <<<DOC
{{bubble}}
\
\ ## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
DOC;


private $default_character = self::COW;

/**
* Use this method to set up a character
* @param $character
*/
public function setCharacter($character)
{
$this->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);
}
}
19 changes: 19 additions & 0 deletions src/Farm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cowsayphp;

class Farm
{
/**
* @param $animal
* @return AnimalInterface
*/
public static function create($animal)
{
$result = null;
if(class_exists($animal)) {
$result = new $animal;
}
return $result;
}
}
22 changes: 22 additions & 0 deletions src/Farm/Cow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Cowsayphp\Farm;

use Cowsayphp\AbstractAnimal;

class Cow extends AbstractAnimal
{
/**
* The fantastic cow string
*/
protected $character = <<<DOC
{{bubble}}
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
DOC;

}
31 changes: 31 additions & 0 deletions src/Farm/Dragon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Cowsayphp\Farm;

use Cowsayphp\AbstractAnimal;

class Dragon extends AbstractAnimal
{

protected $character = <<<DOC
{{bubble}}
\ / \ //\
\ |\___/| / \// \\\\
/0 0 \__ / // | \ \
/ / \/_/ // | \ \
@_^_@'/ \/_ // | \ \
//_^_/ \/_ // | \ \
( //) | \/// | \ \
( / /) _|_ / ) // | \ _\
( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.
(( / / )) ,-{ _ `-.|.-~-. .~ `.
(( // / )) '/\ / ~-. _ .-~ .-~^-. \
(( /// )) `. { } / \ \
(( / )) .----~-.\ \-' .~ \ `. \^-.
///.----..> \ _ -~ `. ^-` ^-_
///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~
/.-~
DOC;

}
22 changes: 22 additions & 0 deletions src/Farm/Tux.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Cowsayphp\Farm;

use Cowsayphp\AbstractAnimal;

class Tux extends AbstractAnimal
{
protected $character = <<<DOC
{{bubble}}
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
DOC;
}
Loading

0 comments on commit 34abd70

Please sign in to comment.