1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Libraries\Validation; |
4 | | | |
5 | | | use Doctrine\Common\Annotations\AnnotationReader; |
6 | | | use Phem\Core\Collection; |
7 | | | use Phem\Core\Object; |
8 | | | use Phem\Libraries\Forms\FormBuilderException; |
9 | | | use Phem\Libraries\Validation\Annotations\Validate; |
10 | | | use ReflectionClass; |
11 | | | use ReflectionProperty; |
12 | | | |
13 | | | /** |
14 | | | * @author kubapet |
15 | | | */ |
16 | | | class 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 | | | } |