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