Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rangevalidator.php
1<?php
10
14
15Loc::loadMessages(__FILE__);
16
18{
20 protected $min;
21
23 protected $max;
24
26 protected $equality;
27
29 protected $errorPhraseMinCode = 'MAIN_ENTITY_VALIDATOR_RANGE_MIN';
30 protected $errorPhraseMin;
31
33 protected $errorPhraseMaxCode = 'MAIN_ENTITY_VALIDATOR_RANGE_MAX';
34 protected $errorPhraseMax;
35
44 public function __construct($min = 0, $max = null, $equality = false, $errorPhrase = array('MIN' => null, 'MAX' => null))
45 {
46 if ($min !== null)
47 {
48 if (!is_numeric($min))
49 {
50 throw new ArgumentTypeException('min', 'numeric');
51 }
52
53 $this->min = $min;
54 }
55
56 if ($max !== null)
57 {
58 if (!is_numeric($max))
59 {
60 throw new ArgumentTypeException('max', 'numeric');
61 }
62
63 $this->max = $max;
64 }
65
66 $this->equality = (bool) $equality;
67
68 if (!empty($errorPhrase['MIN']))
69 {
70 $this->errorPhraseMin = $errorPhrase['MIN'];
71 }
72
73 if (!empty($errorPhrase['MAX']))
74 {
75 $this->errorPhraseMax = $errorPhrase['MAX'];
76 }
77
78 parent::__construct();
79 }
80
81
82 public function validate($value, $primary, array $row, ORM\Fields\Field $field)
83 {
84 if ($this->min !== null)
85 {
86 if ((!$this->equality && $value < $this->min) || ($this->equality && $value <= $this->min))
87 {
88 $mess = ($this->errorPhraseMin !== null? $this->errorPhraseMin : Loc::getMessage($this->errorPhraseMinCode));
89 return $this->getErrorMessage($value, $field, $mess, array("#MIN#" => $this->min));
90 }
91 }
92
93 if ($this->max !== null)
94 {
95 if ((!$this->equality && $value > $this->max) || ($this->equality && $value >= $this->max))
96 {
97 $mess = ($this->errorPhraseMax !== null? $this->errorPhraseMax : Loc::getMessage($this->errorPhraseMaxCode));
98 return $this->getErrorMessage($value, $field, $mess, array("#MAX#" => $this->max));
99 }
100 }
101
102 return true;
103 }
104}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
validate($value, $primary, array $row, ORM\Fields\Field $field)
__construct($min=0, $max=null, $equality=false, $errorPhrase=array('MIN'=> null, 'MAX'=> null))
getErrorMessage($value, ORM\Fields\Field $field, $errorPhrase=null, $additionalTemplates=null)
Definition validator.php:50