-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor on the cowsayphp library
- Loading branch information
Showing
11 changed files
with
218 additions
and
161 deletions.
There are no files selected for viewing
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
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
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
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; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Cowsayphp; | ||
|
||
|
||
interface AnimalInterface | ||
{ | ||
public function say($text); | ||
} |
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
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); | ||
} | ||
} |
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
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; | ||
} | ||
} |
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
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; | ||
|
||
} |
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
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; | ||
|
||
} |
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
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; | ||
} |
Oops, something went wrong.