diff --git a/README.md b/README.md index 6e5bb46b..e70598f6 100644 --- a/README.md +++ b/README.md @@ -144,8 +144,10 @@ Method | Description `contains($value, $subString, $message = '')` | Check that a string contains a substring `notContains($value, $subString, $message = '')` | Check that a string does not contains a substring `startsWith($value, $prefix, $message = '')` | Check that a string has a prefix +`notStartsWith($value, $prefix, $message = '')` | Check that a string does not have a prefix `startsWithLetter($value, $message = '')` | Check that a string starts with a letter `endsWith($value, $suffix, $message = '')` | Check that a string has a suffix +`notEndsWith($value, $suffix, $message = '')` | Check that a string does not have a suffix `regex($value, $pattern, $message = '')` | Check that a string matches a regular expression `notRegex($value, $pattern, $message = '')` | Check that a string does not match a regular expression `unicodeLetters($value, $message = '')` | Check that a string contains Unicode letters only diff --git a/src/Assert.php b/src/Assert.php index e243cb42..7b58f9d1 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -74,8 +74,10 @@ * @method static void nullOrNotContains($value, $subString, $message = '') * @method static void nullOrNotWhitespaceOnly($value, $message = '') * @method static void nullOrStartsWith($value, $prefix, $message = '') + * @method static void nullOrNotStartsWith($value, $prefix, $message = '') * @method static void nullOrStartsWithLetter($value, $message = '') * @method static void nullOrEndsWith($value, $suffix, $message = '') + * @method static void nullOrNotEndsWith($value, $suffix, $message = '') * @method static void nullOrRegex($value, $pattern, $message = '') * @method static void nullOrNotRegex($value, $pattern, $message = '') * @method static void nullOrUnicodeLetters($value, $message = '') @@ -163,8 +165,10 @@ * @method static void allNotContains($values, $subString, $message = '') * @method static void allNotWhitespaceOnly($values, $message = '') * @method static void allStartsWith($values, $prefix, $message = '') + * @method static void allNotStartsWith($values, $prefix, $message = '') * @method static void allStartsWithLetter($values, $message = '') * @method static void allEndsWith($values, $suffix, $message = '') + * @method static void allNotEndsWith($values, $suffix, $message = '') * @method static void allRegex($values, $pattern, $message = '') * @method static void allNotRegex($values, $pattern, $message = '') * @method static void allUnicodeLetters($values, $message = '') @@ -1199,6 +1203,26 @@ public static function startsWith($value, $prefix, $message = '') } } + /** + * @psalm-pure + * + * @param string $value + * @param string $prefix + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function notStartsWith($value, $prefix, $message = '') + { + if (0 === \strpos($value, $prefix)) { + static::reportInvalidArgument(\sprintf( + $message ?: 'Expected a value not to start with %2$s. Got: %s', + static::valueToString($value), + static::valueToString($prefix) + )); + } + } + /** * @psalm-pure * @@ -1248,6 +1272,26 @@ public static function endsWith($value, $suffix, $message = '') } } + /** + * @psalm-pure + * + * @param string $value + * @param string $suffix + * @param string $message + * + * @throws InvalidArgumentException + */ + public static function notEndsWith($value, $suffix, $message = '') + { + if ($suffix === \substr($value, -\strlen($suffix))) { + static::reportInvalidArgument(\sprintf( + $message ?: 'Expected a value not to end with %2$s. Got: %s', + static::valueToString($value), + static::valueToString($suffix) + )); + } + } + /** * @psalm-pure * diff --git a/tests/AssertTest.php b/tests/AssertTest.php index a12a846c..49e9807e 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -295,6 +295,18 @@ public function getTests() array('startsWithLetter', array(''), false), array('startsWithLetter', array(null), false), array('startsWithLetter', array(66), false), + array('notStartsWith', array('abcd', 'ab'), false), + array('notStartsWith', array('abcd', 'bc'), true), + array('notStartsWith', array('', 'bc'), true), + array('notStartsWith', array('äþçð', 'äþ'), false), + array('notStartsWith', array('äþçð', 'þç'), true), + array('notStartsWith', array('', 'þç'), true), + array('notStartsWith', array('あいうえ', 'あい'), false), + array('notStartsWith', array('あいうえ', 'いう'), true), + array('notStartsWith', array('', 'いう'), true), + array('notStartsWith', array('😄😑☹️', '😄'), false), + array('notStartsWith', array('😄😑☹️', '😑'), true), + array('notStartsWith', array('', '😑'), true), array('endsWith', array('abcd', 'cd'), true), array('endsWith', array('abcd', 'bc'), false), array('endsWith', array('', 'bc'), false), @@ -307,6 +319,18 @@ public function getTests() array('endsWith', array('😄😑☹️', '☹️'), true), array('endsWith', array('😄😑☹️', '😑'), false), array('endsWith', array('', '😑'), false), + array('notEndsWith', array('abcd', 'cd'), false), + array('notEndsWith', array('abcd', 'bc'), true), + array('notEndsWith', array('', 'bc'), true), + array('notEndsWith', array('äþçð', 'çð'), false), + array('notEndsWith', array('äþçð', 'þç'), true), + array('notEndsWith', array('', 'þç'), true), + array('notEndsWith', array('あいうえ', 'うえ'), false), + array('notEndsWith', array('あいうえ', 'いう'), true), + array('notEndsWith', array('', 'いう'), true), + array('notEndsWith', array('😄😑☹️', '☹️'), false), + array('notEndsWith', array('😄😑☹️', '😑'), true), + array('notEndsWith', array('', '😑'), true), array('regex', array('abcd', '~^ab~'), true), array('regex', array('abcd', '~^bc~'), false), array('regex', array('', '~^bc~'), false),