Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
booleanfield.php
1<?php
10
12
20{
25 protected $values;
26
35 function __construct($name, $parameters = array())
36 {
37 parent::__construct($name, $parameters);
38
39 if (empty($parameters['values']))
40 {
41 $this->values = array(false, true);
42 }
43 else
44 {
45 $this->values = $parameters['values'];
46 }
47
48 $this->addSaveDataModifier(array($this, 'normalizeValue'));
49 }
50
57 public function configureStorageValues($falseValue, $trueValue)
58 {
59 $this->values = [$falseValue, $trueValue];
60 return $this;
61 }
62
71 public function configureValues($falseValue, $trueValue)
72 {
73 return $this->configureStorageValues($falseValue, $trueValue);
74 }
75
81 public function normalizeValue($value)
82 {
83 if (
84 (is_string($value) && ($value == '1' || $value == '0'))
85 ||
86 (is_bool($value))
87 )
88 {
89 $value = (int) $value;
90 }
91 elseif (is_string($value) && $value == 'true')
92 {
93 $value = 1;
94 }
95 elseif (is_string($value) && $value == 'false')
96 {
97 $value = 0;
98 }
99
100 if (is_integer($value) && ($value == 1 || $value == 0))
101 {
102 $value = $this->values[$value];
103 }
104
105 return $value;
106 }
107
115 public function booleanizeValue($value)
116 {
117 if (is_bool($value))
118 {
119 return $value;
120 }
121
122 $normalizedValue = $this->normalizeValue($value);
123 return (bool) array_search($normalizedValue, $this->values, true);
124 }
125
131 public function getValidators()
132 {
133 $validators = parent::getValidators();
134
135 if ($this->validation === null)
136 {
138 }
139
140 return $validators;
141 }
142
143 public function getValues()
144 {
145 return $this->values;
146 }
147
148 public function isValueEmpty($value)
149 {
150 return (strval($value) === '' && $value !== false);
151 }
152
158 public function cast($value)
159 {
160 if ($this->is_nullable && $value === null)
161 {
162 return $value;
163 }
164
165 if ($value instanceof SqlExpression)
166 {
167 return $value;
168 }
169
170 return $this->booleanizeValue($value);
171 }
172
178 public function convertValueFromDb($value)
179 {
180 return $this->booleanizeValue($value);
181 }
182
189 public function convertValueToDb($value)
190 {
191 if ($value instanceof SqlExpression)
192 {
193 return $value;
194 }
195
196 return $value === null && $this->is_nullable
197 ? $value
198 : $this->getConnection()->getSqlHelper()->convertToDbString(
199 $this->normalizeValue($value)
200 );
201 }
202
206 public function getGetterTypeHint()
207 {
208 return $this->getNullableTypeHint('\\boolean');
209 }
210
214 public function getSetterTypeHint()
215 {
216 return $this->getNullableTypeHint('\\boolean');
217 }
218}
configureStorageValues($falseValue, $trueValue)
configureValues($falseValue, $trueValue)
__construct($name, $parameters=array())
addSaveDataModifier($modifier)
Definition field.php:420