|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHPMongo package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dmytro Sokil <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sokil\Mongo\Validator; |
|
13
|
|
|
|
|
14
|
|
|
use Sokil\Mongo\Structure; |
|
15
|
|
|
|
|
16
|
|
|
class LengthValidator extends \Sokil\Mongo\Validator |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
public function validateField(Structure $document, $fieldName, array $params) |
|
20
|
|
|
{ |
|
21
|
|
|
$value = $document->get($fieldName); |
|
22
|
|
|
|
|
23
|
|
|
if (!$value) { |
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$length = mb_strlen($value); |
|
28
|
|
|
|
|
29
|
|
|
// check if field is of specified length |
|
30
|
|
View Code Duplication |
if (isset($params['is'])) { |
|
|
|
|
|
|
31
|
|
|
if ($length === $params['is']) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($params['message'])) { |
|
36
|
|
|
$params['message'] = sprintf( |
|
37
|
|
|
'Field "%s" length not equal to %s in model %s', |
|
38
|
|
|
$fieldName, |
|
39
|
|
|
$params['is'], |
|
40
|
|
|
get_called_class() |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$document->addError($fieldName, $this->getName(), $params['message']); |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// check if fied is shorter than required |
|
49
|
|
View Code Duplication |
if (isset($params['min'])) { |
|
|
|
|
|
|
50
|
|
|
if ($length < $params['min']) { |
|
51
|
|
|
if (!isset($params['messageTooShort'])) { |
|
52
|
|
|
$params['messageTooShort'] = sprintf( |
|
53
|
|
|
'Field "%s" length is shorter than %s in model %s', |
|
54
|
|
|
$fieldName, |
|
55
|
|
|
$params['min'], |
|
56
|
|
|
get_called_class() |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$document->addError($fieldName, $this->getName(), $params['messageTooShort']); |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// check if fied is longer than required |
|
66
|
|
View Code Duplication |
if (isset($params['max'])) { |
|
|
|
|
|
|
67
|
|
|
if ($length > $params['max']) { |
|
68
|
|
|
if (!isset($params['messageTooLong'])) { |
|
69
|
|
|
$params['messageTooLong'] = sprintf( |
|
70
|
|
|
'Field "%s" length is longer than %s in model %s', |
|
71
|
|
|
$fieldName, |
|
72
|
|
|
$params['max'], |
|
73
|
|
|
get_called_class() |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$document->addError($fieldName, $this->getName(), $params['messageTooLong']); |
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.