freenet-router |
Subversion Repositories: |
Compare with Previous - Blame - Download
<?php
namespace Phem\Libraries\Validation;
use Doctrine\Common\Annotations\AnnotationReader;
use Phem\Core\Collection;
use Phem\Core\Object;
use Phem\Libraries\Forms\FormBuilderException;
use Phem\Libraries\Validation\Annotations\Validate;
use ReflectionClass;
use ReflectionProperty;
/**
* @author kubapet
*/
class Validator extends Object
{
public function validate($object, $errorPrefix = "")
{
$errors = new Collection();
$className = get_class($object);
$r = new ReflectionClass($className);
$annotationReader = new AnnotationReader();
$properties = $r->getProperties();
foreach ($properties as $prop)
{
/* @var $prop ReflectionProperty */
$getMethod = "get" . ucFirst($prop->name);
if (!method_exists($object, $getMethod))
{
throw new FormBuilderException(
"FormBuilder: Missing get method for property "
. $prop->name . " of class "
. get_class($object)
);
}
$annotations = $annotationReader->getPropertyAnnotations($prop);
foreach ($annotations as $annotation)
{
if ($annotation instanceof Validate)
{
switch ($annotation->type)
{
case "ascii":
if (!preg_match('/^[A-Za-z]*$/',$object->$getMethod()))
$errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smĂ obsahovat pouze znaky anglickĂŠ abecedy');
break;
case "alphabet":
if (!preg_match('/^[A-Za-zÄĹĄÄĹŞýåĂÊóżÄĹĹŻĂşÄĹ ÄĹĹ˝ĂĂĂĂĂÄŤĹĹŽĂ]*$/',$object->$getMethod()))
$errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smĂ obsahovat pouze znaky abecedy');
break;
case "alphabetext":
if (!preg_match('/^[A-Za-zÄĹĄÄĹŞýåĂÊóżÄĹĹŻĂşÄĹ ÄĹĹ˝ĂĂĂĂĂÄŤĹĹŽĂ ,._-]*$/',$object->$getMethod()))
$errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" obsahuje nepovolenĂŠ znaky');
break;
case "alphanumeric":
if (!preg_match('/^[0-9A-Za-zÄĹĄÄĹŞýåĂÊóżÄĹĹŻĂşÄĹ ÄĹĹ˝ĂĂĂĂĂÄŤĹĹŽĂ]*$/',$object->$getMethod()))
$errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smĂ obsahovat pouze znaky abecedy a ÄĂslice');
break;
case "password":
if (!preg_match('/[[:print:]]{8,16}/',$object->$getMethod()))
$errors->put($errorPrefix . $prop->name, 'Heslo musà obsahovat jen tisknutelnÊ znaky a musà být dlouhÊ 8-16 znaků');
break;
case "alphanumericext":
if (!preg_match('/^[0-9A-Za-zÄĹĄÄĹŞýåĂÊóżÄĹĹŻĂşÄĹ ÄĹĹ˝ĂĂĂĂĂÄŤĹĹŽĂ ,._-]*$/',$object->$getMethod()))
$errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smĂ obsahovat pouze znaky abecedy a ÄĂslice');
break;
case "integer":
if (!filter_var($object->$getMethod(), FILTER_VALIDATE_INT))
$errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" musĂ bĂ˝t celĂŠ ÄĂslo');
break;
case "email":
if (!filter_var($object->$getMethod(), FILTER_VALIDATE_EMAIL))
$errors->put($errorPrefix . $prop->name, "Ĺ patnĂ˝ formĂĄt E-mailovĂŠ adresy");
break;
}
}
}
}
if ($errors->isEmpty())
{
return false;
}
else
{
return $errors;
}
}
}