diff --git a/Question/ValidableQuestion.php b/Question/ValidableQuestion.php new file mode 100644 index 000000000..b5c1db969 --- /dev/null +++ b/Question/ValidableQuestion.php @@ -0,0 +1,53 @@ +rules = $rules; + $this->setValidator($this->validate()); + } + + private function validate() + { + $rules = $this->rules; + + return function ($answer) use ($rules) { + foreach ($rules as $rule){ + $answer = $this->validateRule($rule, $answer); + } + return $answer; + }; + } + + private function validateRule($rule, $answer) + { + if ($rule == 'required'){ + if (!$answer) { + throw new \InvalidArgumentException( + 'Come on, give me something!' + ); + } + } + + if ($rule == 'numeric'){ + if (!is_numeric($answer)) { + throw new \RuntimeException( + 'Please, enter a num3r1c value' + ); + } + } + + return $answer; + } +} \ No newline at end of file