jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Libraries/] [Validation/] [Validator.php] - Rev 2

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;
        }
    }

}

Powered by WebSVN 2.2.1