Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
lengthvalidator.php
1<?php
10
15
16Loc::loadMessages(__FILE__);
17
19{
21 protected $min;
22
24 protected $max;
25
27 protected $errorPhraseMinCode = 'MAIN_ENTITY_VALIDATOR_LENGTH_MIN';
28 protected $errorPhraseMin;
29
31 protected $errorPhraseMaxCode = 'MAIN_ENTITY_VALIDATOR_LENGTH_MAX';
32 protected $errorPhraseMax;
33
41 public function __construct($min = 1, $max = null, $errorPhrase = array('MIN' => null, 'MAX' => null))
42 {
43 if ($min !== null)
44 {
45 if (!is_int($min))
46 {
47 throw new ArgumentTypeException('min', 'integer');
48 }
49
50 $this->min = $min;
51 }
52
53 if ($max !== null)
54 {
55 if (!is_int($max))
56 {
57 throw new ArgumentTypeException('max', 'integer');
58 }
59
60 $this->max = $max;
61 }
62
63 if (!empty($errorPhrase['MIN']))
64 {
65 $this->errorPhraseMin = $errorPhrase['MIN'];
66 }
67
68 if (!empty($errorPhrase['MAX']))
69 {
70 $this->errorPhraseMax = $errorPhrase['MAX'];
71 }
72
73 parent::__construct();
74 }
75
87 public function validate($value, $primary, array $row, Field $field)
88 {
89 if ($field instanceof ScalarField && $field->isNullable() && $value === null)
90 {
91 return true;
92 }
93
94 if ($this->min !== null)
95 {
96 if (mb_strlen((string)$value) < $this->min)
97 {
98 $mess = ($this->errorPhraseMin !== null? $this->errorPhraseMin : Loc::getMessage($this->errorPhraseMinCode));
99 return $this->getErrorMessage($value, $field, $mess, array("#MIN_LENGTH#" => $this->min));
100 }
101 }
102
103 if ($this->max !== null)
104 {
105 if (mb_strlen((string)$value) > $this->max)
106 {
107 $mess = ($this->errorPhraseMax !== null? $this->errorPhraseMax : Loc::getMessage($this->errorPhraseMaxCode));
108 return $this->getErrorMessage($value, $field, $mess, array("#MAX_LENGTH#" => $this->max));
109 }
110 }
111
112 return true;
113 }
114
121 public function getMin()
122 {
123 return $this->min;
124 }
125
132 public function getMax()
133 {
134 return $this->max;
135 }
136}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
validate($value, $primary, array $row, Field $field)
__construct($min=1, $max=null, $errorPhrase=array('MIN'=> null, 'MAX'=> null))
getErrorMessage($value, ORM\Fields\Field $field, $errorPhrase=null, $additionalTemplates=null)
Definition validator.php:50