Created
June 7, 2011 16:13
-
-
Save yethee/1012574 to your computer and use it in GitHub Desktop.
Using ValidableInterface
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 | |
use Biplane\CoreBundle\Model\ValidableInterface; | |
use Biplane\CoreBundle\Model\Exception\ValidateException; | |
class User implements ValidableInterface | |
{ | |
private $name; | |
private $email; | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function setName($name) | |
{ | |
$this->name = (string)$name; | |
} | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
public function setEmail($name) | |
{ | |
$this->email = (string)$name; | |
} | |
/** | |
* Implement of ValidableInterface | |
*/ | |
public function validate() | |
{ | |
if (empty($this->name)) { | |
throw new ValidateException('Name cannot be empty.'); | |
} | |
if (empty($this->email)) { | |
throw new ValidateException('E-mail cannot be empty.'); | |
} | |
else if (filter_var($this->email, FILTER_VALIDATE_EMAIL)) { | |
throw new ValidateException('E-mail is invalid.'); | |
} | |
} | |
} |
Ну этот интерфейс решает проблему простых проверок данных модели, без внешних зависимостей. Те проверки, которые могли бы быть в setter-ах.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
А проверка уникальности имени?