1+ <?php
2+
3+ namespace Library \Helper ;
4+
5+ class Validator
6+ {
7+ private function __construct (){}
8+
9+ /**
10+ * Validate an email address
11+ */
12+ public static function validateEmail ($ email )
13+ {
14+ return filter_var ($ email , FILTER_VALIDATE_EMAIL );
15+ }
16+
17+ /**
18+ * Validate a URL
19+ */
20+ public static function validateUrl ($ url )
21+ {
22+ return filter_var ($ url , FILTER_VALIDATE_URL );
23+ }
24+
25+ /**
26+ * Validate a float number
27+ */
28+ public static function validateFloat ($ value )
29+ {
30+ return filter_var ($ value , FILTER_VALIDATE_FLOAT );
31+ }
32+
33+ /**
34+ * Validate an integer number
35+ */
36+ public static function validateInteger ($ value )
37+ {
38+ return filter_var ($ value , FILTER_VALIDATE_INT );
39+ }
40+ }
41+ //Read more at http://www.devshed.com/c/a/PHP/PHP-Effects-of-Wrapping-Code-in-Class-Constructs/#TAxCLtYVsdVRXVRp.99
42+
43+ // include the validator helper
44+ //require_once __DIR__ . '/Helper/Validator.php';
45+
46+ use Library \Helper \Validator as Validator ;
47+
48+ // validate a URL
49+ if (!Validator::validateUrl ('http://www.devshed.com ' )) {
50+ echo 'The supplied URL is invalid ' ;
51+ }
52+
53+ // validate an email address
54+ if (!Validator::validateEMail ('user@domain.com ' )) {
55+ echo 'The supplied email address is invalid. ' ;
56+ }
57+
58+ // validate a float number
59+ if (!Validator::validateFloat (2.5 )) {
60+ echo 'The supplied value is an invalid float number. ' ;
61+ }
62+
63+ // validate an integer number
64+ if (!Validator::validateInteger (10.5 )) {
65+ echo 'The supplied value is an invalid integer number. ' ;
66+ }
67+
68+ /* displays the following
69+
70+ The supplied value is an invalid integer number.
71+
72+ */
73+ //Read more at http://www.devshed.com/c/a/PHP/PHP-Effects-of-Wrapping-Code-in-Class-Constructs/#TAxCLtYVsdVRXVRp.99
74+
75+ ?>
76+ <?php
77+
78+
79+ namespace Library \Helper ;
80+
81+ interface ValidatorInterface
82+ {
83+ /**
84+ * Validate the given value
85+ */
86+ public function validate ();
87+ }
88+ //Read more at http://www.devshed.com/c/a/PHP/PHP-Effects-of-Wrapping-Code-in-Class-Constructs/1/#3U5hfgYAl7RKFcHh.99
89+
90+
91+ namespace Library \Helper ;
92+
93+ abstract class AbstractValidator
94+ {
95+ const DEFAULT_ERROR_MESAGE = 'The supplied value is invalid. ' ;
96+ protected $ _value ;
97+ protected $ _errorMessage = self ::DEFAULT_ERROR_MESAGE ;
98+
99+ /**
100+ * Constructor
101+ */
102+ public function __construct ($ value = null , $ errorMessage = null )
103+ {
104+ if ($ value !== null ) {
105+ $ this ->setValue ($ value );
106+ }
107+ if ($ errorMessage !== null ) {
108+ $ this ->setErrorMessage ($ errorMessage );
109+ }
110+ }
111+
112+ /**
113+ * Set the value to be validated
114+ */
115+ public function setValue ($ value )
116+ {
117+ $ this ->_value = $ value ;
118+ }
119+
120+ /**
121+ * Get the inputted value
122+ */
123+ public function getValue ()
124+ {
125+ return $ this ->_value ;
126+ }
127+
128+ /**
129+ * Set the error message
130+ */
131+ public function setErrorMessage ($ errorMessage )
132+ {
133+ if (!is_string ($ errorMessage ) || empty ($ errorMessage )) {
134+ throw new \InvalidArgumentException ('The error message is invalid. It must be a non-empty string. ' );
135+ }
136+ $ this ->_errorMessage = $ errorMessage ;
137+ }
138+
139+ /**
140+ * Get the error message
141+ */
142+ public function getErrorMessage ()
143+ {
144+ return $ this ->_errorMessage ;
145+ }
146+
147+ /**
148+ * Reset the error message to the default value
149+ */
150+ public function resetErrorMessage ()
151+ {
152+ $ this ->_errorMessage = self ::DEFAULT_ERROR_MESAGE ;
153+ }
154+ }
155+ //Read more at http://www.devshed.com/c/a/PHP/PHP-Effects-of-Wrapping-Code-in-Class-Constructs/1/#3U5hfgYAl7RKFcHh.99
156+
157+
158+ ?>
0 commit comments