1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.4.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Validation\Rules; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait Length |
19
|
|
|
* @package Quantum\Libraries\Validation\Rules |
20
|
|
|
*/ |
21
|
|
|
trait Length |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adds validation Error |
26
|
|
|
* @param string $field |
27
|
|
|
* @param string $rule |
28
|
|
|
* @param mixed|null $param |
29
|
|
|
*/ |
30
|
|
|
abstract protected function addError(string $field, string $rule, $param = null); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Checks the min Length |
34
|
|
|
* @param string $field |
35
|
|
|
* @param string $value |
36
|
|
|
* @param null|mixed $param |
37
|
|
|
*/ |
38
|
|
|
protected function minLen(string $field, string $value, $param = null) |
39
|
|
|
{ |
40
|
|
|
if (!empty($value)) { |
41
|
|
|
|
42
|
|
|
$error = false; |
43
|
|
|
|
44
|
|
|
if (function_exists('mb_strlen')) { |
45
|
|
|
if (mb_strlen($value) < (int)$param) { |
46
|
|
|
$error = true; |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
if (strlen($value) < (int)$param) { |
50
|
|
|
$error = true; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($error) { |
55
|
|
|
$this->addError($field, 'minLen', $param); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks the max Length |
62
|
|
|
* @param string $field |
63
|
|
|
* @param string $value |
64
|
|
|
* @param null|mixed $param |
65
|
|
|
*/ |
66
|
|
|
protected function maxLen(string $field, string $value, $param = null) |
67
|
|
|
{ |
68
|
|
|
if (!empty($value)) { |
69
|
|
|
|
70
|
|
|
$error = false; |
71
|
|
|
|
72
|
|
|
if (function_exists('mb_strlen')) { |
73
|
|
|
if (mb_strlen($value) > (int)$param) { |
74
|
|
|
$error = true; |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
if (strlen($value) > (int)$param) { |
78
|
|
|
$error = true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($error) { |
83
|
|
|
$this->addError($field, 'maxLen', $param); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Checks the exact length |
91
|
|
|
* @param string $field |
92
|
|
|
* @param string $value |
93
|
|
|
* @param null|mixed $param |
94
|
|
|
*/ |
95
|
|
|
protected function exactLen(string $field, string $value, $param = null) |
96
|
|
|
{ |
97
|
|
|
if (!empty($value)) { |
98
|
|
|
|
99
|
|
|
$error = false; |
100
|
|
|
|
101
|
|
|
if (function_exists('mb_strlen')) { |
102
|
|
|
if (mb_strlen($value) !== (int)$param) { |
103
|
|
|
$error = true; |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
|
|
if (strlen($value) !== (int)$param) { |
107
|
|
|
$error = true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($error) { |
112
|
|
|
$this->addError($field, 'exactLen', $param); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |