Skip to content

Commit

Permalink
Add not in array assertion. Closes #113.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Jul 27, 2016
1 parent 9e69917 commit 4ee69ee
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Assertion::notBlank($value);
Assertion::notEmpty($value);
Assertion::notEmptyKey($value, $key);
Assertion::notEq($value1, $value2);
Assertion::notInArray($value, $choices);
Assertion::notIsInstanceOf($value, $className);
Assertion::notNull($value);
Assertion::notSame($value1, $value2);
Expand Down
25 changes: 25 additions & 0 deletions lib/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* @method static void allNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty for all values.
* @method static void allNotEmptyKey($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty for all values.
* @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) for all values.
* @method static void allNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices for all values.
* @method static void allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name for all values.
* @method static void allNotNull($value, $message = null, $propertyPath = null) Assert that value is not null for all values.
* @method static void allNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) for all values.
Expand Down Expand Up @@ -126,6 +127,7 @@
* @method static void nullOrNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty or that the value is null.
* @method static void nullOrNotEmptyKey($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty or that the value is null.
* @method static void nullOrNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
* @method static void nullOrNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices or that the value is null.
* @method static void nullOrNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name or that the value is null.
* @method static void nullOrNotNull($value, $message = null, $propertyPath = null) Assert that value is not null or that the value is null.
* @method static void nullOrNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) or that the value is null.
Expand Down Expand Up @@ -182,6 +184,7 @@ class Assertion
const INVALID_TRAVERSABLE = 44;
const INVALID_ARRAY_ACCESSIBLE = 45;
const INVALID_KEY_ISSET = 46;
const INVALID_VALUE_IN_ARRAY = 47;
const INVALID_DIRECTORY = 101;
const INVALID_FILE = 102;
const INVALID_READABLE = 103;
Expand Down Expand Up @@ -311,6 +314,28 @@ public static function notSame($value1, $value2, $message = null, $propertyPath
}
}

/**
* Assert that value is not in array of choices.
*
* @param mixed $value
* @param array $choices
* @param string|null $message
* @param string|null $propertyPath
* @return void
* @throws \Assert\AssertionFailedException
*/
public static function notInArray($value, array $choices, $message = null, $propertyPath = null)
{
if (in_array($value, $choices) === true) {
$message = sprintf(
$message ?: 'Value "%s" is in given "%s".',
self::stringify($value),
self::stringify($choices)
);
throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath);
}
}

/**
* Assert that value is a php integer.
*
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @method AssertionChain notEmpty($message = null, $propertyPath = null) Assert that value is not empty.
* @method AssertionChain notEmptyKey($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty.
* @method AssertionChain notEq($value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ).
* @method AssertionChain notInArray($choices, $message = null, $propertyPath = null) Assert that value is not in array of choices.
* @method AssertionChain notIsInstanceOf($className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name.
* @method AssertionChain notNull($message = null, $propertyPath = null) Assert that value is not null.
* @method AssertionChain notSame($value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ).
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/LazyAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* @method LazyAssertion notEmpty($message = null, $propertyPath = null) Assert that value is not empty.
* @method LazyAssertion notEmptyKey($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty.
* @method LazyAssertion notEq($value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ).
* @method LazyAssertion notInArray($choices, $message = null, $propertyPath = null) Assert that value is not in array of choices.
* @method LazyAssertion notIsInstanceOf($className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name.
* @method LazyAssertion notNull($message = null, $propertyPath = null) Assert that value is not null.
* @method LazyAssertion notSame($value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ).
Expand Down
9 changes: 9 additions & 0 deletions tests/Assert/Tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ public function testNotSame()
Assertion::notSame(1, 1);
}

public function testNotInArray()
{
Assertion::notInArray(6, range(1, 5));

$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_VALUE_IN_ARRAY);
Assertion::notInArray(1, range(1, 5));
Assertion::notInArray(range('a', 'c'), range('a', 'd'));
}

public function testMin()
{
Assertion::min(1, 1);
Expand Down

0 comments on commit 4ee69ee

Please sign in to comment.