Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
assert.php
1<?
10
11use Bitrix\Main;
13
14Loc::loadMessages(__FILE__);
15
16class Assert
17{
18 // checkers
19
31 public final static function expectInteger($arg, $argName = '', $customMsg = '')
32 {
33 $argInt = intval($arg);
34 if($arg != $argInt)
35 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_INTEGER_EXPECTED', $argName, $customMsg));
36
37 return $argInt;
38 }
39
51 public final static function expectIntegerPositive($arg, $argName = '', $customMsg = '')
52 {
53 $argInt = intval($arg);
54 if($arg != $argInt || $argInt <= 0)
55 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_INTEGER_NOTNULL_EXPECTED', $argName, $customMsg));
56
57 return $argInt;
58 }
59
71 public final static function expectIntegerNonNegative($arg, $argName = '', $customMsg = '')
72 {
73 $argInt = intval($arg);
74 if($arg != $argInt || $argInt < 0)
75 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_INTEGER_NONNEGATIVE_EXPECTED', $argName, $customMsg));
76
77 return $argInt;
78 }
79
91 public final static function expectStringNotNull($arg, $argName = '', $customMsg = '')
92 {
93 if($arg == '')
94 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_STRING_NOTNULL_EXPECTED', $argName, $customMsg));
95
96 return (string) $arg;
97 }
98
110 public final static function expectArray($arg, $argName = '', $customMsg = '')
111 {
112 if(!is_array($arg))
113 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ARRAY_EXPECTED', $argName, $customMsg));
114
115 return $arg;
116 }
117
129 public final static function expectNotEmptyArray($arg, $argName = '', $customMsg = '')
130 {
131 if(!is_array($arg) || empty($arg))
132 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ARRAY_NOT_EMPTY_EXPECTED', $argName, $customMsg));
133
134 return $arg;
135 }
136
148 public final static function expectArrayOfUniqueIntegerNotNull($arg, $argName = '', $customMsg = '')
149 {
150 if(!is_array($arg))
151 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ARRAY_EXPECTED', $argName, $customMsg));
152
153 $arg = array_unique(array_values($arg));
154
155 foreach($arg as $k => $v)
156 {
157 $vInt = intval($v);
158 if($v != $vInt || $vInt == 0)
159 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ARRAY_OF_INTEGER_NOT_NULL_EXPECTED', $argName, $customMsg));
160
161 $arg[$k] = $vInt; // it can be casted to integer
162 }
163
164 return $arg;
165 }
166
178 public final static function expectArrayOfUniqueStringNotNull($arg, $argName = '', $customMsg = '')
179 {
180 if(!is_array($arg))
181 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ARRAY_EXPECTED', $argName, $customMsg));
182
183 $arg = array_unique(array_values($arg));
184
185 foreach($arg as $k => $v)
186 {
187 $v = (string) $v;
188 if($v == '')
189 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ARRAY_OF_STRING_NOT_NULL_EXPECTED', $argName, $customMsg));
190
191 $arg[$k] = $v;
192 }
193
194 return $arg;
195 }
196
209 public final static function expectEnumerationMember($arg, $enum = array(), $argName = '', $customMsg = '')
210 {
211 if($arg == '')
212 throw new Main\ArgumentException(Loc::getMessage('SALE_LOCATION_ASSERT_EMPTY_ARGUMENT'));
213
214 if(!is_array($enum) || empty($enum))
215 throw new Main\ArgumentException(Loc::getMessage('SALE_LOCATION_ASSERT_EMPTY_ENUMERATION'));
216
217 // we cannot use in_array() here, kz we need for real data type
218 foreach($enum as $variant)
219 {
220 if($variant == $arg)
221 return $variant;
222 }
223
224 throw new Main\ArgumentException(self::formMessage('SALE_LOCATION_ASSERT_ITEM_NOT_IN_ENUMERATION', $argName, $customMsg));
225 }
226
227 // casters
228
229 public static function castTrimLC($value)
230 {
231 return ToLower(trim($value));
232 }
233
234 // announcers
235
243 public final static function announceNotImplemented($msg = '')
244 {
245 throw new Main\NotImplementedException($msg);
246 }
247
255 public final static function announceNotSupported($msg = '')
256 {
257 throw new Main\NotSupportedException($msg);
258 }
259
260 private static function formMessage($msgCode, $argName = '', $customMsg = '')
261 {
262 if($customMsg <> '')
263 {
264 return str_replace('#ARG_NAME#', $argName, $customMsg);
265 }
266
267 return Loc::getMessage($msgCode, array('#ARG_NAME#' => $argName <> ''? ' "'.$argName.'" ' : ' '));
268 }
269}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static expectArray($arg, $argName='', $customMsg='')
Definition assert.php:110
static expectIntegerNonNegative($arg, $argName='', $customMsg='')
Definition assert.php:71
static expectNotEmptyArray($arg, $argName='', $customMsg='')
Definition assert.php:129
static expectIntegerPositive($arg, $argName='', $customMsg='')
Definition assert.php:51
static expectArrayOfUniqueStringNotNull($arg, $argName='', $customMsg='')
Definition assert.php:178
static announceNotImplemented($msg='')
Definition assert.php:243
static expectArrayOfUniqueIntegerNotNull($arg, $argName='', $customMsg='')
Definition assert.php:148
static expectStringNotNull($arg, $argName='', $customMsg='')
Definition assert.php:91
static announceNotSupported($msg='')
Definition assert.php:255
static expectEnumerationMember($arg, $enum=array(), $argName='', $customMsg='')
Definition assert.php:209
static expectInteger($arg, $argName='', $customMsg='')
Definition assert.php:31