Skip to content

Instantly share code, notes, and snippets.

@yethee
Created June 7, 2011 16:13
Show Gist options
  • Save yethee/1012574 to your computer and use it in GitHub Desktop.
Save yethee/1012574 to your computer and use it in GitHub Desktop.
Using ValidableInterface
<?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.');
}
}
}
@yethee
Copy link
Author

yethee commented Jun 8, 2011

Ну этот интерфейс решает проблему простых проверок данных модели, без внешних зависимостей. Те проверки, которые могли бы быть в setter-ах.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment