Complex classes like Assertion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assertion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
146 | class Assertion |
||
147 | { |
||
148 | const INVALID_FLOAT = 9; |
||
149 | const INVALID_INTEGER = 10; |
||
150 | const INVALID_DIGIT = 11; |
||
151 | const INVALID_INTEGERISH = 12; |
||
152 | const INVALID_BOOLEAN = 13; |
||
153 | const VALUE_EMPTY = 14; |
||
154 | const VALUE_NULL = 15; |
||
155 | const INVALID_STRING = 16; |
||
156 | const INVALID_REGEX = 17; |
||
157 | const INVALID_MIN_LENGTH = 18; |
||
158 | const INVALID_MAX_LENGTH = 19; |
||
159 | const INVALID_STRING_START = 20; |
||
160 | const INVALID_STRING_CONTAINS = 21; |
||
161 | const INVALID_CHOICE = 22; |
||
162 | const INVALID_NUMERIC = 23; |
||
163 | const INVALID_ARRAY = 24; |
||
164 | const INVALID_KEY_EXISTS = 26; |
||
165 | const INVALID_NOT_BLANK = 27; |
||
166 | const INVALID_INSTANCE_OF = 28; |
||
167 | const INVALID_SUBCLASS_OF = 29; |
||
168 | const INVALID_RANGE = 30; |
||
169 | const INVALID_ALNUM = 31; |
||
170 | const INVALID_TRUE = 32; |
||
171 | const INVALID_EQ = 33; |
||
172 | const INVALID_SAME = 34; |
||
173 | const INVALID_MIN = 35; |
||
174 | const INVALID_MAX = 36; |
||
175 | const INVALID_LENGTH = 37; |
||
176 | const INVALID_FALSE = 38; |
||
177 | const INVALID_STRING_END = 39; |
||
178 | const INVALID_UUID = 40; |
||
179 | const INVALID_COUNT = 41; |
||
180 | const INVALID_NOT_EQ = 42; |
||
181 | const INVALID_NOT_SAME = 43; |
||
182 | const INVALID_TRAVERSABLE = 44; |
||
183 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
184 | const INVALID_KEY_ISSET = 46; |
||
185 | const INVALID_DIRECTORY = 101; |
||
186 | const INVALID_FILE = 102; |
||
187 | const INVALID_READABLE = 103; |
||
188 | const INVALID_WRITEABLE = 104; |
||
189 | const INVALID_CLASS = 105; |
||
190 | const INVALID_EMAIL = 201; |
||
191 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
192 | const INVALID_URL = 203; |
||
193 | const INVALID_NOT_INSTANCE_OF = 204; |
||
194 | const VALUE_NOT_EMPTY = 205; |
||
195 | const INVALID_JSON_STRING = 206; |
||
196 | const INVALID_OBJECT = 207; |
||
197 | const INVALID_METHOD = 208; |
||
198 | const INVALID_SCALAR = 209; |
||
199 | const INVALID_LESS = 210; |
||
200 | const INVALID_LESS_OR_EQUAL = 211; |
||
201 | const INVALID_GREATER = 212; |
||
202 | const INVALID_GREATER_OR_EQUAL = 212; |
||
203 | const INVALID_DATE = 213; |
||
204 | const INVALID_CALLABLE = 214; |
||
205 | |||
206 | /** |
||
207 | * Exception to throw when an assertion failed. |
||
208 | * |
||
209 | * @var string |
||
210 | */ |
||
211 | static protected $exceptionClass = 'Assert\InvalidArgumentException'; |
||
212 | |||
213 | /** |
||
214 | * Helper method that handles building the assertion failure exceptions. |
||
215 | * They are returned from this method so that the stack trace still shows |
||
216 | * the assertions method. |
||
217 | */ |
||
218 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
219 | { |
||
220 | $exceptionClass = static::$exceptionClass; |
||
221 | return new $exceptionClass($message, $code, $propertyPath, $value, $constraints); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Assert that two values are equal (using == ). |
||
226 | * |
||
227 | * @param mixed $value |
||
228 | * @param mixed $value2 |
||
229 | * @param string|null $message |
||
230 | * @param string|null $propertyPath |
||
231 | * @return void |
||
232 | * @throws \Assert\AssertionFailedException |
||
233 | */ |
||
234 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
235 | { |
||
236 | if ($value != $value2) { |
||
237 | $message = sprintf( |
||
|
|||
238 | $message ?: 'Value "%s" does not equal expected value "%s".', |
||
239 | self::stringify($value), |
||
240 | self::stringify($value2) |
||
241 | ); |
||
242 | |||
243 | throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2)); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Assert that two values are the same (using ===). |
||
249 | * |
||
250 | * @param mixed $value |
||
251 | * @param mixed $value2 |
||
252 | * @param string|null $message |
||
253 | * @param string|null $propertyPath |
||
254 | * @return void |
||
255 | * @throws \Assert\AssertionFailedException |
||
256 | */ |
||
257 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
258 | { |
||
259 | if ($value !== $value2) { |
||
260 | $message = sprintf( |
||
261 | $message ?: 'Value "%s" is not the same as expected value "%s".', |
||
262 | self::stringify($value), |
||
263 | self::stringify($value2) |
||
264 | ); |
||
265 | |||
266 | throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2)); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Assert that two values are not equal (using == ). |
||
272 | * |
||
273 | * @param mixed $value1 |
||
274 | * @param mixed $value2 |
||
275 | * @param string|null $message |
||
276 | * @param string|null $propertyPath |
||
277 | * @return void |
||
278 | * @throws \Assert\AssertionFailedException |
||
279 | */ |
||
280 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
281 | { |
||
282 | if ($value1 == $value2) { |
||
283 | $message = sprintf( |
||
284 | $message ?: 'Value "%s" is equal to expected value "%s".', |
||
285 | self::stringify($value1), |
||
286 | self::stringify($value2) |
||
287 | ); |
||
288 | throw static::createException($value1, $message,static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2)); |
||
289 | } |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Assert that two values are not the same (using === ). |
||
294 | * |
||
295 | * @param mixed $value1 |
||
296 | * @param mixed $value2 |
||
297 | * @param string|null $message |
||
298 | * @param string|null $propertyPath |
||
299 | * @return void |
||
300 | * @throws \Assert\AssertionFailedException |
||
301 | */ |
||
302 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
303 | { |
||
304 | if ($value1 === $value2) { |
||
305 | $message = sprintf( |
||
306 | $message ?: 'Value "%s" is the same as expected value "%s".', |
||
307 | self::stringify($value1), |
||
308 | self::stringify($value2) |
||
309 | ); |
||
310 | throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2)); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Assert that value is a php integer. |
||
316 | * |
||
317 | * @param mixed $value |
||
318 | * @param string|null $message |
||
319 | * @param string|null $propertyPath |
||
320 | * @return void |
||
321 | * @throws \Assert\AssertionFailedException |
||
322 | */ |
||
323 | public static function integer($value, $message = null, $propertyPath = null) |
||
324 | { |
||
325 | if ( ! is_int($value)) { |
||
326 | $message = sprintf( |
||
327 | $message ?: 'Value "%s" is not an integer.', |
||
328 | self::stringify($value) |
||
329 | ); |
||
330 | |||
331 | throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Assert that value is a php float. |
||
337 | * |
||
338 | * @param mixed $value |
||
339 | * @param string|null $message |
||
340 | * @param string|null $propertyPath |
||
341 | * @return void |
||
342 | * @throws \Assert\AssertionFailedException |
||
343 | */ |
||
344 | public static function float($value, $message = null, $propertyPath = null) |
||
345 | { |
||
346 | if ( ! is_float($value)) { |
||
347 | $message = sprintf( |
||
348 | $message ?: 'Value "%s" is not a float.', |
||
349 | self::stringify($value) |
||
350 | ); |
||
351 | |||
352 | throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Validates if an integer or integerish is a digit. |
||
358 | * |
||
359 | * @param mixed $value |
||
360 | * @param string|null $message |
||
361 | * @param string|null $propertyPath |
||
362 | * @return void |
||
363 | * @throws \Assert\AssertionFailedException |
||
364 | */ |
||
365 | public static function digit($value, $message = null, $propertyPath = null) |
||
366 | { |
||
367 | if ( ! ctype_digit((string)$value)) { |
||
368 | $message = sprintf( |
||
369 | $message ?: 'Value "%s" is not a digit.', |
||
370 | self::stringify($value) |
||
371 | ); |
||
372 | |||
373 | throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Assert that value is a php integer'ish. |
||
379 | * |
||
380 | * @param mixed $value |
||
381 | * @param string|null $message |
||
382 | * @param string|null $propertyPath |
||
383 | * @return void |
||
384 | * @throws \Assert\AssertionFailedException |
||
385 | */ |
||
386 | public static function integerish($value, $message = null, $propertyPath = null) |
||
387 | { |
||
388 | if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) { |
||
389 | $message = sprintf( |
||
390 | $message ?: 'Value "%s" is not an integer or a number castable to integer.', |
||
391 | self::stringify($value) |
||
392 | ); |
||
393 | |||
394 | throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Assert that value is php boolean |
||
400 | * |
||
401 | * @param mixed $value |
||
402 | * @param string|null $message |
||
403 | * @param string|null $propertyPath |
||
404 | * @return void |
||
405 | * @throws \Assert\AssertionFailedException |
||
406 | */ |
||
407 | public static function boolean($value, $message = null, $propertyPath = null) |
||
408 | { |
||
409 | if ( ! is_bool($value)) { |
||
410 | $message = sprintf( |
||
411 | $message ?: 'Value "%s" is not a boolean.', |
||
412 | self::stringify($value) |
||
413 | ); |
||
414 | |||
415 | throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath); |
||
416 | } |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Assert that value is a PHP scalar |
||
421 | * |
||
422 | * @param mixed $value |
||
423 | * @param string|null $message |
||
424 | * @param string|null $propertyPath |
||
425 | * @return void |
||
426 | * @throws \Assert\AssertionFailedException |
||
427 | */ |
||
428 | public static function scalar($value, $message = null, $propertyPath = null) |
||
429 | { |
||
430 | if (!is_scalar($value)) { |
||
431 | $message = sprintf( |
||
432 | $message ?: 'Value "%s" is not a scalar.', |
||
433 | self::stringify($value) |
||
434 | ); |
||
435 | |||
436 | throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath); |
||
437 | } |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Assert that value is not empty |
||
442 | * |
||
443 | * @param mixed $value |
||
444 | * @param string|null $message |
||
445 | * @param string|null $propertyPath |
||
446 | * @return void |
||
447 | * @throws \Assert\AssertionFailedException |
||
448 | */ |
||
449 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
450 | { |
||
451 | if (empty($value)) { |
||
452 | $message = sprintf( |
||
453 | $message ?: 'Value "%s" is empty, but non empty value was expected.', |
||
454 | self::stringify($value) |
||
455 | ); |
||
456 | |||
457 | throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath); |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Assert that value is empty |
||
463 | * |
||
464 | * @param mixed $value |
||
465 | * @param string|null $message |
||
466 | * @param string|null $propertyPath |
||
467 | * @return void |
||
468 | * @throws \Assert\AssertionFailedException |
||
469 | */ |
||
470 | public static function noContent($value, $message = null, $propertyPath = null) |
||
471 | { |
||
472 | if (!empty($value)) { |
||
473 | $message = sprintf( |
||
474 | $message ?: 'Value "%s" is not empty, but empty value was expected.', |
||
475 | self::stringify($value) |
||
476 | ); |
||
477 | |||
478 | throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath); |
||
479 | } |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Assert that value is not null |
||
484 | * |
||
485 | * @param mixed $value |
||
486 | * @param string|null $message |
||
487 | * @param string|null $propertyPath |
||
488 | * @return void |
||
489 | * @throws \Assert\AssertionFailedException |
||
490 | */ |
||
491 | public static function notNull($value, $message = null, $propertyPath = null) |
||
492 | { |
||
493 | if ($value === null) { |
||
494 | $message = sprintf( |
||
495 | $message ?: 'Value "%s" is null, but non null value was expected.', |
||
496 | self::stringify($value) |
||
497 | ); |
||
498 | |||
499 | throw static::createException($value, $message, static::VALUE_NULL, $propertyPath); |
||
500 | } |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Assert that value is a string |
||
505 | * |
||
506 | * @param mixed $value |
||
507 | * @param string|null $message |
||
508 | * @param string|null $propertyPath |
||
509 | * @return void |
||
510 | * @throws \Assert\AssertionFailedException |
||
511 | */ |
||
512 | public static function string($value, $message = null, $propertyPath = null) |
||
513 | { |
||
514 | if ( ! is_string($value)) { |
||
515 | $message = sprintf( |
||
516 | $message ?: 'Value "%s" expected to be string, type %s given.', |
||
517 | self::stringify($value), |
||
518 | gettype($value) |
||
519 | ); |
||
520 | |||
521 | throw static::createException($value, $message, static::INVALID_STRING, $propertyPath); |
||
522 | } |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Assert that value matches a regex |
||
527 | * |
||
528 | * @param mixed $value |
||
529 | * @param string $pattern |
||
530 | * @param string|null $message |
||
531 | * @param string|null $propertyPath |
||
532 | * @return void |
||
533 | * @throws \Assert\AssertionFailedException |
||
534 | */ |
||
535 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
536 | { |
||
537 | static::string($value, $message, $propertyPath); |
||
538 | |||
539 | if ( ! preg_match($pattern, $value)) { |
||
540 | $message = sprintf( |
||
541 | $message ?: 'Value "%s" does not match expression.', |
||
542 | self::stringify($value) |
||
543 | ); |
||
544 | |||
545 | throw static::createException($value, $message, static::INVALID_REGEX , $propertyPath, array('pattern' => $pattern)); |
||
546 | } |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Assert that string has a given length. |
||
551 | * |
||
552 | * @param mixed $value |
||
553 | * @param int $length |
||
554 | * @param string|null $message |
||
555 | * @param string|null $propertyPath |
||
556 | * @param string $encoding |
||
557 | * @return void |
||
558 | * @throws \Assert\AssertionFailedException |
||
559 | */ |
||
560 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
561 | { |
||
562 | static::string($value, $message, $propertyPath); |
||
563 | |||
564 | if (mb_strlen($value, $encoding) !== $length) { |
||
565 | $message = sprintf( |
||
566 | $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.', |
||
567 | self::stringify($value), |
||
568 | $length, |
||
569 | mb_strlen($value, $encoding) |
||
570 | ); |
||
571 | |||
572 | $constraints = array('length' => $length, 'encoding' => $encoding); |
||
573 | throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints); |
||
574 | } |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Assert that a string is at least $minLength chars long. |
||
579 | * |
||
580 | * @param mixed $value |
||
581 | * @param int $minLength |
||
582 | * @param string|null $message |
||
583 | * @param string|null $propertyPath |
||
584 | * @param string $encoding |
||
585 | * @return void |
||
586 | * @throws \Assert\AssertionFailedException |
||
587 | */ |
||
588 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
589 | { |
||
590 | static::string($value, $message, $propertyPath); |
||
591 | |||
592 | if (mb_strlen($value, $encoding) < $minLength) { |
||
593 | $message = sprintf( |
||
594 | $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.', |
||
595 | self::stringify($value), |
||
596 | $minLength, |
||
597 | mb_strlen($value, $encoding) |
||
598 | ); |
||
599 | |||
600 | $constraints = array('min_length' => $minLength, 'encoding' => $encoding); |
||
601 | throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints); |
||
602 | } |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * Assert that string value is not longer than $maxLength chars. |
||
607 | * |
||
608 | * @param mixed $value |
||
609 | * @param integer $maxLength |
||
610 | * @param string|null $message |
||
611 | * @param string|null $propertyPath |
||
612 | * @param string $encoding |
||
613 | * @return void |
||
614 | * @throws \Assert\AssertionFailedException |
||
615 | */ |
||
616 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
617 | { |
||
618 | static::string($value, $message, $propertyPath); |
||
619 | |||
620 | if (mb_strlen($value, $encoding) > $maxLength) { |
||
621 | $message = sprintf( |
||
622 | $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.', |
||
623 | self::stringify($value), |
||
624 | $maxLength, |
||
625 | mb_strlen($value, $encoding) |
||
626 | ); |
||
627 | |||
628 | $constraints = array('max_length' => $maxLength, 'encoding' => $encoding); |
||
629 | throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints); |
||
630 | } |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * Assert that string length is between min,max lengths. |
||
635 | * |
||
636 | * @param mixed $value |
||
637 | * @param integer $minLength |
||
638 | * @param integer $maxLength |
||
639 | * @param string|null $message |
||
640 | * @param string|null $propertyPath |
||
641 | * @param string $encoding |
||
642 | * @return void |
||
643 | * @throws \Assert\AssertionFailedException |
||
644 | */ |
||
645 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
646 | { |
||
647 | static::string($value, $message, $propertyPath); |
||
648 | |||
649 | if (mb_strlen($value, $encoding) < $minLength) { |
||
650 | $message = sprintf( |
||
651 | $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.', |
||
652 | self::stringify($value), |
||
653 | $minLength, |
||
654 | mb_strlen($value, $encoding) |
||
655 | ); |
||
656 | |||
657 | $constraints = array('min_length' => $minLength, 'encoding' => $encoding); |
||
658 | throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints); |
||
659 | } |
||
660 | |||
661 | if (mb_strlen($value, $encoding) > $maxLength) { |
||
662 | $message = sprintf( |
||
663 | $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.', |
||
664 | self::stringify($value), |
||
665 | $maxLength, |
||
666 | mb_strlen($value, $encoding) |
||
667 | ); |
||
668 | |||
669 | $constraints = array('max_length' => $maxLength, 'encoding' => $encoding); |
||
670 | throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints); |
||
671 | } |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Assert that string starts with a sequence of chars. |
||
676 | * |
||
677 | * @param mixed $string |
||
678 | * @param string $needle |
||
679 | * @param string|null $message |
||
680 | * @param string|null $propertyPath |
||
681 | * @param string $encoding |
||
682 | * @return void |
||
683 | * @throws \Assert\AssertionFailedException |
||
684 | */ |
||
685 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
686 | { |
||
687 | static::string($string, $message, $propertyPath); |
||
688 | |||
689 | if (mb_strpos($string, $needle, null, $encoding) !== 0) { |
||
690 | $message = sprintf( |
||
691 | $message ?: 'Value "%s" does not start with "%s".', |
||
692 | self::stringify($string), |
||
693 | self::stringify($needle) |
||
694 | ); |
||
695 | |||
696 | $constraints = array('needle' => $needle, 'encoding' => $encoding); |
||
697 | throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints); |
||
698 | } |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * Assert that string ends with a sequence of chars. |
||
703 | * |
||
704 | * @param mixed $string |
||
705 | * @param string $needle |
||
706 | * @param string|null $message |
||
707 | * @param string|null $propertyPath |
||
708 | * @param string $encoding |
||
709 | * @return void |
||
710 | * @throws \Assert\AssertionFailedException |
||
711 | */ |
||
712 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
713 | { |
||
714 | static::string($string, $message, $propertyPath); |
||
715 | |||
716 | $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding); |
||
717 | |||
718 | if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) { |
||
719 | $message = sprintf( |
||
720 | $message ?: 'Value "%s" does not end with "%s".', |
||
721 | self::stringify($string), |
||
722 | self::stringify($needle) |
||
723 | ); |
||
724 | |||
725 | $constraints = array('needle' => $needle, 'encoding' => $encoding); |
||
726 | throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints); |
||
727 | } |
||
728 | } |
||
729 | |||
730 | /** |
||
731 | * Assert that string contains a sequence of chars. |
||
732 | * |
||
733 | * @param mixed $string |
||
734 | * @param string $needle |
||
735 | * @param string|null $message |
||
736 | * @param string|null $propertyPath |
||
737 | * @param string $encoding |
||
738 | * @return void |
||
739 | * @throws \Assert\AssertionFailedException |
||
740 | */ |
||
741 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
742 | { |
||
743 | static::string($string, $message, $propertyPath); |
||
744 | |||
745 | if (mb_strpos($string, $needle, null, $encoding) === false) { |
||
746 | $message = sprintf( |
||
747 | $message ?: 'Value "%s" does not contain "%s".', |
||
748 | self::stringify($string), |
||
749 | self::stringify($needle) |
||
750 | ); |
||
751 | |||
752 | $constraints = array('needle' => $needle, 'encoding' => $encoding); |
||
753 | throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints); |
||
754 | } |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * Assert that value is in array of choices. |
||
759 | * |
||
760 | * @param mixed $value |
||
761 | * @param array $choices |
||
762 | * @param string|null $message |
||
763 | * @param string|null $propertyPath |
||
764 | * @return void |
||
765 | * @throws \Assert\AssertionFailedException |
||
766 | */ |
||
767 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
768 | { |
||
769 | if ( ! in_array($value, $choices, true)) { |
||
770 | $message = sprintf( |
||
771 | $message ?: 'Value "%s" is not an element of the valid values: %s', |
||
772 | self::stringify($value), |
||
773 | implode(", ", array_map('Assert\Assertion::stringify', $choices)) |
||
774 | ); |
||
775 | |||
776 | throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices)); |
||
777 | } |
||
778 | } |
||
779 | |||
780 | /** |
||
781 | * Alias of {@see choice()} |
||
782 | * |
||
783 | * @throws \Assert\AssertionFailedException |
||
784 | */ |
||
785 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
786 | { |
||
787 | static::choice($value, $choices, $message, $propertyPath); |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * Assert that value is numeric. |
||
792 | * |
||
793 | * @param mixed $value |
||
794 | * @param string|null $message |
||
795 | * @param string|null $propertyPath |
||
796 | * @return void |
||
797 | * @throws \Assert\AssertionFailedException |
||
798 | */ |
||
799 | public static function numeric($value, $message = null, $propertyPath = null) |
||
800 | { |
||
801 | if ( ! is_numeric($value)) { |
||
802 | $message = sprintf( |
||
803 | $message ?: 'Value "%s" is not numeric.', |
||
804 | self::stringify($value) |
||
805 | ); |
||
806 | |||
807 | throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath); |
||
808 | } |
||
809 | } |
||
810 | |||
811 | /** |
||
812 | * Assert that value is an array. |
||
813 | * |
||
814 | * @param mixed $value |
||
815 | * @param string|null $message |
||
816 | * @param string|null $propertyPath |
||
817 | * @return void |
||
818 | * @throws \Assert\AssertionFailedException |
||
819 | */ |
||
820 | public static function isArray($value, $message = null, $propertyPath = null) |
||
821 | { |
||
822 | if ( ! is_array($value)) { |
||
823 | $message = sprintf( |
||
824 | $message ?: 'Value "%s" is not an array.', |
||
825 | self::stringify($value) |
||
826 | ); |
||
827 | |||
828 | throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath); |
||
829 | } |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * Assert that value is an array or a traversable object. |
||
834 | * |
||
835 | * @param mixed $value |
||
836 | * @param string|null $message |
||
837 | * @param string|null $propertyPath |
||
838 | * @return void |
||
839 | * @throws \Assert\AssertionFailedException |
||
840 | */ |
||
841 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
842 | { |
||
843 | if ( ! is_array($value) && ! $value instanceof \Traversable) { |
||
844 | $message = sprintf( |
||
845 | $message ?: 'Value "%s" is not an array and does not implement Traversable.', |
||
846 | self::stringify($value) |
||
847 | ); |
||
848 | |||
849 | throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath); |
||
850 | } |
||
851 | } |
||
852 | |||
853 | /** |
||
854 | * Assert that value is an array or an array-accessible object. |
||
855 | * |
||
856 | * @param mixed $value |
||
857 | * @param string|null $message |
||
858 | * @param string|null $propertyPath |
||
859 | * @return void |
||
860 | * @throws \Assert\AssertionFailedException |
||
861 | */ |
||
862 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
863 | { |
||
864 | if ( ! is_array($value) && ! $value instanceof \ArrayAccess) { |
||
865 | $message = sprintf( |
||
866 | $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.', |
||
867 | self::stringify($value) |
||
868 | ); |
||
869 | |||
870 | throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath); |
||
871 | } |
||
872 | } |
||
873 | |||
874 | /** |
||
875 | * Assert that key exists in an array |
||
876 | * |
||
877 | * @param mixed $value |
||
878 | * @param string|integer $key |
||
879 | * @param string|null $message |
||
880 | * @param string|null $propertyPath |
||
881 | * @return void |
||
882 | * @throws \Assert\AssertionFailedException |
||
883 | */ |
||
884 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
885 | { |
||
886 | static::isArray($value, $message, $propertyPath); |
||
887 | |||
888 | if ( ! array_key_exists($key, $value)) { |
||
889 | $message = sprintf( |
||
890 | $message ?: 'Array does not contain an element with key "%s"', |
||
891 | self::stringify($key) |
||
892 | ); |
||
893 | |||
894 | throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key)); |
||
895 | } |
||
896 | } |
||
897 | |||
898 | /** |
||
899 | * Assert that key exists in an array/array-accessible object using isset() |
||
900 | * |
||
901 | * @param mixed $value |
||
902 | * @param string|integer $key |
||
903 | * @param string|null $message |
||
904 | * @param string|null $propertyPath |
||
905 | * @return void |
||
906 | * @throws \Assert\AssertionFailedException |
||
907 | */ |
||
908 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
909 | { |
||
910 | static::isArrayAccessible($value, $message, $propertyPath); |
||
911 | |||
912 | if ( ! isset($value[$key])) { |
||
913 | $message = sprintf( |
||
914 | $message ?: 'The element with key "%s" was not found', |
||
915 | self::stringify($key) |
||
916 | ); |
||
917 | |||
918 | throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key)); |
||
919 | } |
||
920 | } |
||
921 | |||
922 | /** |
||
923 | * Assert that key exists in an array/array-accessible object and it's value is not empty. |
||
924 | * |
||
925 | * @param mixed $value |
||
926 | * @param string|integer $key |
||
927 | * @param string|null $message |
||
928 | * @param string|null $propertyPath |
||
929 | * @return void |
||
930 | * @throws \Assert\AssertionFailedException |
||
931 | */ |
||
932 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
933 | { |
||
934 | static::keyIsset($value, $key, $message, $propertyPath); |
||
935 | static::notEmpty($value[$key], $message, $propertyPath); |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * Assert that value is not blank |
||
940 | * |
||
941 | * @param mixed $value |
||
942 | * @param string|null $message |
||
943 | * @param string|null $propertyPath |
||
944 | * @return void |
||
945 | * @throws \Assert\AssertionFailedException |
||
946 | */ |
||
947 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
948 | { |
||
949 | if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) { |
||
950 | $message = sprintf( |
||
951 | $message ?: 'Value "%s" is blank, but was expected to contain a value.', |
||
952 | self::stringify($value) |
||
953 | ); |
||
954 | |||
955 | throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath); |
||
956 | } |
||
957 | } |
||
958 | |||
959 | /** |
||
960 | * Assert that value is instance of given class-name. |
||
961 | * |
||
962 | * @param mixed $value |
||
963 | * @param string $className |
||
964 | * @param string|null $message |
||
965 | * @param string|null $propertyPath |
||
966 | * @return void |
||
967 | * @throws \Assert\AssertionFailedException |
||
968 | */ |
||
969 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
970 | { |
||
971 | if ( ! ($value instanceof $className)) { |
||
972 | $message = sprintf( |
||
973 | $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.', |
||
974 | self::stringify($value), |
||
975 | $className |
||
976 | ); |
||
977 | |||
978 | throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className)); |
||
979 | } |
||
980 | } |
||
981 | |||
982 | /** |
||
983 | * Assert that value is not instance of given class-name. |
||
984 | * |
||
985 | * @param mixed $value |
||
986 | * @param string $className |
||
987 | * @param string|null $message |
||
988 | * @param string|null $propertyPath |
||
989 | * @return void |
||
990 | * @throws \Assert\AssertionFailedException |
||
991 | */ |
||
992 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
993 | { |
||
994 | if ($value instanceof $className) { |
||
995 | $message = sprintf( |
||
996 | $message ?: 'Class "%s" was not expected to be instanceof of "%s".', |
||
997 | self::stringify($value), |
||
998 | $className |
||
999 | ); |
||
1000 | |||
1001 | throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className)); |
||
1002 | } |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * Assert that value is subclass of given class-name. |
||
1007 | * |
||
1008 | * @param mixed $value |
||
1009 | * @param string $className |
||
1010 | * @param string|null $message |
||
1011 | * @param string|null $propertyPath |
||
1012 | * @return void |
||
1013 | * @throws \Assert\AssertionFailedException |
||
1014 | */ |
||
1015 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
1016 | { |
||
1017 | if ( ! is_subclass_of($value, $className)) { |
||
1018 | $message = sprintf( |
||
1019 | $message ?: 'Class "%s" was expected to be subclass of "%s".', |
||
1020 | self::stringify($value), |
||
1021 | $className |
||
1022 | ); |
||
1023 | |||
1024 | throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className)); |
||
1025 | } |
||
1026 | } |
||
1027 | |||
1028 | /** |
||
1029 | * Assert that value is in range of numbers. |
||
1030 | * |
||
1031 | * @param mixed $value |
||
1032 | * @param integer $minValue |
||
1033 | * @param integer $maxValue |
||
1034 | * @param string|null $message |
||
1035 | * @param string|null $propertyPath |
||
1036 | * @return void |
||
1037 | * @throws \Assert\AssertionFailedException |
||
1038 | */ |
||
1039 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
1040 | { |
||
1041 | static::numeric($value, $message, $propertyPath); |
||
1042 | |||
1043 | if ($value < $minValue || $value > $maxValue) { |
||
1044 | $message = sprintf( |
||
1045 | $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".', |
||
1046 | self::stringify($value), |
||
1047 | self::stringify($minValue), |
||
1048 | self::stringify($maxValue) |
||
1049 | ); |
||
1050 | |||
1051 | throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue)); |
||
1052 | } |
||
1053 | } |
||
1054 | |||
1055 | /** |
||
1056 | * Assert that a value is at least as big as a given limit |
||
1057 | * |
||
1058 | * @param mixed $value |
||
1059 | * @param mixed $minValue |
||
1060 | * @param string|null $message |
||
1061 | * @param string|null $propertyPath |
||
1062 | * @return void |
||
1063 | * @throws \Assert\AssertionFailedException |
||
1064 | */ |
||
1065 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
1066 | { |
||
1067 | static::numeric($value, $message, $propertyPath); |
||
1068 | |||
1069 | if ($value < $minValue) { |
||
1070 | $message = sprintf( |
||
1071 | $message ?: 'Number "%s" was expected to be at least "%d".', |
||
1072 | self::stringify($value), |
||
1073 | self::stringify($minValue) |
||
1074 | ); |
||
1075 | |||
1076 | throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue)); |
||
1077 | } |
||
1078 | } |
||
1079 | |||
1080 | /** |
||
1081 | * Assert that a number is smaller as a given limit |
||
1082 | * |
||
1083 | * @param mixed $value |
||
1084 | * @param mixed $maxValue |
||
1085 | * @param string|null $message |
||
1086 | * @param string|null $propertyPath |
||
1087 | * @return void |
||
1088 | * @throws \Assert\AssertionFailedException |
||
1089 | */ |
||
1090 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
1091 | { |
||
1092 | static::numeric($value, $message, $propertyPath); |
||
1093 | |||
1094 | if ($value > $maxValue) { |
||
1095 | $message = sprintf( |
||
1096 | $message ?: 'Number "%s" was expected to be at most "%d".', |
||
1097 | self::stringify($value), |
||
1098 | self::stringify($maxValue) |
||
1099 | ); |
||
1100 | |||
1101 | throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue)); |
||
1102 | } |
||
1103 | } |
||
1104 | |||
1105 | /** |
||
1106 | * Assert that a file exists |
||
1107 | * |
||
1108 | * @param string $value |
||
1109 | * @param string|null $message |
||
1110 | * @param string|null $propertyPath |
||
1111 | * @return void |
||
1112 | * @throws \Assert\AssertionFailedException |
||
1113 | */ |
||
1114 | public static function file($value, $message = null, $propertyPath = null) |
||
1115 | { |
||
1116 | static::string($value, $message, $propertyPath); |
||
1117 | static::notEmpty($value, $message, $propertyPath); |
||
1118 | |||
1119 | if ( ! is_file($value)) { |
||
1120 | $message = sprintf( |
||
1121 | $message ?: 'File "%s" was expected to exist.', |
||
1122 | self::stringify($value) |
||
1123 | ); |
||
1124 | |||
1125 | throw static::createException($value, $message, static::INVALID_FILE, $propertyPath); |
||
1126 | } |
||
1127 | } |
||
1128 | |||
1129 | /** |
||
1130 | * Assert that a directory exists |
||
1131 | * |
||
1132 | * @param string $value |
||
1133 | * @param string|null $message |
||
1134 | * @param string|null $propertyPath |
||
1135 | * @return void |
||
1136 | * @throws \Assert\AssertionFailedException |
||
1137 | */ |
||
1138 | public static function directory($value, $message = null, $propertyPath = null) |
||
1139 | { |
||
1140 | static::string($value, $message, $propertyPath); |
||
1141 | |||
1142 | if ( ! is_dir($value)) { |
||
1143 | $message = sprintf( |
||
1144 | $message ?: 'Path "%s" was expected to be a directory.', |
||
1145 | self::stringify($value) |
||
1146 | ); |
||
1147 | |||
1148 | throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath); |
||
1149 | } |
||
1150 | } |
||
1151 | |||
1152 | /** |
||
1153 | * Assert that the value is something readable |
||
1154 | * |
||
1155 | * @param string $value |
||
1156 | * @param string|null $message |
||
1157 | * @param string|null $propertyPath |
||
1158 | * @return void |
||
1159 | * @throws \Assert\AssertionFailedException |
||
1160 | */ |
||
1161 | public static function readable($value, $message = null, $propertyPath = null) |
||
1162 | { |
||
1163 | static::string($value, $message, $propertyPath); |
||
1164 | |||
1165 | if ( ! is_readable($value)) { |
||
1166 | $message = sprintf( |
||
1167 | $message ?: 'Path "%s" was expected to be readable.', |
||
1168 | self::stringify($value) |
||
1169 | ); |
||
1170 | |||
1171 | throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath); |
||
1172 | } |
||
1173 | } |
||
1174 | |||
1175 | /** |
||
1176 | * Assert that the value is something writeable |
||
1177 | * |
||
1178 | * @param string $value |
||
1179 | * @param string|null $message |
||
1180 | * @param string|null $propertyPath |
||
1181 | * @return void |
||
1182 | * @throws \Assert\AssertionFailedException |
||
1183 | */ |
||
1184 | public static function writeable($value, $message = null, $propertyPath = null) |
||
1185 | { |
||
1186 | static::string($value, $message, $propertyPath); |
||
1187 | |||
1188 | if ( ! is_writeable($value)) { |
||
1189 | $message = sprintf( |
||
1190 | $message ?: 'Path "%s" was expected to be writeable.', |
||
1191 | self::stringify($value) |
||
1192 | ); |
||
1193 | |||
1194 | throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath); |
||
1195 | } |
||
1196 | } |
||
1197 | |||
1198 | /** |
||
1199 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
1200 | * |
||
1201 | * @param mixed $value |
||
1202 | * @param string|null $message |
||
1203 | * @param string|null $propertyPath |
||
1204 | * @return void |
||
1205 | * @throws \Assert\AssertionFailedException |
||
1206 | */ |
||
1207 | public static function email($value, $message = null, $propertyPath = null) |
||
1232 | |||
1233 | /** |
||
1234 | * Assert that value is an URL. |
||
1235 | * |
||
1236 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
1237 | * |
||
1238 | * @param mixed $value |
||
1239 | * @param string|null $message |
||
1240 | * @param string|null $propertyPath |
||
1241 | * @return void |
||
1242 | * @throws \Assert\AssertionFailedException |
||
1243 | * |
||
1244 | * |
||
1245 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
1246 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
1247 | */ |
||
1248 | public static function url($value, $message = null, $propertyPath = null) |
||
1281 | |||
1282 | /** |
||
1283 | * Assert that value is alphanumeric. |
||
1284 | * |
||
1285 | * @param mixed $value |
||
1286 | * @param string|null $message |
||
1287 | * @param string|null $propertyPath |
||
1288 | * @return void |
||
1289 | * @throws \Assert\AssertionFailedException |
||
1290 | */ |
||
1291 | public static function alnum($value, $message = null, $propertyPath = null) |
||
1304 | |||
1305 | /** |
||
1306 | * Assert that the value is boolean True. |
||
1307 | * |
||
1308 | * @param mixed $value |
||
1309 | * @param string|null $message |
||
1310 | * @param string|null $propertyPath |
||
1311 | * @return void |
||
1312 | * @throws \Assert\AssertionFailedException |
||
1313 | */ |
||
1314 | public static function true($value, $message = null, $propertyPath = null) |
||
1325 | |||
1326 | /** |
||
1327 | * Assert that the value is boolean False. |
||
1328 | * |
||
1329 | * @param mixed $value |
||
1330 | * @param string|null $message |
||
1331 | * @param string|null $propertyPath |
||
1332 | * @return void |
||
1333 | * @throws \Assert\AssertionFailedException |
||
1334 | */ |
||
1335 | public static function false($value, $message = null, $propertyPath = null) |
||
1346 | |||
1347 | /** |
||
1348 | * Assert that the class exists. |
||
1349 | * |
||
1350 | * @param mixed $value |
||
1351 | * @param string|null $message |
||
1352 | * @param string|null $propertyPath |
||
1353 | * @return void |
||
1354 | * @throws \Assert\AssertionFailedException |
||
1355 | */ |
||
1356 | public static function classExists($value, $message = null, $propertyPath = null) |
||
1367 | |||
1368 | /** |
||
1369 | * Assert that the class implements the interface |
||
1370 | * |
||
1371 | * @param mixed $class |
||
1372 | * @param string $interfaceName |
||
1373 | * @param string|null $message |
||
1374 | * @param string|null $propertyPath |
||
1375 | * @return void |
||
1376 | * @throws \Assert\AssertionFailedException |
||
1377 | */ |
||
1378 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
1391 | |||
1392 | /** |
||
1393 | * Assert that the given string is a valid json string. |
||
1394 | * |
||
1395 | * NOTICE: |
||
1396 | * Since this does a json_decode to determine its validity |
||
1397 | * you probably should consider, when using the variable |
||
1398 | * content afterwards, just to decode and check for yourself instead |
||
1399 | * of using this assertion. |
||
1400 | * |
||
1401 | * @param mixed $value |
||
1402 | * @param string|null $message |
||
1403 | * @param string|null $propertyPath |
||
1404 | * @return void |
||
1405 | * @throws \Assert\AssertionFailedException |
||
1406 | */ |
||
1407 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
1418 | |||
1419 | /** |
||
1420 | * Assert that the given string is a valid UUID |
||
1421 | * |
||
1422 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
1423 | * |
||
1424 | * @param string $value |
||
1425 | * @param string|null $message |
||
1426 | * @param string|null $propertyPath |
||
1427 | * @return void |
||
1428 | * @throws \Assert\AssertionFailedException |
||
1429 | */ |
||
1430 | public static function uuid($value, $message = null, $propertyPath = null) |
||
1447 | |||
1448 | /** |
||
1449 | * Assert that the count of countable is equal to count. |
||
1450 | * |
||
1451 | * @param array|\Countable $countable |
||
1452 | * @param int $count |
||
1453 | * @param string $message |
||
1454 | * @param string $propertyPath |
||
1455 | * @return void |
||
1456 | * @throws \Assert\AssertionFailedException |
||
1457 | */ |
||
1458 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
1469 | |||
1470 | /** |
||
1471 | * static call handler to implement: |
||
1472 | * - "null or assertion" delegation |
||
1473 | * - "all" delegation |
||
1474 | */ |
||
1475 | public static function __callStatic($method, $args) |
||
1511 | |||
1512 | /** |
||
1513 | * Determines if the values array has every choice as key and that this choice has content. |
||
1514 | * |
||
1515 | * @param array $values |
||
1516 | * @param array $choices |
||
1517 | * @param null $message |
||
1518 | * @param null $propertyPath |
||
1519 | */ |
||
1520 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
1529 | |||
1530 | /** |
||
1531 | * Determines that the named method is defined in the provided object. |
||
1532 | * |
||
1533 | * @param string $value |
||
1534 | * @param mixed $object |
||
1535 | * @param null $message |
||
1536 | * @param null $propertyPath |
||
1537 | * |
||
1538 | * @throws |
||
1539 | */ |
||
1540 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
1553 | |||
1554 | /** |
||
1555 | * Determines that the provided value is an object. |
||
1556 | * |
||
1557 | * @param mixed $value |
||
1558 | * @param null $message |
||
1559 | * @param null $propertyPath |
||
1560 | */ |
||
1561 | public static function isObject($value, $message = null, $propertyPath = null) |
||
1573 | |||
1574 | /** |
||
1575 | * Determines if the value is less than given limit. |
||
1576 | * |
||
1577 | * @param mixed $value |
||
1578 | * @param mixed $limit |
||
1579 | * @param null $message |
||
1580 | * @param null $propertyPath |
||
1581 | */ |
||
1582 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
1594 | |||
1595 | /** |
||
1596 | * Determines if the value is less or than given limit. |
||
1597 | * |
||
1598 | * @param mixed $value |
||
1599 | * @param mixed $limit |
||
1600 | * @param null $message |
||
1601 | * @param null $propertyPath |
||
1602 | */ |
||
1603 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1615 | |||
1616 | /** |
||
1617 | * Determines if the value is greater than given limit. |
||
1618 | * |
||
1619 | * @param mixed $value |
||
1620 | * @param mixed $limit |
||
1621 | * @param null $message |
||
1622 | * @param null $propertyPath |
||
1623 | */ |
||
1624 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
1636 | |||
1637 | /** |
||
1638 | * Determines if the value is greater or equal than given limit. |
||
1639 | * |
||
1640 | * @param mixed $value |
||
1641 | * @param mixed $limit |
||
1642 | * @param null $message |
||
1643 | * @param null $propertyPath |
||
1644 | */ |
||
1645 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
1657 | |||
1658 | /** |
||
1659 | * Assert that date is valid and corresponds to the given format. |
||
1660 | * |
||
1661 | * @param string $value |
||
1662 | * @param string $format supports all of the options date(), except for the following: |
||
1663 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
1664 | * @param string|null $message |
||
1665 | * @param string|null $propertyPath |
||
1666 | * |
||
1667 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
1668 | */ |
||
1669 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
1686 | |||
1687 | /** |
||
1688 | * Determines that the provided value is callable. |
||
1689 | * |
||
1690 | * @param mixed $value |
||
1691 | * @param null $message |
||
1692 | * @param null $propertyPath |
||
1693 | */ |
||
1694 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
1695 | { |
||
1696 | if (!is_callable($value)) { |
||
1697 | $message = sprintf( |
||
1698 | $message ?: 'Provided "%s" is not a callable.', |
||
1699 | self::stringify($value) |
||
1700 | ); |
||
1701 | |||
1702 | throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath); |
||
1703 | } |
||
1704 | } |
||
1705 | |||
1706 | /** |
||
1707 | * Make a string version of a value. |
||
1708 | * |
||
1709 | * @param mixed $value |
||
1710 | * @return string |
||
1711 | */ |
||
1712 | protected static function stringify($value) |
||
1713 | { |
||
1714 | if (is_bool($value)) { |
||
1715 | return $value ? '<TRUE>' : '<FALSE>'; |
||
1716 | } |
||
1717 | |||
1718 | if (is_scalar($value)) { |
||
1719 | $val = (string)$value; |
||
1720 | |||
1721 | if (strlen($val) > 100) { |
||
1722 | $val = substr($val, 0, 97) . '...'; |
||
1723 | } |
||
1724 | |||
1725 | return $val; |
||
1726 | } |
||
1727 | |||
1728 | if (is_array($value)) { |
||
1729 | return '<ARRAY>'; |
||
1730 | } |
||
1746 | } |
||
1747 | |||
1748 |