Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This library contains efficient assertions to test the input and output of
your methods. With these assertions, you can greatly reduce the amount of coding
needed to write a safe implementation.

All assertions in the [`Assert`] class throw an `\InvalidArgumentException` if
All assertions in the [`Assert`] class throw an `\InvalidArgumentException` if
they fail.

FAQ
Expand All @@ -22,21 +22,21 @@ This library is heavily inspired by Benjamin Eberlei's wonderful [assert package
but fixes a usability issue with error messages that can't be fixed there without
breaking backwards compatibility.

This package features usable error messages by default. However, you can also
This package features usable error messages by default. However, you can also
easily write custom error messages:

```
Assert::string($path, 'The path is expected to be a string. Got: %s');
```

In [beberlei/assert], the ordering of the `%s` placeholders is different for
every assertion. This package, on the contrary, provides consistent placeholder
In [beberlei/assert], the ordering of the `%s` placeholders is different for
every assertion. This package, on the contrary, provides consistent placeholder
ordering for all assertions:

* `%s`: The tested value as string, e.g. `"/foo/bar"`.
* `%2$s`, `%3$s`, ...: Additional assertion-specific values, e.g. the
minimum/maximum length, allowed values, etc.

Check the source code of the assertions to find out details about the additional
available placeholders.

Expand Down Expand Up @@ -69,11 +69,11 @@ If you create an employee with an invalid ID, an exception is thrown:

```php
new Employee('foobar');
// => InvalidArgumentException:
// => InvalidArgumentException:
// The employee ID must be an integer. Got: string

new Employee(-10);
// => InvalidArgumentException:
// => InvalidArgumentException:
// The employee ID must be a positive integer. Got: -10
```

Expand Down Expand Up @@ -143,6 +143,7 @@ Method | Description
`endsWith($value, $suffix, $message = '')` | Check that a string has 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
`alpha($value, $message = '')` | Check that a string contains letters only
`digits($value, $message = '')` | Check that a string contains digits only
`alnum($value, $message = '')` | Check that a string contains letters and digits only
Expand Down
14 changes: 14 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @method static void nullOrEndsWith($value, $suffix, $message = '')
* @method static void nullOrRegex($value, $pattern, $message = '')
* @method static void nullOrNotRegex($value, $pattern, $message = '')
* @method static void nullOrUnicodeLetters($value, $message = '')
* @method static void nullOrAlpha($value, $message = '')
* @method static void nullOrDigits($value, $message = '')
* @method static void nullOrAlnum($value, $message = '')
Expand Down Expand Up @@ -149,6 +150,7 @@
* @method static void allEndsWith($values, $suffix, $message = '')
* @method static void allRegex($values, $pattern, $message = '')
* @method static void allNotRegex($values, $pattern, $message = '')
* @method static void allUnicodeLetters($values, $message = '')
* @method static void allAlpha($values, $message = '')
* @method static void allDigits($values, $message = '')
* @method static void allAlnum($values, $message = '')
Expand Down Expand Up @@ -719,6 +721,18 @@ public static function notRegex($value, $pattern, $message = '')
}
}

public static function unicodeLetters($value, $message = '')
{
static::string($value);

if (!preg_match('/^\p{L}+$/u', $value)) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected a value to contain only Unicode letters. Got: %s',
static::valueToString($value)
));
}
}

public static function alpha($value, $message = '')
{
$locale = setlocale(LC_CTYPE, 0);
Expand Down
7 changes: 7 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ public function getTests()
array('notRegex', array('abcd', '{^ab}'), false),
array('notRegex', array('abcd', '{^bc}'), true),
array('notRegex', array('', '{^bc}'), true),
array('unicodeLetters', array('abcd'), true),
array('unicodeLetters', array('ᴁڅਘธブ乶'), true),
array('unicodeLetters', array('ȁȄ'), true),
array('unicodeLetters', array('ȁ1Ȅ'), false),
array('unicodeLetters', array('©'), false),
array('unicodeLetters', array('🙁'), false),
array('unicodeLetters', array(''), false),
array('alpha', array('abcd'), true),
array('alpha', array('ab1cd'), false),
array('alpha', array(''), false),
Expand Down