1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Utils { |
4
|
|
|
|
5
|
|
|
use Str; |
6
|
|
|
|
7
|
|
|
class Validate extends \Validate { |
8
|
|
|
|
9
|
|
|
# Validate auth code |
10
|
|
|
|
11
|
|
|
public static function authCode(string $value) { |
12
|
|
|
|
13
|
|
|
return (preg_match(REGEX_USER_AUTH_CODE, $value) ? $value : false); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
# Validate user name |
17
|
|
|
|
18
|
|
|
public static function userName(string $value) { |
19
|
|
|
|
20
|
|
|
$min = CONFIG_USER_NAME_MIN_LENGTH; $max = CONFIG_USER_NAME_MAX_LENGTH; |
21
|
|
|
|
22
|
|
|
return ((preg_match(REGEX_USER_NAME, $value) && Str::between($value, $min, $max)) ? $value : false); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
# Validate user password |
26
|
|
|
|
27
|
|
|
public static function userPassword(string $value) { |
28
|
|
|
|
29
|
|
|
$min = CONFIG_USER_PASSWORD_MIN_LENGTH; $max = CONFIG_USER_PASSWORD_MAX_LENGTH; |
30
|
|
|
|
31
|
|
|
return ((preg_match(REGEX_USER_PASSWORD, $value) && Str::between($value, $min, $max)) ? $value : false); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
# Validate user email |
35
|
|
|
|
36
|
|
|
public static function userEmail(string $value) { |
37
|
|
|
|
38
|
|
|
return self::email($value); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
# Validate template component name |
42
|
|
|
|
43
|
|
|
public static function templateComponentName(string $value) { |
44
|
|
|
|
45
|
|
|
return (preg_match(REGEX_TEMPLATE_COMPONENT_NAME, $value) ? $value : false); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
# Validate file or directory name |
49
|
|
|
|
50
|
|
|
public static function fileName(string $value) { |
51
|
|
|
|
52
|
|
|
return (preg_match(REGEX_FILE_NAME, $value) ? $value : false); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
# Validate url |
56
|
|
|
|
57
|
|
|
public static function url(string $value) { |
58
|
|
|
|
59
|
|
|
if (false === ($value = parent::url($value))) return false; |
|
|
|
|
60
|
|
|
|
61
|
|
|
if (!preg_match('/^https?:\/\//', $value)) return false; |
|
|
|
|
62
|
|
|
|
63
|
|
|
# ------------------------ |
64
|
|
|
|
65
|
|
|
return rtrim($value, '/'); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.