Skip to content

Instantly share code, notes, and snippets.

@nicolish
Created December 20, 2008 07:32
Show Gist options
  • Save nicolish/38261 to your computer and use it in GitHub Desktop.
Save nicolish/38261 to your computer and use it in GitHub Desktop.
<?php
class Controller_ModeA(){
function getPost($key){
return isset($_POST[$key])?$_POST[$key]:null;
}
private $validator;
function accept($validator){
$this->validator = $validator;
$this->validator->visit($this);
}
function getErrorMessage(){
return $this->validator->errorMessages;
}
}
class Validator_ModeA{
private $controller;
/*
* こいつをコントローラとどっちが持つかは悩みどころ。
*/
public $errorMessages = array();
function visit($controller){
$this->controller = $controller;
$this->execute();
}
private function execute(){
/*
* 基本。
*/
if(false === HogeID::validate($this->controller->getPost('hoge_id')){
$this->errorMessages[] = 'なんとかIDのフォーマットが不正です。';
}
/*
* エラー内容を直接返させる手もある。
*/
if(null !== MyDate::getValidateMessage($this->controller->getPost('date')){
$this->errorMessages[] = MyDate::getValidateMessage($this->controller->getPost('date');
}
/*
* 単純なのはその場でやる
*/
if(1000 < strlen(($this->controller->getPost('message'))){
$this->errorMessages[] = 'メッセージ長いです';
}
if(false === ctype_digit($this->controller->getPost('chojin_power'))){
$this->errorMessages[] = '超人パワーが数値ではありません';
}
}
}
?>
<?php
$c = new Controller_ModeA();
$c->accept(new Validator_ModeA());
/*
* ValidatorFactory::getInstance($modeFlag)とか、Controller_ModeA::getValidator()みたいに
* どっか他所でインスタンス生成するほうが実践的。
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment