jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Libraries/] [Validation/] [Validator.php] - Blame information for rev 2

 

Line No. Rev Author Line
12simandl<?php
2 
3namespace Phem\Libraries\Validation;
4 
5use Doctrine\Common\Annotations\AnnotationReader;
6use Phem\Core\Collection;
7use Phem\Core\Object;
8use Phem\Libraries\Forms\FormBuilderException;
9use Phem\Libraries\Validation\Annotations\Validate;
10use ReflectionClass;
11use ReflectionProperty;
12 
13/**
14 * @author kubapet
15 */
16class Validator extends Object
17{
18 
19 public function validate($object, $errorPrefix = "")
20 {
21 
22 $errors = new Collection();
23 $className = get_class($object);
24 $r = new ReflectionClass($className);
25 
26 $annotationReader = new AnnotationReader();
27 
28 $properties = $r->getProperties();
29 
30 foreach ($properties as $prop)
31 {
32 /* @var $prop ReflectionProperty */
33 $getMethod = "get" . ucFirst($prop->name);
34 if (!method_exists($object, $getMethod))
35 {
36 throw new FormBuilderException(
37 "FormBuilder: Missing get method for property "
38 . $prop->name . " of class "
39 . get_class($object)
40 );
41 }
42 
43 $annotations = $annotationReader->getPropertyAnnotations($prop);
44 
45 foreach ($annotations as $annotation)
46 {
47 if ($annotation instanceof Validate)
48 {
49 switch ($annotation->type)
50 {
51 case "ascii":
52 if (!preg_match('/^[A-Za-z]*$/',$object->$getMethod()))
53 $errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smĂ­ obsahovat pouze znaky anglickĂŠ abecedy');
54 break;
55 case "alphabet":
56 if (!preg_match('/^[A-Za-zěščřžýáíéóťďňůúĚŠČŘŽÝÁÍÉÓĎŤŇŮÚ]*$/',$object->$getMethod()))
57 $errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smĂ­ obsahovat pouze znaky abecedy');
58 break;
59 case "alphabetext":
60 if (!preg_match('/^[A-Za-zěščřžýáíéóťďňůúĚŠČŘŽÝÁÍÉÓĎŤŇŮÚ ,._-]*$/',$object->$getMethod()))
61 $errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" obsahuje nepovolenĂŠ znaky');
62 break;
63 case "alphanumeric":
64 if (!preg_match('/^[0-9A-Za-zěščřžýáíéóťďňůúĚŠČŘŽÝÁÍÉÓĎŤŇŮÚ]*$/',$object->$getMethod()))
65 $errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smí obsahovat pouze znaky abecedy a číslice');
66 break;
67 case "password":
68 if (!preg_match('/[[:print:]]{8,16}/',$object->$getMethod()))
69 $errors->put($errorPrefix . $prop->name, 'Heslo musí obsahovat jen tisknutelnÊ znaky a musí být dlouhÊ 8-16 znaků');
70 break;
71 case "alphanumericext":
72 if (!preg_match('/^[0-9A-Za-zěščřžýáíéóťďňůúĚŠČŘŽÝÁÍÉÓĎŤŇŮÚ ,._-]*$/',$object->$getMethod()))
73 $errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" smí obsahovat pouze znaky abecedy a číslice');
74 break;
75 case "integer":
76 if (!filter_var($object->$getMethod(), FILTER_VALIDATE_INT))
77 $errors->put($errorPrefix . $prop->name, 'Pole "' . $prop->name . '" musí být celé číslo');
78 break;
79 case "email":
80 if (!filter_var($object->$getMethod(), FILTER_VALIDATE_EMAIL))
81 $errors->put($errorPrefix . $prop->name, "Ĺ patnĂ˝ formĂĄt E-mailovĂŠ adresy");
82 break;
83 }
84 }
85 }
86 }
87 
88 
89 if ($errors->isEmpty())
90 {
91 return false;
92 }
93 else
94 {
95 return $errors;
96 }
97 }
98 
99}

Powered by WebSVN 2.2.1