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