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 |
||
| 173 | class Assertion |
||
| 174 | { |
||
| 175 | const INVALID_FLOAT = 9; |
||
| 176 | const INVALID_INTEGER = 10; |
||
| 177 | const INVALID_DIGIT = 11; |
||
| 178 | const INVALID_INTEGERISH = 12; |
||
| 179 | const INVALID_BOOLEAN = 13; |
||
| 180 | const VALUE_EMPTY = 14; |
||
| 181 | const VALUE_NULL = 15; |
||
| 182 | const VALUE_NOT_NULL = 25; |
||
| 183 | const INVALID_STRING = 16; |
||
| 184 | const INVALID_REGEX = 17; |
||
| 185 | const INVALID_MIN_LENGTH = 18; |
||
| 186 | const INVALID_MAX_LENGTH = 19; |
||
| 187 | const INVALID_STRING_START = 20; |
||
| 188 | const INVALID_STRING_CONTAINS = 21; |
||
| 189 | const INVALID_CHOICE = 22; |
||
| 190 | const INVALID_NUMERIC = 23; |
||
| 191 | const INVALID_ARRAY = 24; |
||
| 192 | const INVALID_KEY_EXISTS = 26; |
||
| 193 | const INVALID_NOT_BLANK = 27; |
||
| 194 | const INVALID_INSTANCE_OF = 28; |
||
| 195 | const INVALID_SUBCLASS_OF = 29; |
||
| 196 | const INVALID_RANGE = 30; |
||
| 197 | const INVALID_ALNUM = 31; |
||
| 198 | const INVALID_TRUE = 32; |
||
| 199 | const INVALID_EQ = 33; |
||
| 200 | const INVALID_SAME = 34; |
||
| 201 | const INVALID_MIN = 35; |
||
| 202 | const INVALID_MAX = 36; |
||
| 203 | const INVALID_LENGTH = 37; |
||
| 204 | const INVALID_FALSE = 38; |
||
| 205 | const INVALID_STRING_END = 39; |
||
| 206 | const INVALID_UUID = 40; |
||
| 207 | const INVALID_COUNT = 41; |
||
| 208 | const INVALID_NOT_EQ = 42; |
||
| 209 | const INVALID_NOT_SAME = 43; |
||
| 210 | const INVALID_TRAVERSABLE = 44; |
||
| 211 | const INVALID_ARRAY_ACCESSIBLE = 45; |
||
| 212 | const INVALID_KEY_ISSET = 46; |
||
| 213 | const INVALID_VALUE_IN_ARRAY = 47; |
||
| 214 | const INVALID_E164 = 48; |
||
| 215 | const INVALID_DIRECTORY = 101; |
||
| 216 | const INVALID_FILE = 102; |
||
| 217 | const INVALID_READABLE = 103; |
||
| 218 | const INVALID_WRITEABLE = 104; |
||
| 219 | const INVALID_CLASS = 105; |
||
| 220 | const INVALID_INTERFACE = 106; |
||
| 221 | const INVALID_EMAIL = 201; |
||
| 222 | const INTERFACE_NOT_IMPLEMENTED = 202; |
||
| 223 | const INVALID_URL = 203; |
||
| 224 | const INVALID_NOT_INSTANCE_OF = 204; |
||
| 225 | const VALUE_NOT_EMPTY = 205; |
||
| 226 | const INVALID_JSON_STRING = 206; |
||
| 227 | const INVALID_OBJECT = 207; |
||
| 228 | const INVALID_METHOD = 208; |
||
| 229 | const INVALID_SCALAR = 209; |
||
| 230 | const INVALID_LESS = 210; |
||
| 231 | const INVALID_LESS_OR_EQUAL = 211; |
||
| 232 | const INVALID_GREATER = 212; |
||
| 233 | const INVALID_GREATER_OR_EQUAL = 213; |
||
| 234 | const INVALID_DATE = 214; |
||
| 235 | const INVALID_CALLABLE = 215; |
||
| 236 | const INVALID_KEY_NOT_EXISTS = 216; |
||
| 237 | const INVALID_SATISFY = 217; |
||
| 238 | const INVALID_IP = 218; |
||
| 239 | const INVALID_BETWEEN = 219; |
||
| 240 | const INVALID_BETWEEN_EXCLUSIVE = 220; |
||
| 241 | const INVALID_EXTENSION = 222; |
||
| 242 | const INVALID_CONSTANT = 221; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Exception to throw when an assertion failed. |
||
| 246 | * |
||
| 247 | * @var string |
||
| 248 | */ |
||
| 249 | protected static $exceptionClass = 'Assert\InvalidArgumentException'; |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Helper method that handles building the assertion failure exceptions. |
||
| 253 | * They are returned from this method so that the stack trace still shows |
||
| 254 | * the assertions method. |
||
| 255 | * |
||
| 256 | * @param mixed $value |
||
| 257 | * @param string $message |
||
| 258 | * @param int $code |
||
| 259 | * @param string|null $propertyPath |
||
| 260 | * @param array $constraints |
||
| 261 | * |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | protected static function createException($value, $message, $code, $propertyPath, array $constraints = array()) |
||
| 265 | { |
||
| 266 | $exceptionClass = static::$exceptionClass; |
||
| 267 | return new $exceptionClass($message, $code, $propertyPath, $value, $constraints); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Assert that two values are equal (using == ). |
||
| 272 | * |
||
| 273 | * @param mixed $value |
||
| 274 | * @param mixed $value2 |
||
| 275 | * @param string|null $message |
||
| 276 | * @param string|null $propertyPath |
||
| 277 | * @return bool |
||
| 278 | * @throws \Assert\AssertionFailedException |
||
| 279 | */ |
||
| 280 | public static function eq($value, $value2, $message = null, $propertyPath = null) |
||
| 281 | { |
||
| 282 | if ($value != $value2) { |
||
| 283 | $message = sprintf( |
||
| 284 | $message ?: 'Value "%s" does not equal expected value "%s".', |
||
| 285 | static::stringify($value), |
||
| 286 | static::stringify($value2) |
||
| 287 | ); |
||
| 288 | |||
| 289 | throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2)); |
||
| 290 | } |
||
| 291 | |||
| 292 | return true; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Assert that two values are the same (using ===). |
||
| 297 | * |
||
| 298 | * @param mixed $value |
||
| 299 | * @param mixed $value2 |
||
| 300 | * @param string|null $message |
||
| 301 | * @param string|null $propertyPath |
||
| 302 | * @return bool |
||
| 303 | * @throws \Assert\AssertionFailedException |
||
| 304 | */ |
||
| 305 | public static function same($value, $value2, $message = null, $propertyPath = null) |
||
| 306 | { |
||
| 307 | if ($value !== $value2) { |
||
| 308 | $message = sprintf( |
||
| 309 | $message ?: 'Value "%s" is not the same as expected value "%s".', |
||
| 310 | static::stringify($value), |
||
| 311 | static::stringify($value2) |
||
| 312 | ); |
||
| 313 | |||
| 314 | throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2)); |
||
| 315 | } |
||
| 316 | |||
| 317 | return true; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Assert that two values are not equal (using == ). |
||
| 322 | * |
||
| 323 | * @param mixed $value1 |
||
| 324 | * @param mixed $value2 |
||
| 325 | * @param string|null $message |
||
| 326 | * @param string|null $propertyPath |
||
| 327 | * @return bool |
||
| 328 | * @throws \Assert\AssertionFailedException |
||
| 329 | */ |
||
| 330 | public static function notEq($value1, $value2, $message = null, $propertyPath = null) |
||
| 331 | { |
||
| 332 | if ($value1 == $value2) { |
||
| 333 | $message = sprintf( |
||
| 334 | $message ?: 'Value "%s" is equal to expected value "%s".', |
||
| 335 | static::stringify($value1), |
||
| 336 | static::stringify($value2) |
||
| 337 | ); |
||
| 338 | throw static::createException($value1, $message, static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2)); |
||
| 339 | } |
||
| 340 | |||
| 341 | return true; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Assert that two values are not the same (using === ). |
||
| 346 | * |
||
| 347 | * @param mixed $value1 |
||
| 348 | * @param mixed $value2 |
||
| 349 | * @param string|null $message |
||
| 350 | * @param string|null $propertyPath |
||
| 351 | * @return bool |
||
| 352 | * @throws \Assert\AssertionFailedException |
||
| 353 | */ |
||
| 354 | public static function notSame($value1, $value2, $message = null, $propertyPath = null) |
||
| 355 | { |
||
| 356 | if ($value1 === $value2) { |
||
| 357 | $message = sprintf( |
||
| 358 | $message ?: 'Value "%s" is the same as expected value "%s".', |
||
| 359 | static::stringify($value1), |
||
| 360 | static::stringify($value2) |
||
| 361 | ); |
||
| 362 | throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2)); |
||
| 363 | } |
||
| 364 | |||
| 365 | return true; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Assert that value is not in array of choices. |
||
| 370 | * |
||
| 371 | * @param mixed $value |
||
| 372 | * @param array $choices |
||
| 373 | * @param string|null $message |
||
| 374 | * @param string|null $propertyPath |
||
| 375 | * @return bool |
||
| 376 | * @throws \Assert\AssertionFailedException |
||
| 377 | */ |
||
| 378 | public static function notInArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 379 | { |
||
| 380 | if (in_array($value, $choices) === true) { |
||
| 381 | $message = sprintf( |
||
| 382 | $message ?: 'Value "%s" is in given "%s".', |
||
| 383 | static::stringify($value), |
||
| 384 | static::stringify($choices) |
||
| 385 | ); |
||
| 386 | throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath); |
||
| 387 | } |
||
| 388 | |||
| 389 | return true; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Assert that value is a php integer. |
||
| 394 | * |
||
| 395 | * @param mixed $value |
||
| 396 | * @param string|null $message |
||
| 397 | * @param string|null $propertyPath |
||
| 398 | * @return bool |
||
| 399 | * @throws \Assert\AssertionFailedException |
||
| 400 | */ |
||
| 401 | public static function integer($value, $message = null, $propertyPath = null) |
||
| 402 | { |
||
| 403 | if (! is_int($value)) { |
||
| 404 | $message = sprintf( |
||
| 405 | $message ?: 'Value "%s" is not an integer.', |
||
| 406 | static::stringify($value) |
||
| 407 | ); |
||
| 408 | |||
| 409 | throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath); |
||
| 410 | } |
||
| 411 | |||
| 412 | return true; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Assert that value is a php float. |
||
| 417 | * |
||
| 418 | * @param mixed $value |
||
| 419 | * @param string|null $message |
||
| 420 | * @param string|null $propertyPath |
||
| 421 | * @return bool |
||
| 422 | * @throws \Assert\AssertionFailedException |
||
| 423 | */ |
||
| 424 | public static function float($value, $message = null, $propertyPath = null) |
||
| 425 | { |
||
| 426 | if (! is_float($value)) { |
||
| 427 | $message = sprintf( |
||
| 428 | $message ?: 'Value "%s" is not a float.', |
||
| 429 | static::stringify($value) |
||
| 430 | ); |
||
| 431 | |||
| 432 | throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath); |
||
| 433 | } |
||
| 434 | |||
| 435 | return true; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Validates if an integer or integerish is a digit. |
||
| 440 | * |
||
| 441 | * @param mixed $value |
||
| 442 | * @param string|null $message |
||
| 443 | * @param string|null $propertyPath |
||
| 444 | * @return bool |
||
| 445 | * @throws \Assert\AssertionFailedException |
||
| 446 | */ |
||
| 447 | public static function digit($value, $message = null, $propertyPath = null) |
||
| 448 | { |
||
| 449 | if (! ctype_digit((string)$value)) { |
||
| 450 | $message = sprintf( |
||
| 451 | $message ?: 'Value "%s" is not a digit.', |
||
| 452 | static::stringify($value) |
||
| 453 | ); |
||
| 454 | |||
| 455 | throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath); |
||
| 456 | } |
||
| 457 | |||
| 458 | return true; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Assert that value is a php integer'ish. |
||
| 463 | * |
||
| 464 | * @param mixed $value |
||
| 465 | * @param string|null $message |
||
| 466 | * @param string|null $propertyPath |
||
| 467 | * @return bool |
||
| 468 | * @throws \Assert\AssertionFailedException |
||
| 469 | */ |
||
| 470 | public static function integerish($value, $message = null, $propertyPath = null) |
||
| 471 | { |
||
| 472 | if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) { |
||
| 473 | $message = sprintf( |
||
| 474 | $message ?: 'Value "%s" is not an integer or a number castable to integer.', |
||
| 475 | static::stringify($value) |
||
| 476 | ); |
||
| 477 | |||
| 478 | throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath); |
||
| 479 | } |
||
| 480 | |||
| 481 | return true; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Assert that value is php boolean |
||
| 486 | * |
||
| 487 | * @param mixed $value |
||
| 488 | * @param string|null $message |
||
| 489 | * @param string|null $propertyPath |
||
| 490 | * @return bool |
||
| 491 | * @throws \Assert\AssertionFailedException |
||
| 492 | */ |
||
| 493 | public static function boolean($value, $message = null, $propertyPath = null) |
||
| 494 | { |
||
| 495 | if (! is_bool($value)) { |
||
| 496 | $message = sprintf( |
||
| 497 | $message ?: 'Value "%s" is not a boolean.', |
||
| 498 | static::stringify($value) |
||
| 499 | ); |
||
| 500 | |||
| 501 | throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath); |
||
| 502 | } |
||
| 503 | |||
| 504 | return true; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Assert that value is a PHP scalar |
||
| 509 | * |
||
| 510 | * @param mixed $value |
||
| 511 | * @param string|null $message |
||
| 512 | * @param string|null $propertyPath |
||
| 513 | * @return bool |
||
| 514 | * @throws \Assert\AssertionFailedException |
||
| 515 | */ |
||
| 516 | public static function scalar($value, $message = null, $propertyPath = null) |
||
| 517 | { |
||
| 518 | if (!is_scalar($value)) { |
||
| 519 | $message = sprintf( |
||
| 520 | $message ?: 'Value "%s" is not a scalar.', |
||
| 521 | static::stringify($value) |
||
| 522 | ); |
||
| 523 | |||
| 524 | throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath); |
||
| 525 | } |
||
| 526 | |||
| 527 | return true; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Assert that value is not empty |
||
| 532 | * |
||
| 533 | * @param mixed $value |
||
| 534 | * @param string|null $message |
||
| 535 | * @param string|null $propertyPath |
||
| 536 | * @return bool |
||
| 537 | * @throws \Assert\AssertionFailedException |
||
| 538 | */ |
||
| 539 | public static function notEmpty($value, $message = null, $propertyPath = null) |
||
| 540 | { |
||
| 541 | if (empty($value)) { |
||
| 542 | $message = sprintf( |
||
| 543 | $message ?: 'Value "%s" is empty, but non empty value was expected.', |
||
| 544 | static::stringify($value) |
||
| 545 | ); |
||
| 546 | |||
| 547 | throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath); |
||
| 548 | } |
||
| 549 | |||
| 550 | return true; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Assert that value is empty |
||
| 555 | * |
||
| 556 | * @param mixed $value |
||
| 557 | * @param string|null $message |
||
| 558 | * @param string|null $propertyPath |
||
| 559 | * @return bool |
||
| 560 | * @throws \Assert\AssertionFailedException |
||
| 561 | */ |
||
| 562 | public static function noContent($value, $message = null, $propertyPath = null) |
||
| 563 | { |
||
| 564 | if (!empty($value)) { |
||
| 565 | $message = sprintf( |
||
| 566 | $message ?: 'Value "%s" is not empty, but empty value was expected.', |
||
| 567 | static::stringify($value) |
||
| 568 | ); |
||
| 569 | |||
| 570 | throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath); |
||
| 571 | } |
||
| 572 | |||
| 573 | return true; |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Assert that value is null |
||
| 578 | * |
||
| 579 | * @param mixed $value |
||
| 580 | * @param string|null $message |
||
| 581 | * @param string|null $propertyPath |
||
| 582 | * @return bool |
||
| 583 | * @throws \Assert\AssertionFailedException |
||
| 584 | */ |
||
| 585 | public static function null($value, $message = null, $propertyPath = null) |
||
| 586 | { |
||
| 587 | if ($value !== null) { |
||
| 588 | $message = sprintf( |
||
| 589 | $message ?: 'Value "%s" is not null, but null value was expected.', |
||
| 590 | static::stringify($value) |
||
| 591 | ); |
||
| 592 | |||
| 593 | throw static::createException($value, $message, static::VALUE_NOT_NULL, $propertyPath); |
||
| 594 | } |
||
| 595 | |||
| 596 | return true; |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Assert that value is not null |
||
| 601 | * |
||
| 602 | * @param mixed $value |
||
| 603 | * @param string|null $message |
||
| 604 | * @param string|null $propertyPath |
||
| 605 | * @return bool |
||
| 606 | * @throws \Assert\AssertionFailedException |
||
| 607 | */ |
||
| 608 | public static function notNull($value, $message = null, $propertyPath = null) |
||
| 609 | { |
||
| 610 | if ($value === null) { |
||
| 611 | $message = sprintf( |
||
| 612 | $message ?: 'Value "%s" is null, but non null value was expected.', |
||
| 613 | static::stringify($value) |
||
| 614 | ); |
||
| 615 | |||
| 616 | throw static::createException($value, $message, static::VALUE_NULL, $propertyPath); |
||
| 617 | } |
||
| 618 | |||
| 619 | return true; |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Assert that value is a string |
||
| 624 | * |
||
| 625 | * @param mixed $value |
||
| 626 | * @param string|null $message |
||
| 627 | * @param string|null $propertyPath |
||
| 628 | * @return bool |
||
| 629 | * @throws \Assert\AssertionFailedException |
||
| 630 | */ |
||
| 631 | public static function string($value, $message = null, $propertyPath = null) |
||
| 632 | { |
||
| 633 | if (! is_string($value)) { |
||
| 634 | $message = sprintf( |
||
| 635 | $message ?: 'Value "%s" expected to be string, type %s given.', |
||
| 636 | static::stringify($value), |
||
| 637 | gettype($value) |
||
| 638 | ); |
||
| 639 | |||
| 640 | throw static::createException($value, $message, static::INVALID_STRING, $propertyPath); |
||
| 641 | } |
||
| 642 | |||
| 643 | return true; |
||
| 644 | } |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Assert that value matches a regex |
||
| 648 | * |
||
| 649 | * @param mixed $value |
||
| 650 | * @param string $pattern |
||
| 651 | * @param string|null $message |
||
| 652 | * @param string|null $propertyPath |
||
| 653 | * @return bool |
||
| 654 | * @throws \Assert\AssertionFailedException |
||
| 655 | */ |
||
| 656 | public static function regex($value, $pattern, $message = null, $propertyPath = null) |
||
| 657 | { |
||
| 658 | static::string($value, $message, $propertyPath); |
||
| 659 | |||
| 660 | if (! preg_match($pattern, $value)) { |
||
| 661 | $message = sprintf( |
||
| 662 | $message ?: 'Value "%s" does not match expression.', |
||
| 663 | static::stringify($value) |
||
| 664 | ); |
||
| 665 | |||
| 666 | throw static::createException($value, $message, static::INVALID_REGEX, $propertyPath, array('pattern' => $pattern)); |
||
| 667 | } |
||
| 668 | |||
| 669 | return true; |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Assert that string has a given length. |
||
| 674 | * |
||
| 675 | * @param mixed $value |
||
| 676 | * @param int $length |
||
| 677 | * @param string|null $message |
||
| 678 | * @param string|null $propertyPath |
||
| 679 | * @param string $encoding |
||
| 680 | * @return bool |
||
| 681 | * @throws \Assert\AssertionFailedException |
||
| 682 | */ |
||
| 683 | public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 684 | { |
||
| 685 | static::string($value, $message, $propertyPath); |
||
| 686 | |||
| 687 | if (mb_strlen($value, $encoding) !== $length) { |
||
| 688 | $message = sprintf( |
||
| 689 | $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.', |
||
| 690 | static::stringify($value), |
||
| 691 | $length, |
||
| 692 | mb_strlen($value, $encoding) |
||
| 693 | ); |
||
| 694 | |||
| 695 | $constraints = array('length' => $length, 'encoding' => $encoding); |
||
| 696 | throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints); |
||
| 697 | } |
||
| 698 | |||
| 699 | return true; |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Assert that a string is at least $minLength chars long. |
||
| 704 | * |
||
| 705 | * @param mixed $value |
||
| 706 | * @param int $minLength |
||
| 707 | * @param string|null $message |
||
| 708 | * @param string|null $propertyPath |
||
| 709 | * @param string $encoding |
||
| 710 | * @return bool |
||
| 711 | * @throws \Assert\AssertionFailedException |
||
| 712 | */ |
||
| 713 | public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 714 | { |
||
| 715 | static::string($value, $message, $propertyPath); |
||
| 716 | |||
| 717 | if (mb_strlen($value, $encoding) < $minLength) { |
||
| 718 | $message = sprintf( |
||
| 719 | $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.', |
||
| 720 | static::stringify($value), |
||
| 721 | $minLength, |
||
| 722 | mb_strlen($value, $encoding) |
||
| 723 | ); |
||
| 724 | |||
| 725 | $constraints = array('min_length' => $minLength, 'encoding' => $encoding); |
||
| 726 | throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints); |
||
| 727 | } |
||
| 728 | |||
| 729 | return true; |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Assert that string value is not longer than $maxLength chars. |
||
| 734 | * |
||
| 735 | * @param mixed $value |
||
| 736 | * @param integer $maxLength |
||
| 737 | * @param string|null $message |
||
| 738 | * @param string|null $propertyPath |
||
| 739 | * @param string $encoding |
||
| 740 | * @return bool |
||
| 741 | * @throws \Assert\AssertionFailedException |
||
| 742 | */ |
||
| 743 | public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 744 | { |
||
| 745 | static::string($value, $message, $propertyPath); |
||
| 746 | |||
| 747 | if (mb_strlen($value, $encoding) > $maxLength) { |
||
| 748 | $message = sprintf( |
||
| 749 | $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.', |
||
| 750 | static::stringify($value), |
||
| 751 | $maxLength, |
||
| 752 | mb_strlen($value, $encoding) |
||
| 753 | ); |
||
| 754 | |||
| 755 | $constraints = array('max_length' => $maxLength, 'encoding' => $encoding); |
||
| 756 | throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints); |
||
| 757 | } |
||
| 758 | |||
| 759 | return true; |
||
| 760 | } |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Assert that string length is between min,max lengths. |
||
| 764 | * |
||
| 765 | * @param mixed $value |
||
| 766 | * @param integer $minLength |
||
| 767 | * @param integer $maxLength |
||
| 768 | * @param string|null $message |
||
| 769 | * @param string|null $propertyPath |
||
| 770 | * @param string $encoding |
||
| 771 | * @return bool |
||
| 772 | * @throws \Assert\AssertionFailedException |
||
| 773 | */ |
||
| 774 | public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 775 | { |
||
| 776 | static::string($value, $message, $propertyPath); |
||
| 777 | |||
| 778 | if (mb_strlen($value, $encoding) < $minLength) { |
||
| 779 | $message = sprintf( |
||
| 780 | $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.', |
||
| 781 | static::stringify($value), |
||
| 782 | $minLength, |
||
| 783 | mb_strlen($value, $encoding) |
||
| 784 | ); |
||
| 785 | |||
| 786 | $constraints = array('min_length' => $minLength, 'encoding' => $encoding); |
||
| 787 | throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints); |
||
| 788 | } |
||
| 789 | |||
| 790 | if (mb_strlen($value, $encoding) > $maxLength) { |
||
| 791 | $message = sprintf( |
||
| 792 | $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.', |
||
| 793 | static::stringify($value), |
||
| 794 | $maxLength, |
||
| 795 | mb_strlen($value, $encoding) |
||
| 796 | ); |
||
| 797 | |||
| 798 | $constraints = array('max_length' => $maxLength, 'encoding' => $encoding); |
||
| 799 | throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints); |
||
| 800 | } |
||
| 801 | |||
| 802 | return true; |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Assert that string starts with a sequence of chars. |
||
| 807 | * |
||
| 808 | * @param mixed $string |
||
| 809 | * @param string $needle |
||
| 810 | * @param string|null $message |
||
| 811 | * @param string|null $propertyPath |
||
| 812 | * @param string $encoding |
||
| 813 | * @return bool |
||
| 814 | * @throws \Assert\AssertionFailedException |
||
| 815 | */ |
||
| 816 | public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 817 | { |
||
| 818 | static::string($string, $message, $propertyPath); |
||
| 819 | |||
| 820 | if (mb_strpos($string, $needle, null, $encoding) !== 0) { |
||
| 821 | $message = sprintf( |
||
| 822 | $message ?: 'Value "%s" does not start with "%s".', |
||
| 823 | static::stringify($string), |
||
| 824 | static::stringify($needle) |
||
| 825 | ); |
||
| 826 | |||
| 827 | $constraints = array('needle' => $needle, 'encoding' => $encoding); |
||
| 828 | throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints); |
||
| 829 | } |
||
| 830 | |||
| 831 | return true; |
||
| 832 | } |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Assert that string ends with a sequence of chars. |
||
| 836 | * |
||
| 837 | * @param mixed $string |
||
| 838 | * @param string $needle |
||
| 839 | * @param string|null $message |
||
| 840 | * @param string|null $propertyPath |
||
| 841 | * @param string $encoding |
||
| 842 | * @return bool |
||
| 843 | * @throws \Assert\AssertionFailedException |
||
| 844 | */ |
||
| 845 | public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 846 | { |
||
| 847 | static::string($string, $message, $propertyPath); |
||
| 848 | |||
| 849 | $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding); |
||
| 850 | |||
| 851 | if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) { |
||
| 852 | $message = sprintf( |
||
| 853 | $message ?: 'Value "%s" does not end with "%s".', |
||
| 854 | static::stringify($string), |
||
| 855 | static::stringify($needle) |
||
| 856 | ); |
||
| 857 | |||
| 858 | $constraints = array('needle' => $needle, 'encoding' => $encoding); |
||
| 859 | throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints); |
||
| 860 | } |
||
| 861 | |||
| 862 | return true; |
||
| 863 | } |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Assert that string contains a sequence of chars. |
||
| 867 | * |
||
| 868 | * @param mixed $string |
||
| 869 | * @param string $needle |
||
| 870 | * @param string|null $message |
||
| 871 | * @param string|null $propertyPath |
||
| 872 | * @param string $encoding |
||
| 873 | * @return bool |
||
| 874 | * @throws \Assert\AssertionFailedException |
||
| 875 | */ |
||
| 876 | public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8') |
||
| 877 | { |
||
| 878 | static::string($string, $message, $propertyPath); |
||
| 879 | |||
| 880 | if (mb_strpos($string, $needle, null, $encoding) === false) { |
||
| 881 | $message = sprintf( |
||
| 882 | $message ?: 'Value "%s" does not contain "%s".', |
||
| 883 | static::stringify($string), |
||
| 884 | static::stringify($needle) |
||
| 885 | ); |
||
| 886 | |||
| 887 | $constraints = array('needle' => $needle, 'encoding' => $encoding); |
||
| 888 | throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints); |
||
| 889 | } |
||
| 890 | |||
| 891 | return true; |
||
| 892 | } |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Assert that value is in array of choices. |
||
| 896 | * |
||
| 897 | * @param mixed $value |
||
| 898 | * @param array $choices |
||
| 899 | * @param string|null $message |
||
| 900 | * @param string|null $propertyPath |
||
| 901 | * @return bool |
||
| 902 | * @throws \Assert\AssertionFailedException |
||
| 903 | */ |
||
| 904 | public static function choice($value, array $choices, $message = null, $propertyPath = null) |
||
| 905 | { |
||
| 906 | if (! in_array($value, $choices, true)) { |
||
| 907 | $message = sprintf( |
||
| 908 | $message ?: 'Value "%s" is not an element of the valid values: %s', |
||
| 909 | static::stringify($value), |
||
| 910 | implode(", ", array_map('Assert\Assertion::stringify', $choices)) |
||
| 911 | ); |
||
| 912 | |||
| 913 | throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices)); |
||
| 914 | } |
||
| 915 | |||
| 916 | return true; |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Alias of {@see choice()} |
||
| 921 | * |
||
| 922 | * @param mixed $value |
||
| 923 | * @param array $choices |
||
| 924 | * @param string|null $message |
||
| 925 | * @param string|null $propertyPath |
||
| 926 | * @return bool |
||
| 927 | */ |
||
| 928 | public static function inArray($value, array $choices, $message = null, $propertyPath = null) |
||
| 929 | { |
||
| 930 | return static::choice($value, $choices, $message, $propertyPath); |
||
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Assert that value is numeric. |
||
| 935 | * |
||
| 936 | * @param mixed $value |
||
| 937 | * @param string|null $message |
||
| 938 | * @param string|null $propertyPath |
||
| 939 | * @return bool |
||
| 940 | * @throws \Assert\AssertionFailedException |
||
| 941 | */ |
||
| 942 | public static function numeric($value, $message = null, $propertyPath = null) |
||
| 943 | { |
||
| 944 | if (! is_numeric($value)) { |
||
| 945 | $message = sprintf( |
||
| 946 | $message ?: 'Value "%s" is not numeric.', |
||
| 947 | static::stringify($value) |
||
| 948 | ); |
||
| 949 | |||
| 950 | throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath); |
||
| 951 | } |
||
| 952 | |||
| 953 | return true; |
||
| 954 | } |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Assert that value is an array. |
||
| 958 | * |
||
| 959 | * @param mixed $value |
||
| 960 | * @param string|null $message |
||
| 961 | * @param string|null $propertyPath |
||
| 962 | * @return bool |
||
| 963 | * @throws \Assert\AssertionFailedException |
||
| 964 | */ |
||
| 965 | public static function isArray($value, $message = null, $propertyPath = null) |
||
| 966 | { |
||
| 967 | if (! is_array($value)) { |
||
| 968 | $message = sprintf( |
||
| 969 | $message ?: 'Value "%s" is not an array.', |
||
| 970 | static::stringify($value) |
||
| 971 | ); |
||
| 972 | |||
| 973 | throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath); |
||
| 974 | } |
||
| 975 | |||
| 976 | return true; |
||
| 977 | } |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Assert that value is an array or a traversable object. |
||
| 981 | * |
||
| 982 | * @param mixed $value |
||
| 983 | * @param string|null $message |
||
| 984 | * @param string|null $propertyPath |
||
| 985 | * @return bool |
||
| 986 | * @throws \Assert\AssertionFailedException |
||
| 987 | */ |
||
| 988 | public static function isTraversable($value, $message = null, $propertyPath = null) |
||
| 989 | { |
||
| 990 | if (! is_array($value) && ! $value instanceof \Traversable) { |
||
| 991 | $message = sprintf( |
||
| 992 | $message ?: 'Value "%s" is not an array and does not implement Traversable.', |
||
| 993 | static::stringify($value) |
||
| 994 | ); |
||
| 995 | |||
| 996 | throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath); |
||
| 997 | } |
||
| 998 | |||
| 999 | return true; |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Assert that value is an array or an array-accessible object. |
||
| 1004 | * |
||
| 1005 | * @param mixed $value |
||
| 1006 | * @param string|null $message |
||
| 1007 | * @param string|null $propertyPath |
||
| 1008 | * @return bool |
||
| 1009 | * @throws \Assert\AssertionFailedException |
||
| 1010 | */ |
||
| 1011 | public static function isArrayAccessible($value, $message = null, $propertyPath = null) |
||
| 1012 | { |
||
| 1013 | if (! is_array($value) && ! $value instanceof \ArrayAccess) { |
||
| 1014 | $message = sprintf( |
||
| 1015 | $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.', |
||
| 1016 | static::stringify($value) |
||
| 1017 | ); |
||
| 1018 | |||
| 1019 | throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath); |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | return true; |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Assert that key exists in an array |
||
| 1027 | * |
||
| 1028 | * @param mixed $value |
||
| 1029 | * @param string|integer $key |
||
| 1030 | * @param string|null $message |
||
| 1031 | * @param string|null $propertyPath |
||
| 1032 | * @return bool |
||
| 1033 | * @throws \Assert\AssertionFailedException |
||
| 1034 | */ |
||
| 1035 | public static function keyExists($value, $key, $message = null, $propertyPath = null) |
||
| 1036 | { |
||
| 1037 | static::isArray($value, $message, $propertyPath); |
||
| 1038 | |||
| 1039 | if (! array_key_exists($key, $value)) { |
||
| 1040 | $message = sprintf( |
||
| 1041 | $message ?: 'Array does not contain an element with key "%s"', |
||
| 1042 | static::stringify($key) |
||
| 1043 | ); |
||
| 1044 | |||
| 1045 | throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key)); |
||
| 1046 | } |
||
| 1047 | |||
| 1048 | return true; |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Assert that key does not exist in an array |
||
| 1053 | * |
||
| 1054 | * @param mixed $value |
||
| 1055 | * @param string|integer $key |
||
| 1056 | * @param string|null $message |
||
| 1057 | * @param string|null $propertyPath |
||
| 1058 | * @return bool |
||
| 1059 | * @throws \Assert\AssertionFailedException |
||
| 1060 | */ |
||
| 1061 | public static function keyNotExists($value, $key, $message = null, $propertyPath = null) |
||
| 1062 | { |
||
| 1063 | static::isArray($value, $message, $propertyPath); |
||
| 1064 | |||
| 1065 | if (array_key_exists($key, $value)) { |
||
| 1066 | $message = sprintf( |
||
| 1067 | $message ?: 'Array contains an element with key "%s"', |
||
| 1068 | self::stringify($key) |
||
| 1069 | ); |
||
| 1070 | |||
| 1071 | throw static::createException($value, $message, static::INVALID_KEY_NOT_EXISTS, $propertyPath, array('key' => $key)); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | return true; |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Assert that key exists in an array/array-accessible object using isset() |
||
| 1079 | * |
||
| 1080 | * @param mixed $value |
||
| 1081 | * @param string|integer $key |
||
| 1082 | * @param string|null $message |
||
| 1083 | * @param string|null $propertyPath |
||
| 1084 | * @return bool |
||
| 1085 | * @throws \Assert\AssertionFailedException |
||
| 1086 | */ |
||
| 1087 | public static function keyIsset($value, $key, $message = null, $propertyPath = null) |
||
| 1088 | { |
||
| 1089 | static::isArrayAccessible($value, $message, $propertyPath); |
||
| 1090 | |||
| 1091 | if (! isset($value[$key])) { |
||
| 1092 | $message = sprintf( |
||
| 1093 | $message ?: 'The element with key "%s" was not found', |
||
| 1094 | static::stringify($key) |
||
| 1095 | ); |
||
| 1096 | |||
| 1097 | throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key)); |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | return true; |
||
| 1101 | } |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Assert that key exists in an array/array-accessible object and its value is not empty. |
||
| 1105 | * |
||
| 1106 | * @param mixed $value |
||
| 1107 | * @param string|integer $key |
||
| 1108 | * @param string|null $message |
||
| 1109 | * @param string|null $propertyPath |
||
| 1110 | * @return bool |
||
| 1111 | * @throws \Assert\AssertionFailedException |
||
| 1112 | */ |
||
| 1113 | public static function notEmptyKey($value, $key, $message = null, $propertyPath = null) |
||
| 1114 | { |
||
| 1115 | static::keyIsset($value, $key, $message, $propertyPath); |
||
| 1116 | static::notEmpty($value[$key], $message, $propertyPath); |
||
| 1117 | |||
| 1118 | return true; |
||
| 1119 | } |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Assert that value is not blank |
||
| 1123 | * |
||
| 1124 | * @param mixed $value |
||
| 1125 | * @param string|null $message |
||
| 1126 | * @param string|null $propertyPath |
||
| 1127 | * @return bool |
||
| 1128 | * @throws \Assert\AssertionFailedException |
||
| 1129 | */ |
||
| 1130 | public static function notBlank($value, $message = null, $propertyPath = null) |
||
| 1131 | { |
||
| 1132 | if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) { |
||
| 1133 | $message = sprintf( |
||
| 1134 | $message ?: 'Value "%s" is blank, but was expected to contain a value.', |
||
| 1135 | static::stringify($value) |
||
| 1136 | ); |
||
| 1137 | |||
| 1138 | throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath); |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | return true; |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Assert that value is instance of given class-name. |
||
| 1146 | * |
||
| 1147 | * @param mixed $value |
||
| 1148 | * @param string $className |
||
| 1149 | * @param string|null $message |
||
| 1150 | * @param string|null $propertyPath |
||
| 1151 | * @return bool |
||
| 1152 | * @throws \Assert\AssertionFailedException |
||
| 1153 | */ |
||
| 1154 | public static function isInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1155 | { |
||
| 1156 | if (! ($value instanceof $className)) { |
||
| 1157 | $message = sprintf( |
||
| 1158 | $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.', |
||
| 1159 | static::stringify($value), |
||
| 1160 | $className |
||
| 1161 | ); |
||
| 1162 | |||
| 1163 | throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className)); |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | return true; |
||
| 1167 | } |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Assert that value is not instance of given class-name. |
||
| 1171 | * |
||
| 1172 | * @param mixed $value |
||
| 1173 | * @param string $className |
||
| 1174 | * @param string|null $message |
||
| 1175 | * @param string|null $propertyPath |
||
| 1176 | * @return bool |
||
| 1177 | * @throws \Assert\AssertionFailedException |
||
| 1178 | */ |
||
| 1179 | public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null) |
||
| 1180 | { |
||
| 1181 | if ($value instanceof $className) { |
||
| 1182 | $message = sprintf( |
||
| 1183 | $message ?: 'Class "%s" was not expected to be instanceof of "%s".', |
||
| 1184 | static::stringify($value), |
||
| 1185 | $className |
||
| 1186 | ); |
||
| 1187 | |||
| 1188 | throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className)); |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | return true; |
||
| 1192 | } |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Assert that value is subclass of given class-name. |
||
| 1196 | * |
||
| 1197 | * @param mixed $value |
||
| 1198 | * @param string $className |
||
| 1199 | * @param string|null $message |
||
| 1200 | * @param string|null $propertyPath |
||
| 1201 | * @return bool |
||
| 1202 | * @throws \Assert\AssertionFailedException |
||
| 1203 | */ |
||
| 1204 | public static function subclassOf($value, $className, $message = null, $propertyPath = null) |
||
| 1205 | { |
||
| 1206 | if (! is_subclass_of($value, $className)) { |
||
| 1207 | $message = sprintf( |
||
| 1208 | $message ?: 'Class "%s" was expected to be subclass of "%s".', |
||
| 1209 | static::stringify($value), |
||
| 1210 | $className |
||
| 1211 | ); |
||
| 1212 | |||
| 1213 | throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className)); |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | return true; |
||
| 1217 | } |
||
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Assert that value is in range of numbers. |
||
| 1221 | * |
||
| 1222 | * @param mixed $value |
||
| 1223 | * @param integer $minValue |
||
| 1224 | * @param integer $maxValue |
||
| 1225 | * @param string|null $message |
||
| 1226 | * @param string|null $propertyPath |
||
| 1227 | * @return bool |
||
| 1228 | * @throws \Assert\AssertionFailedException |
||
| 1229 | */ |
||
| 1230 | public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null) |
||
| 1231 | { |
||
| 1232 | static::numeric($value, $message, $propertyPath); |
||
| 1233 | |||
| 1234 | if ($value < $minValue || $value > $maxValue) { |
||
| 1235 | $message = sprintf( |
||
| 1236 | $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".', |
||
| 1237 | static::stringify($value), |
||
| 1238 | static::stringify($minValue), |
||
| 1239 | static::stringify($maxValue) |
||
| 1240 | ); |
||
| 1241 | |||
| 1242 | throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue)); |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | return true; |
||
| 1246 | } |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Assert that a value is at least as big as a given limit |
||
| 1250 | * |
||
| 1251 | * @param mixed $value |
||
| 1252 | * @param mixed $minValue |
||
| 1253 | * @param string|null $message |
||
| 1254 | * @param string|null $propertyPath |
||
| 1255 | * @return bool |
||
| 1256 | * @throws \Assert\AssertionFailedException |
||
| 1257 | */ |
||
| 1258 | public static function min($value, $minValue, $message = null, $propertyPath = null) |
||
| 1259 | { |
||
| 1260 | static::numeric($value, $message, $propertyPath); |
||
| 1261 | |||
| 1262 | if ($value < $minValue) { |
||
| 1263 | $message = sprintf( |
||
| 1264 | $message ?: 'Number "%s" was expected to be at least "%s".', |
||
| 1265 | static::stringify($value), |
||
| 1266 | static::stringify($minValue) |
||
| 1267 | ); |
||
| 1268 | |||
| 1269 | throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue)); |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | return true; |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Assert that a number is smaller as a given limit |
||
| 1277 | * |
||
| 1278 | * @param mixed $value |
||
| 1279 | * @param mixed $maxValue |
||
| 1280 | * @param string|null $message |
||
| 1281 | * @param string|null $propertyPath |
||
| 1282 | * @return bool |
||
| 1283 | * @throws \Assert\AssertionFailedException |
||
| 1284 | */ |
||
| 1285 | public static function max($value, $maxValue, $message = null, $propertyPath = null) |
||
| 1286 | { |
||
| 1287 | static::numeric($value, $message, $propertyPath); |
||
| 1288 | |||
| 1289 | if ($value > $maxValue) { |
||
| 1290 | $message = sprintf( |
||
| 1291 | $message ?: 'Number "%s" was expected to be at most "%s".', |
||
| 1292 | static::stringify($value), |
||
| 1293 | static::stringify($maxValue) |
||
| 1294 | ); |
||
| 1295 | |||
| 1296 | throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue)); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | return true; |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Assert that a file exists |
||
| 1304 | * |
||
| 1305 | * @param string $value |
||
| 1306 | * @param string|null $message |
||
| 1307 | * @param string|null $propertyPath |
||
| 1308 | * @return bool |
||
| 1309 | * @throws \Assert\AssertionFailedException |
||
| 1310 | */ |
||
| 1311 | public static function file($value, $message = null, $propertyPath = null) |
||
| 1312 | { |
||
| 1313 | static::string($value, $message, $propertyPath); |
||
| 1314 | static::notEmpty($value, $message, $propertyPath); |
||
| 1315 | |||
| 1316 | if (! is_file($value)) { |
||
| 1317 | $message = sprintf( |
||
| 1318 | $message ?: 'File "%s" was expected to exist.', |
||
| 1319 | static::stringify($value) |
||
| 1320 | ); |
||
| 1321 | |||
| 1322 | throw static::createException($value, $message, static::INVALID_FILE, $propertyPath); |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | return true; |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Assert that a directory exists |
||
| 1330 | * |
||
| 1331 | * @param string $value |
||
| 1332 | * @param string|null $message |
||
| 1333 | * @param string|null $propertyPath |
||
| 1334 | * @return bool |
||
| 1335 | * @throws \Assert\AssertionFailedException |
||
| 1336 | */ |
||
| 1337 | public static function directory($value, $message = null, $propertyPath = null) |
||
| 1338 | { |
||
| 1339 | static::string($value, $message, $propertyPath); |
||
| 1340 | |||
| 1341 | if (! is_dir($value)) { |
||
| 1342 | $message = sprintf( |
||
| 1343 | $message ?: 'Path "%s" was expected to be a directory.', |
||
| 1344 | static::stringify($value) |
||
| 1345 | ); |
||
| 1346 | |||
| 1347 | throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath); |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | return true; |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Assert that the value is something readable |
||
| 1355 | * |
||
| 1356 | * @param string $value |
||
| 1357 | * @param string|null $message |
||
| 1358 | * @param string|null $propertyPath |
||
| 1359 | * @return bool |
||
| 1360 | * @throws \Assert\AssertionFailedException |
||
| 1361 | */ |
||
| 1362 | public static function readable($value, $message = null, $propertyPath = null) |
||
| 1363 | { |
||
| 1364 | static::string($value, $message, $propertyPath); |
||
| 1365 | |||
| 1366 | if (! is_readable($value)) { |
||
| 1367 | $message = sprintf( |
||
| 1368 | $message ?: 'Path "%s" was expected to be readable.', |
||
| 1369 | static::stringify($value) |
||
| 1370 | ); |
||
| 1371 | |||
| 1372 | throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath); |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | return true; |
||
| 1376 | } |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Assert that the value is something writeable |
||
| 1380 | * |
||
| 1381 | * @param string $value |
||
| 1382 | * @param string|null $message |
||
| 1383 | * @param string|null $propertyPath |
||
| 1384 | * @return bool |
||
| 1385 | * @throws \Assert\AssertionFailedException |
||
| 1386 | */ |
||
| 1387 | public static function writeable($value, $message = null, $propertyPath = null) |
||
| 1388 | { |
||
| 1389 | static::string($value, $message, $propertyPath); |
||
| 1390 | |||
| 1391 | if (! is_writeable($value)) { |
||
| 1392 | $message = sprintf( |
||
| 1393 | $message ?: 'Path "%s" was expected to be writeable.', |
||
| 1394 | static::stringify($value) |
||
| 1395 | ); |
||
| 1396 | |||
| 1397 | throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath); |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | return true; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
||
| 1405 | * |
||
| 1406 | * @param mixed $value |
||
| 1407 | * @param string|null $message |
||
| 1408 | * @param string|null $propertyPath |
||
| 1409 | * @return bool |
||
| 1410 | * @throws \Assert\AssertionFailedException |
||
| 1411 | */ |
||
| 1412 | public static function email($value, $message = null, $propertyPath = null) |
||
| 1413 | { |
||
| 1414 | static::string($value, $message, $propertyPath); |
||
| 1415 | |||
| 1416 | $message = $message ?: 'Value "%s" was expected to be a valid e-mail address.' |
||
| 1417 | |||
| 1418 | if (! filter_var($value, FILTER_VALIDATE_EMAIL)) { |
||
|
|
|||
| 1419 | $message = sprintf($message, static::stringify($value)); |
||
| 1420 | |||
| 1421 | throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath); |
||
| 1422 | } else { |
||
| 1423 | $host = substr($value, strpos($value, '@') + 1); |
||
| 1424 | |||
| 1425 | // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3 |
||
| 1426 | if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) { |
||
| 1427 | $message = sprintf($message, static::stringify($value)); |
||
| 1428 | |||
| 1429 | throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath); |
||
| 1430 | } |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | return true; |
||
| 1434 | } |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Assert that value is an URL. |
||
| 1438 | * |
||
| 1439 | * This code snipped was taken from the Symfony project and modified to the special demands of this method. |
||
| 1440 | * |
||
| 1441 | * @param mixed $value |
||
| 1442 | * @param string|null $message |
||
| 1443 | * @param string|null $propertyPath |
||
| 1444 | * @return bool |
||
| 1445 | * @throws \Assert\AssertionFailedException |
||
| 1446 | * |
||
| 1447 | * |
||
| 1448 | * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
||
| 1449 | * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php |
||
| 1450 | */ |
||
| 1451 | public static function url($value, $message = null, $propertyPath = null) |
||
| 1452 | { |
||
| 1453 | static::string($value, $message, $propertyPath); |
||
| 1454 | |||
| 1455 | $protocols = array('http', 'https'); |
||
| 1456 | |||
| 1457 | $pattern = '~^ |
||
| 1458 | (%s):// # protocol |
||
| 1459 | (([\pL\pN-]+:)?([\pL\pN-]+)@)? # basic auth |
||
| 1460 | ( |
||
| 1461 | ([\pL\pN\pS-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name |
||
| 1462 | | # or |
||
| 1463 | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address |
||
| 1464 | | # or |
||
| 1465 | \[ |
||
| 1466 | (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::)))) |
||
| 1467 | \] # an IPv6 address |
||
| 1468 | ) |
||
| 1469 | (:[0-9]+)? # a port (optional) |
||
| 1470 | (/?|/\S+|\?\S*|\#\S*) # a /, nothing, a / with something, a query or a fragment |
||
| 1471 | $~ixu'; |
||
| 1472 | |||
| 1473 | $pattern = sprintf($pattern, implode('|', $protocols)); |
||
| 1474 | |||
| 1475 | if (!preg_match($pattern, $value)) { |
||
| 1476 | $message = sprintf( |
||
| 1477 | $message ?: 'Value "%s" was expected to be a valid URL starting with http or https', |
||
| 1478 | static::stringify($value) |
||
| 1479 | ); |
||
| 1480 | |||
| 1481 | throw static::createException($value, $message, static::INVALID_URL, $propertyPath); |
||
| 1482 | } |
||
| 1483 | |||
| 1484 | return true; |
||
| 1485 | } |
||
| 1486 | |||
| 1487 | /** |
||
| 1488 | * Assert that value is alphanumeric. |
||
| 1489 | * |
||
| 1490 | * @param mixed $value |
||
| 1491 | * @param string|null $message |
||
| 1492 | * @param string|null $propertyPath |
||
| 1493 | * @return bool |
||
| 1494 | * @throws \Assert\AssertionFailedException |
||
| 1495 | */ |
||
| 1496 | public static function alnum($value, $message = null, $propertyPath = null) |
||
| 1497 | { |
||
| 1498 | try { |
||
| 1499 | static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath); |
||
| 1500 | } catch (AssertionFailedException $e) { |
||
| 1501 | $message = sprintf( |
||
| 1502 | $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.', |
||
| 1503 | static::stringify($value) |
||
| 1504 | ); |
||
| 1505 | |||
| 1506 | throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath); |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | return true; |
||
| 1510 | } |
||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * Assert that the value is boolean True. |
||
| 1514 | * |
||
| 1515 | * @param mixed $value |
||
| 1516 | * @param string|null $message |
||
| 1517 | * @param string|null $propertyPath |
||
| 1518 | * @return bool |
||
| 1519 | * @throws \Assert\AssertionFailedException |
||
| 1520 | */ |
||
| 1521 | public static function true($value, $message = null, $propertyPath = null) |
||
| 1522 | { |
||
| 1523 | if ($value !== true) { |
||
| 1524 | $message = sprintf( |
||
| 1525 | $message ?: 'Value "%s" is not TRUE.', |
||
| 1526 | static::stringify($value) |
||
| 1527 | ); |
||
| 1528 | |||
| 1529 | throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath); |
||
| 1530 | } |
||
| 1531 | |||
| 1532 | return true; |
||
| 1533 | } |
||
| 1534 | |||
| 1535 | /** |
||
| 1536 | * Assert that the value is boolean False. |
||
| 1537 | * |
||
| 1538 | * @param mixed $value |
||
| 1539 | * @param string|null $message |
||
| 1540 | * @param string|null $propertyPath |
||
| 1541 | * @return bool |
||
| 1542 | * @throws \Assert\AssertionFailedException |
||
| 1543 | */ |
||
| 1544 | public static function false($value, $message = null, $propertyPath = null) |
||
| 1545 | { |
||
| 1546 | if ($value !== false) { |
||
| 1547 | $message = sprintf( |
||
| 1548 | $message ?: 'Value "%s" is not FALSE.', |
||
| 1549 | static::stringify($value) |
||
| 1550 | ); |
||
| 1551 | |||
| 1552 | throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath); |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | return true; |
||
| 1556 | } |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * Assert that the class exists. |
||
| 1560 | * |
||
| 1561 | * @param mixed $value |
||
| 1562 | * @param string|null $message |
||
| 1563 | * @param string|null $propertyPath |
||
| 1564 | * @return bool |
||
| 1565 | * @throws \Assert\AssertionFailedException |
||
| 1566 | */ |
||
| 1567 | public static function classExists($value, $message = null, $propertyPath = null) |
||
| 1568 | { |
||
| 1569 | if (! class_exists($value)) { |
||
| 1570 | $message = sprintf( |
||
| 1571 | $message ?: 'Class "%s" does not exist.', |
||
| 1572 | static::stringify($value) |
||
| 1573 | ); |
||
| 1574 | |||
| 1575 | throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath); |
||
| 1576 | } |
||
| 1577 | |||
| 1578 | return true; |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Assert that the interface exists. |
||
| 1583 | * |
||
| 1584 | * @param mixed $value |
||
| 1585 | * @param string|null $message |
||
| 1586 | * @param string|null $propertyPath |
||
| 1587 | * @return bool |
||
| 1588 | * @throws \Assert\AssertionFailedException |
||
| 1589 | */ |
||
| 1590 | public static function interfaceExists($value, $message = null, $propertyPath = null) |
||
| 1591 | { |
||
| 1592 | if (! interface_exists($value)) { |
||
| 1593 | $message = sprintf( |
||
| 1594 | $message ?: 'Interface "%s" does not exist.', |
||
| 1595 | static::stringify($value) |
||
| 1596 | ); |
||
| 1597 | |||
| 1598 | throw static::createException($value, $message, static::INVALID_INTERFACE, $propertyPath); |
||
| 1599 | } |
||
| 1600 | |||
| 1601 | return true; |
||
| 1602 | } |
||
| 1603 | |||
| 1604 | /** |
||
| 1605 | * Assert that the class implements the interface |
||
| 1606 | * |
||
| 1607 | * @param mixed $class |
||
| 1608 | * @param string $interfaceName |
||
| 1609 | * @param string|null $message |
||
| 1610 | * @param string|null $propertyPath |
||
| 1611 | * @return bool |
||
| 1612 | * @throws \Assert\AssertionFailedException |
||
| 1613 | */ |
||
| 1614 | public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null) |
||
| 1615 | { |
||
| 1616 | $reflection = new \ReflectionClass($class); |
||
| 1617 | if (! $reflection->implementsInterface($interfaceName)) { |
||
| 1618 | $message = sprintf( |
||
| 1619 | $message ?: 'Class "%s" does not implement interface "%s".', |
||
| 1620 | static::stringify($class), |
||
| 1621 | static::stringify($interfaceName) |
||
| 1622 | ); |
||
| 1623 | |||
| 1624 | throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName)); |
||
| 1625 | } |
||
| 1626 | |||
| 1627 | return true; |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | /** |
||
| 1631 | * Assert that the given string is a valid json string. |
||
| 1632 | * |
||
| 1633 | * NOTICE: |
||
| 1634 | * Since this does a json_decode to determine its validity |
||
| 1635 | * you probably should consider, when using the variable |
||
| 1636 | * content afterwards, just to decode and check for yourself instead |
||
| 1637 | * of using this assertion. |
||
| 1638 | * |
||
| 1639 | * @param mixed $value |
||
| 1640 | * @param string|null $message |
||
| 1641 | * @param string|null $propertyPath |
||
| 1642 | * @return bool |
||
| 1643 | * @throws \Assert\AssertionFailedException |
||
| 1644 | */ |
||
| 1645 | public static function isJsonString($value, $message = null, $propertyPath = null) |
||
| 1646 | { |
||
| 1647 | if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) { |
||
| 1648 | $message = sprintf( |
||
| 1649 | $message ?: 'Value "%s" is not a valid JSON string.', |
||
| 1650 | static::stringify($value) |
||
| 1651 | ); |
||
| 1652 | |||
| 1653 | throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath); |
||
| 1654 | } |
||
| 1655 | |||
| 1656 | return true; |
||
| 1657 | } |
||
| 1658 | |||
| 1659 | /** |
||
| 1660 | * Assert that the given string is a valid UUID |
||
| 1661 | * |
||
| 1662 | * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed. |
||
| 1663 | * |
||
| 1664 | * @param string $value |
||
| 1665 | * @param string|null $message |
||
| 1666 | * @param string|null $propertyPath |
||
| 1667 | * @return bool |
||
| 1668 | * @throws \Assert\AssertionFailedException |
||
| 1669 | */ |
||
| 1670 | public static function uuid($value, $message = null, $propertyPath = null) |
||
| 1671 | { |
||
| 1672 | $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
||
| 1673 | |||
| 1674 | if ($value === '00000000-0000-0000-0000-000000000000') { |
||
| 1675 | return true; |
||
| 1676 | } |
||
| 1677 | |||
| 1678 | if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { |
||
| 1679 | $message = sprintf( |
||
| 1680 | $message ?: 'Value "%s" is not a valid UUID.', |
||
| 1681 | static::stringify($value) |
||
| 1682 | ); |
||
| 1683 | |||
| 1684 | throw static::createException($value, $message, static::INVALID_UUID, $propertyPath); |
||
| 1685 | } |
||
| 1686 | |||
| 1687 | return true; |
||
| 1688 | } |
||
| 1689 | |||
| 1690 | /** |
||
| 1691 | * Assert that the given string is a valid E164 Phone Number |
||
| 1692 | * |
||
| 1693 | * @link https://en.wikipedia.org/wiki/E.164 |
||
| 1694 | * |
||
| 1695 | * @param string $value |
||
| 1696 | * @param string|null $message |
||
| 1697 | * @param string|null $propertyPath |
||
| 1698 | * @return bool |
||
| 1699 | * @throws \Assert\AssertionFailedException |
||
| 1700 | */ |
||
| 1701 | public static function e164($value, $message = null, $propertyPath = null) |
||
| 1702 | { |
||
| 1703 | if (!preg_match('/^\+?[1-9]\d{1,14}$/', $value)) { |
||
| 1704 | $message = sprintf( |
||
| 1705 | $message ?: 'Value "%s" is not a valid E164.', |
||
| 1706 | static::stringify($value) |
||
| 1707 | ); |
||
| 1708 | |||
| 1709 | throw static::createException($value, $message, static::INVALID_E164, $propertyPath); |
||
| 1710 | } |
||
| 1711 | |||
| 1712 | return true; |
||
| 1713 | } |
||
| 1714 | |||
| 1715 | /** |
||
| 1716 | * Assert that the count of countable is equal to count. |
||
| 1717 | * |
||
| 1718 | * @param array|\Countable $countable |
||
| 1719 | * @param int $count |
||
| 1720 | * @param string $message |
||
| 1721 | * @param string $propertyPath |
||
| 1722 | * @return bool |
||
| 1723 | * @throws \Assert\AssertionFailedException |
||
| 1724 | */ |
||
| 1725 | public static function count($countable, $count, $message = null, $propertyPath = null) |
||
| 1726 | { |
||
| 1727 | if ($count !== count($countable)) { |
||
| 1728 | $message = sprintf( |
||
| 1729 | $message ?: 'List does not contain exactly "%d" elements.', |
||
| 1730 | static::stringify($count) |
||
| 1731 | ); |
||
| 1732 | |||
| 1733 | throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count)); |
||
| 1734 | } |
||
| 1735 | |||
| 1736 | return true; |
||
| 1737 | } |
||
| 1738 | |||
| 1739 | /** |
||
| 1740 | * static call handler to implement: |
||
| 1741 | * - "null or assertion" delegation |
||
| 1742 | * - "all" delegation |
||
| 1743 | * |
||
| 1744 | * @param string $method |
||
| 1745 | * @param array $args |
||
| 1746 | * |
||
| 1747 | * @return bool|mixed |
||
| 1748 | */ |
||
| 1749 | public static function __callStatic($method, $args) |
||
| 1750 | { |
||
| 1751 | if (strpos($method, "nullOr") === 0) { |
||
| 1752 | if (! array_key_exists(0, $args)) { |
||
| 1753 | throw new BadMethodCallException("Missing the first argument."); |
||
| 1754 | } |
||
| 1755 | |||
| 1756 | if ($args[0] === null) { |
||
| 1757 | return true; |
||
| 1758 | } |
||
| 1759 | |||
| 1760 | $method = substr($method, 6); |
||
| 1761 | |||
| 1762 | return call_user_func_array(array(get_called_class(), $method), $args); |
||
| 1763 | } |
||
| 1764 | |||
| 1765 | if (strpos($method, "all") === 0) { |
||
| 1766 | if (! array_key_exists(0, $args)) { |
||
| 1767 | throw new BadMethodCallException("Missing the first argument."); |
||
| 1768 | } |
||
| 1769 | |||
| 1770 | static::isTraversable($args[0]); |
||
| 1771 | |||
| 1772 | $method = substr($method, 3); |
||
| 1773 | $values = array_shift($args); |
||
| 1774 | $calledClass = get_called_class(); |
||
| 1775 | |||
| 1776 | foreach ($values as $value) { |
||
| 1777 | call_user_func_array(array($calledClass, $method), array_merge(array($value), $args)); |
||
| 1778 | } |
||
| 1779 | |||
| 1780 | return true; |
||
| 1781 | } |
||
| 1782 | |||
| 1783 | throw new BadMethodCallException("No assertion Assertion#" . $method . " exists."); |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Determines if the values array has every choice as key and that this choice has content. |
||
| 1788 | * |
||
| 1789 | * @param array $values |
||
| 1790 | * @param array $choices |
||
| 1791 | * @param null $message |
||
| 1792 | * @param null $propertyPath |
||
| 1793 | * @return bool |
||
| 1794 | */ |
||
| 1795 | public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null) |
||
| 1796 | { |
||
| 1797 | self::notEmpty($values, $message, $propertyPath); |
||
| 1798 | |||
| 1799 | foreach ($choices as $choice) { |
||
| 1800 | self::notEmptyKey($values, $choice, $message, $propertyPath); |
||
| 1801 | } |
||
| 1802 | |||
| 1803 | return true; |
||
| 1804 | } |
||
| 1805 | |||
| 1806 | /** |
||
| 1807 | * Determines that the named method is defined in the provided object. |
||
| 1808 | * |
||
| 1809 | * @param string $value |
||
| 1810 | * @param mixed $object |
||
| 1811 | * @param null $message |
||
| 1812 | * @param null $propertyPath |
||
| 1813 | * @return bool |
||
| 1814 | * @throws |
||
| 1815 | */ |
||
| 1816 | public static function methodExists($value, $object, $message = null, $propertyPath = null) |
||
| 1817 | { |
||
| 1818 | self::isObject($object, $message, $propertyPath); |
||
| 1819 | |||
| 1820 | if (!method_exists($object, $value)) { |
||
| 1821 | $message = sprintf( |
||
| 1822 | $message ?: 'Expected "%s" does not exist in provided object.', |
||
| 1823 | static::stringify($value) |
||
| 1824 | ); |
||
| 1825 | |||
| 1826 | throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath); |
||
| 1827 | } |
||
| 1828 | |||
| 1829 | return true; |
||
| 1830 | } |
||
| 1831 | |||
| 1832 | /** |
||
| 1833 | * Determines that the provided value is an object. |
||
| 1834 | * |
||
| 1835 | * @param mixed $value |
||
| 1836 | * @param null $message |
||
| 1837 | * @param null $propertyPath |
||
| 1838 | * @return bool |
||
| 1839 | */ |
||
| 1840 | public static function isObject($value, $message = null, $propertyPath = null) |
||
| 1841 | { |
||
| 1842 | if (!is_object($value)) { |
||
| 1843 | $message = sprintf( |
||
| 1844 | $message ?: 'Provided "%s" is not a valid object.', |
||
| 1845 | static::stringify($value) |
||
| 1846 | ); |
||
| 1847 | |||
| 1848 | throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath); |
||
| 1849 | } |
||
| 1850 | |||
| 1851 | return true; |
||
| 1852 | } |
||
| 1853 | |||
| 1854 | /** |
||
| 1855 | * Determines if the value is less than given limit. |
||
| 1856 | * |
||
| 1857 | * @param mixed $value |
||
| 1858 | * @param mixed $limit |
||
| 1859 | * @param null $message |
||
| 1860 | * @param null $propertyPath |
||
| 1861 | * @return bool |
||
| 1862 | */ |
||
| 1863 | public static function lessThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1864 | { |
||
| 1865 | if ($value >= $limit) { |
||
| 1866 | $message = sprintf( |
||
| 1867 | $message ?: 'Provided "%s" is not less than "%s".', |
||
| 1868 | static::stringify($value), |
||
| 1869 | static::stringify($limit) |
||
| 1870 | ); |
||
| 1871 | |||
| 1872 | throw static::createException($value, $message, static::INVALID_LESS, $propertyPath); |
||
| 1873 | } |
||
| 1874 | |||
| 1875 | return true; |
||
| 1876 | } |
||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Determines if the value is less or than given limit. |
||
| 1880 | * |
||
| 1881 | * @param mixed $value |
||
| 1882 | * @param mixed $limit |
||
| 1883 | * @param null $message |
||
| 1884 | * @param null $propertyPath |
||
| 1885 | * @return bool |
||
| 1886 | */ |
||
| 1887 | public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1888 | { |
||
| 1889 | if ($value > $limit) { |
||
| 1890 | $message = sprintf( |
||
| 1891 | $message ?: 'Provided "%s" is not less or equal than "%s".', |
||
| 1892 | static::stringify($value), |
||
| 1893 | static::stringify($limit) |
||
| 1894 | ); |
||
| 1895 | |||
| 1896 | throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath); |
||
| 1897 | } |
||
| 1898 | |||
| 1899 | return true; |
||
| 1900 | } |
||
| 1901 | |||
| 1902 | /** |
||
| 1903 | * Determines if the value is greater than given limit. |
||
| 1904 | * |
||
| 1905 | * @param mixed $value |
||
| 1906 | * @param mixed $limit |
||
| 1907 | * @param null $message |
||
| 1908 | * @param null $propertyPath |
||
| 1909 | * @return bool |
||
| 1910 | */ |
||
| 1911 | public static function greaterThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1912 | { |
||
| 1913 | if ($value <= $limit) { |
||
| 1914 | $message = sprintf( |
||
| 1915 | $message ?: 'Provided "%s" is not greater than "%s".', |
||
| 1916 | static::stringify($value), |
||
| 1917 | static::stringify($limit) |
||
| 1918 | ); |
||
| 1919 | |||
| 1920 | throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath); |
||
| 1921 | } |
||
| 1922 | |||
| 1923 | return true; |
||
| 1924 | } |
||
| 1925 | |||
| 1926 | /** |
||
| 1927 | * Determines if the value is greater or equal than given limit. |
||
| 1928 | * |
||
| 1929 | * @param mixed $value |
||
| 1930 | * @param mixed $limit |
||
| 1931 | * @param null $message |
||
| 1932 | * @param null $propertyPath |
||
| 1933 | * @return bool |
||
| 1934 | */ |
||
| 1935 | public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) |
||
| 1936 | { |
||
| 1937 | if ($value < $limit) { |
||
| 1938 | $message = sprintf( |
||
| 1939 | $message ?: 'Provided "%s" is not greater or equal than "%s".', |
||
| 1940 | static::stringify($value), |
||
| 1941 | static::stringify($limit) |
||
| 1942 | ); |
||
| 1943 | |||
| 1944 | throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath); |
||
| 1945 | } |
||
| 1946 | |||
| 1947 | return true; |
||
| 1948 | } |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
||
| 1952 | * |
||
| 1953 | * @param mixed $value |
||
| 1954 | * @param mixed $lowerLimit |
||
| 1955 | * @param mixed $upperLimit |
||
| 1956 | * @param string $message |
||
| 1957 | * @param string $propertyPath |
||
| 1958 | * @return bool |
||
| 1959 | */ |
||
| 1960 | public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
| 1961 | { |
||
| 1962 | if ($lowerLimit > $value || $value > $upperLimit) { |
||
| 1963 | $message = sprintf( |
||
| 1964 | $message ?: 'Provided "%s" is neither greater than or equal to "%s" nor less than or equal to "%s".', |
||
| 1965 | static::stringify($value), |
||
| 1966 | static::stringify($lowerLimit), |
||
| 1967 | static::stringify($upperLimit) |
||
| 1968 | ); |
||
| 1969 | |||
| 1970 | throw static::createException($value, $message, static::INVALID_BETWEEN, $propertyPath); |
||
| 1971 | } |
||
| 1972 | |||
| 1973 | return true; |
||
| 1974 | } |
||
| 1975 | |||
| 1976 | /** |
||
| 1977 | * Assert that a value is greater than a lower limit, and less than an upper limit. |
||
| 1978 | * |
||
| 1979 | * @param mixed $value |
||
| 1980 | * @param mixed $lowerLimit |
||
| 1981 | * @param mixed $upperLimit |
||
| 1982 | * @param string $message |
||
| 1983 | * @param string $propertyPath |
||
| 1984 | * @return bool |
||
| 1985 | */ |
||
| 1986 | public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) |
||
| 1987 | { |
||
| 1988 | if ($lowerLimit >= $value || $value >= $upperLimit) { |
||
| 1989 | $message = sprintf( |
||
| 1990 | $message ?: 'Provided "%s" is neither greater than "%s" nor less than "%s".', |
||
| 1991 | static::stringify($value), |
||
| 1992 | static::stringify($lowerLimit), |
||
| 1993 | static::stringify($upperLimit) |
||
| 1994 | ); |
||
| 1995 | |||
| 1996 | throw static::createException($value, $message, static::INVALID_BETWEEN_EXCLUSIVE, $propertyPath); |
||
| 1997 | } |
||
| 1998 | |||
| 1999 | return true; |
||
| 2000 | } |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Assert that extension is loaded. |
||
| 2004 | * |
||
| 2005 | * @param mixed $value |
||
| 2006 | * @param string|null $message |
||
| 2007 | * @param string|null $propertyPath |
||
| 2008 | * @return bool |
||
| 2009 | * @throws \Assert\AssertionFailedException |
||
| 2010 | */ |
||
| 2011 | public static function extensionLoaded($value, $message = null, $propertyPath = null) |
||
| 2012 | { |
||
| 2013 | if (! extension_loaded($value)) { |
||
| 2014 | $message = sprintf( |
||
| 2015 | $message ?: 'Extension "%s" is required.', |
||
| 2016 | static::stringify($value) |
||
| 2017 | ); |
||
| 2018 | |||
| 2019 | throw static::createException($value, $message, static::INVALID_EXTENSION, $propertyPath); |
||
| 2020 | } |
||
| 2021 | |||
| 2022 | return true; |
||
| 2023 | } |
||
| 2024 | |||
| 2025 | /** |
||
| 2026 | * Assert that date is valid and corresponds to the given format. |
||
| 2027 | * |
||
| 2028 | * @param string $value |
||
| 2029 | * @param string $format supports all of the options date(), except for the following: |
||
| 2030 | * N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r. |
||
| 2031 | * @param string|null $message |
||
| 2032 | * @param string|null $propertyPath |
||
| 2033 | * @return bool |
||
| 2034 | * |
||
| 2035 | * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters |
||
| 2036 | */ |
||
| 2037 | public static function date($value, $format, $message = null, $propertyPath = null) |
||
| 2038 | { |
||
| 2039 | static::string($value, $message, $propertyPath); |
||
| 2040 | static::string($format, $message, $propertyPath); |
||
| 2041 | |||
| 2042 | $dateTime = \DateTime::createFromFormat($format, $value); |
||
| 2043 | |||
| 2044 | if (false === $dateTime || $value !== $dateTime->format($format)) { |
||
| 2045 | $message = sprintf( |
||
| 2046 | $message ?: 'Date "%s" is invalid or does not match format "%s".', |
||
| 2047 | static::stringify($value), |
||
| 2048 | static::stringify($format) |
||
| 2049 | ); |
||
| 2050 | |||
| 2051 | throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format)); |
||
| 2052 | } |
||
| 2053 | |||
| 2054 | return true; |
||
| 2055 | } |
||
| 2056 | |||
| 2057 | /** |
||
| 2058 | * Determines that the provided value is callable. |
||
| 2059 | * |
||
| 2060 | * @param mixed $value |
||
| 2061 | * @param null $message |
||
| 2062 | * @param null $propertyPath |
||
| 2063 | * @return bool |
||
| 2064 | */ |
||
| 2065 | public static function isCallable($value, $message = null, $propertyPath = null) |
||
| 2066 | { |
||
| 2067 | if (!is_callable($value)) { |
||
| 2068 | $message = sprintf( |
||
| 2069 | $message ?: 'Provided "%s" is not a callable.', |
||
| 2070 | static::stringify($value) |
||
| 2071 | ); |
||
| 2072 | |||
| 2073 | throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath); |
||
| 2074 | } |
||
| 2075 | |||
| 2076 | return true; |
||
| 2077 | } |
||
| 2078 | |||
| 2079 | /** |
||
| 2080 | * Assert that the provided value is valid according to a callback. |
||
| 2081 | * |
||
| 2082 | * If the callback returns `false` the assertion will fail. |
||
| 2083 | * |
||
| 2084 | * @param mixed $value |
||
| 2085 | * @param callable $callback |
||
| 2086 | * @param string|null $message |
||
| 2087 | * @param string|null $propertyPath |
||
| 2088 | * @return bool |
||
| 2089 | */ |
||
| 2090 | public static function satisfy($value, $callback, $message = null, $propertyPath = null) |
||
| 2091 | { |
||
| 2092 | static::isCallable($callback); |
||
| 2093 | |||
| 2094 | if (call_user_func($callback, $value) === false) { |
||
| 2095 | $message = sprintf( |
||
| 2096 | $message ?: 'Provided "%s" is invalid according to custom rule.', |
||
| 2097 | static::stringify($value) |
||
| 2098 | ); |
||
| 2099 | |||
| 2100 | throw static::createException($value, $message, static::INVALID_SATISFY, $propertyPath); |
||
| 2101 | } |
||
| 2102 | |||
| 2103 | return true; |
||
| 2104 | } |
||
| 2105 | |||
| 2106 | /** |
||
| 2107 | * Assert that value is an IPv4 or IPv6 address |
||
| 2108 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 2109 | * |
||
| 2110 | * @param string $value |
||
| 2111 | * @param null|int $flag |
||
| 2112 | * @param string|null $message |
||
| 2113 | * @param string|null $propertyPath |
||
| 2114 | * @return bool |
||
| 2115 | * |
||
| 2116 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 2117 | */ |
||
| 2118 | public static function ip($value, $flag = null, $message = null, $propertyPath = null) |
||
| 2119 | { |
||
| 2120 | self::string($value, $message, $propertyPath); |
||
| 2121 | if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) { |
||
| 2122 | $message = sprintf( |
||
| 2123 | $message ?: 'Value "%s" was expected to be a valid IP address.', |
||
| 2124 | self::stringify($value) |
||
| 2125 | ); |
||
| 2126 | throw static::createException($value, $message, static::INVALID_IP, $propertyPath); |
||
| 2127 | } |
||
| 2128 | |||
| 2129 | return true; |
||
| 2130 | } |
||
| 2131 | |||
| 2132 | /** |
||
| 2133 | * Assert that value is an IPv4 address |
||
| 2134 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 2135 | * |
||
| 2136 | * @param string $value |
||
| 2137 | * @param null|int $flag |
||
| 2138 | * @param string|null $message |
||
| 2139 | * @param string|null $propertyPath |
||
| 2140 | * @return bool |
||
| 2141 | * |
||
| 2142 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 2143 | */ |
||
| 2144 | public static function ipv4($value, $flag = null, $message = null, $propertyPath = null) |
||
| 2145 | { |
||
| 2146 | self::ip($value, $flag | FILTER_FLAG_IPV4, $message ?: 'Value "%s" was expected to be a valid IPv4 address.', $propertyPath); |
||
| 2147 | |||
| 2148 | return true; |
||
| 2149 | } |
||
| 2150 | |||
| 2151 | /** |
||
| 2152 | * Assert that value is an IPv6 address |
||
| 2153 | * (using input_filter/FILTER_VALIDATE_IP). |
||
| 2154 | * |
||
| 2155 | * @param string $value |
||
| 2156 | * @param null|int $flag |
||
| 2157 | * @param string|null $message |
||
| 2158 | * @param string|null $propertyPath |
||
| 2159 | * @return bool |
||
| 2160 | * |
||
| 2161 | * @link http://php.net/manual/filter.filters.flags.php |
||
| 2162 | */ |
||
| 2163 | public static function ipv6($value, $flag = null, $message = null, $propertyPath = null) |
||
| 2164 | { |
||
| 2165 | self::ip($value, $flag | FILTER_FLAG_IPV6, $message ?: 'Value "%s" was expected to be a valid IPv6 address.', $propertyPath); |
||
| 2166 | |||
| 2167 | return true; |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Make a string version of a value. |
||
| 2172 | * |
||
| 2173 | * @param mixed $value |
||
| 2174 | * @return string |
||
| 2175 | */ |
||
| 2176 | protected static function stringify($value) |
||
| 2177 | { |
||
| 2178 | if (is_bool($value)) { |
||
| 2179 | return $value ? '<TRUE>' : '<FALSE>'; |
||
| 2180 | } |
||
| 2181 | |||
| 2182 | if (is_scalar($value)) { |
||
| 2183 | $val = (string)$value; |
||
| 2184 | |||
| 2185 | if (strlen($val) > 100) { |
||
| 2186 | $val = substr($val, 0, 97) . '...'; |
||
| 2187 | } |
||
| 2188 | |||
| 2189 | return $val; |
||
| 2190 | } |
||
| 2191 | |||
| 2192 | if (is_array($value)) { |
||
| 2193 | return '<ARRAY>'; |
||
| 2194 | } |
||
| 2195 | |||
| 2196 | if (is_object($value)) { |
||
| 2197 | return get_class($value); |
||
| 2198 | } |
||
| 2199 | |||
| 2200 | if (is_resource($value)) { |
||
| 2201 | return get_resource_type($value); |
||
| 2202 | } |
||
| 2203 | |||
| 2204 | if ($value === null) { |
||
| 2205 | return '<NULL>'; |
||
| 2206 | } |
||
| 2207 | |||
| 2208 | return gettype($value); |
||
| 2209 | } |
||
| 2210 | |||
| 2211 | /** |
||
| 2212 | * Assert that a constant is defined. |
||
| 2213 | * |
||
| 2214 | * @param mixed $constant |
||
| 2215 | * @param string|null $message |
||
| 2216 | * @param string|null $propertyPath |
||
| 2217 | * @return bool |
||
| 2218 | * @throws \Assert\AssertionFailedException |
||
| 2219 | */ |
||
| 2220 | public static function defined($constant, $message = null, $propertyPath = null) |
||
| 2221 | { |
||
| 2222 | if (!defined($constant)) { |
||
| 2223 | $message = sprintf($message ?: 'Value "%s" expected to be a defined constant.', $constant); |
||
| 2224 | |||
| 2225 | throw static::createException($constant, $message, static::INVALID_CONSTANT, $propertyPath); |
||
| 2226 | } |
||
| 2227 | |||
| 2228 | return true; |
||
| 2229 | } |
||
| 2230 | } |
||
| 2231 |