Last active
May 13, 2019 19:30
-
-
Save trafficinc/c44a761cf58d9b5f2d54b6fc15fa9759 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/* | |
Francis, a simple message responder that responds as follows: | |
Francis answers 'Sure.' if you ask him a question | |
He answers 'Chill!' if you yell at him using all capital letters | |
He says 'See if I care!' if you address him without actually saying anything | |
He answers 'Whatevs.' to anything else | |
*/ | |
class Validator { | |
public $success = true; | |
public $defaultReason = 'Whatevs.'; | |
public $errors = []; | |
private $reasons = []; | |
private $data; | |
private $rules; | |
public function setup($data, $rules) | |
{ | |
$this->data = $data; | |
$this->rules = $rules; | |
$this->reasons = [ | |
'seeif' => 'See if I care!', | |
'sure' => 'Sure.', | |
'chill' => 'Chill!', | |
]; | |
return $this->fire(); | |
} | |
public function fire() | |
{ | |
foreach ($this->rules as $attribute => $rule) { | |
foreach (explode('|', $rule) as $item) { | |
$detail = explode(':', $item); | |
if (count($detail) > 1) { | |
$reason = call_user_func_array([$this, $detail[0]], [$this->data[$attribute], $detail[1]]); | |
} else { | |
$reason = $this->$item($this->data[$attribute]); | |
} | |
if ($reason !== true) { | |
$this->errors[] = str_replace(':attribute', $attribute, $reason); | |
} | |
} | |
} | |
if (count($this->errors) > 0) { | |
return $this->errors; | |
} | |
} | |
protected function stringIsNumber($string) | |
{ | |
$newstring = preg_replace('/[^A-Za-z0-9\-]/', '', $string); | |
return (ctype_digit($newstring)) ? TRUE : FALSE; | |
} | |
protected function isEmpty($statement) | |
{ | |
return (trim($statement) === '') ? TRUE : FALSE; | |
} | |
protected function isNone($statement) | |
{ | |
return (trim($statement) === '') ? $this->reasons['seeif'] : FALSE; | |
} | |
protected function isquestion($statement) | |
{ | |
$questionMark = substr(trim($statement), -1); | |
return ($questionMark === "?") ? $this->reasons['sure'] : FALSE; | |
} | |
protected function allcaps($statement) | |
{ | |
return (strtoupper($statement) === $statement && !$this->isEmpty($statement) && !$this->stringIsNumber($statement)) ? $this->reasons['chill'] : FALSE; | |
} | |
public function __call($method, $parameters) | |
{ | |
throw new \UnexpectedValueException("Validate rule [$method] does not exist!"); | |
} | |
} | |
class Francis { | |
public function yo($statement) | |
{ | |
$validate = new Validator; | |
$valid = $validate->setup(['statement' => $statement], [ | |
'statement' => 'allcaps|isquestion|isNone' | |
]); | |
if (count($valid) > 0) { | |
$empty = array(); | |
foreach ($valid as $message) { | |
if (!empty($message)) { | |
return $message; | |
} else { | |
$empty[] = 1; | |
} | |
} | |
if (count($empty) === count($valid)) { | |
return $validate->defaultReason; | |
} | |
} | |
} | |
} | |
/* TEST STARTS HERE - francisTest.php */ | |
require_once './Francis.php'; | |
class FrancisTest extends PHPUnit\Framework\TestCase { | |
public function setUp () { | |
$this->teen = new Francis(); | |
} | |
public function testStating () { | |
$this->assertEquals('Whatevs.', $this->teen->yo('Oh blah di, oh blah da.')); | |
} | |
public function testYelling () { | |
$this->assertEquals('Chill!', $this->teen->yo('GOOOAAAALLL!')); | |
} | |
public function testInquiring () { | |
$this->assertEquals('Sure.', $this->teen->yo('Did you finish your homework?')); | |
} | |
public function testInquiringWithNumber () { | |
$this->assertEquals('Sure.', $this->teen->yo('Was the score 1 to nil?')); | |
} | |
public function testStatingExcitedly () { | |
$this->assertEquals('Whatevs.', $this->teen->yo("I love that book!")); | |
} | |
public function testStatingWithAcronyms () { | |
$this->assertEquals('Whatevs.', $this->teen->yo("NPR just had a good piece on the BBC.")); | |
} | |
public function testYellingQuestions () { | |
$this->assertEquals('Chill!', $this->teen->yo('WHAT IS YOUR PROBLEM?')); | |
} | |
public function testYellingNumbers () { | |
$this->assertEquals('Chill!', $this->teen->yo('3, 2, 1, CONTACT!')); | |
} | |
public function testOnlyNumbers () { | |
$this->assertEquals('Whatevs.', $this->teen->yo('3, 2, 1')); | |
} | |
public function testQuestionWithJustNumbers () { | |
$this->assertEquals('Sure.', $this->teen->yo('7?')); | |
} | |
public function testYellingWithSpecialChars () { | |
$this->assertEquals('Chill!', $this->teen->yo('ZOMG WE 1 THE WORLD @*&#%$^ CUP!1!11!')); | |
} | |
public function testYellingWithoutPunctuation () { | |
$this->assertEquals('Chill!', $this->teen->yo('PAY ATTENTION')); | |
} | |
public function testStatingWithOddPunctuation () { | |
$this->assertEquals('Whatevs.', $this->teen->yo("Then I was like 'You good?'.")); | |
} | |
public function testInquiringEventually () { | |
$this->assertEquals('Sure.', $this->teen->yo("Woah! Hold up. Who said that?")); | |
} | |
public function testIgnoring () { | |
$this->assertEquals('See if I care!', $this->teen->yo('')); | |
} | |
public function testMoreIgnoring () { | |
$this->assertEquals('See if I care!', $this->teen->yo(null)); | |
} | |
public function testProlongedIgnoring () { | |
$this->assertEquals('See if I care!', $this->teen->yo(' ')); | |
} | |
public function testMultipleLines () { | |
$this->assertEquals('Whatevs.', $this->teen->yo(' | |
Is this done yet? | |
no | |
')); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment