Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds notInArray and notOneOf. #262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,28 @@ Method | Description

### Comparison Assertions

Method | Description
----------------------------------------------- | ------------------------------------------------------------------
`true($value, $message = '')` | Check that a value is `true`
`false($value, $message = '')` | Check that a value is `false`
`notFalse($value, $message = '')` | Check that a value is not `false`
`null($value, $message = '')` | Check that a value is `null`
`notNull($value, $message = '')` | Check that a value is not `null`
`isEmpty($value, $message = '')` | Check that a value is `empty()`
`notEmpty($value, $message = '')` | Check that a value is not `empty()`
`eq($value, $value2, $message = '')` | Check that a value equals another (`==`)
`notEq($value, $value2, $message = '')` | Check that a value does not equal another (`!=`)
`same($value, $value2, $message = '')` | Check that a value is identical to another (`===`)
`notSame($value, $value2, $message = '')` | Check that a value is not identical to another (`!==`)
`greaterThan($value, $value2, $message = '')` | Check that a value is greater than another
`greaterThanEq($value, $value2, $message = '')` | Check that a value is greater than or equal to another
`lessThan($value, $value2, $message = '')` | Check that a value is less than another
`lessThanEq($value, $value2, $message = '')` | Check that a value is less than or equal to another
`range($value, $min, $max, $message = '')` | Check that a value is within a range
`inArray($value, array $values, $message = '')` | Check that a value is one of a list of values
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values (alias of `inArray`)
Method | Description
----------------------------------------------------- | ------------------------------------------------------------------
`true($value, $message = '')` | Check that a value is `true`
`false($value, $message = '')` | Check that a value is `false`
`notFalse($value, $message = '')` | Check that a value is not `false`
`null($value, $message = '')` | Check that a value is `null`
`notNull($value, $message = '')` | Check that a value is not `null`
`isEmpty($value, $message = '')` | Check that a value is `empty()`
`notEmpty($value, $message = '')` | Check that a value is not `empty()`
`eq($value, $value2, $message = '')` | Check that a value equals another (`==`)
`notEq($value, $value2, $message = '')` | Check that a value does not equal another (`!=`)
`same($value, $value2, $message = '')` | Check that a value is identical to another (`===`)
`notSame($value, $value2, $message = '')` | Check that a value is not identical to another (`!==`)
`greaterThan($value, $value2, $message = '')` | Check that a value is greater than another
`greaterThanEq($value, $value2, $message = '')` | Check that a value is greater than or equal to another
`lessThan($value, $value2, $message = '')` | Check that a value is less than another
`lessThanEq($value, $value2, $message = '')` | Check that a value is less than or equal to another
`range($value, $min, $max, $message = '')` | Check that a value is within a range
`inArray($value, array $values, $message = '')` | Check that a value is one of a list of values
`notInArray($value, array $values, $message = '')` | Check that a value is not one of a list of values
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values (alias of `inArray`)
`notOneOf($value, array $values, $message = '')` | Check that a value is not one of a list of values (alias of `notInArray`)

### String Assertions

Expand Down
39 changes: 39 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,45 @@ public static function inArray($value, array $values, $message = '')
}
}

/**
* A more human-readable alias of Assert::notInArray().
*
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function notOneOf($value, array $values, $message = '')
{
static::notInArray($value, $values, $message);
}

/**
* Does strict comparison, so Assert::notInArray(3, [1, 2, 3]) will not pass
* the assertion, but Assert::notInArray(3, ['3']) will.
*
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function notInArray($value, array $values, $message = '')
{
if (\in_array($value, $values, true)) {
static::reportInvalidArgument(\sprintf(
$message ?: '%2$s was not expected to contain a value. Got: %s',
static::valueToString($value),
\implode(', ', \array_map(array('static', 'valueToString'), $values))
));
}
}

/**
* @psalm-pure
*
Expand Down
72 changes: 72 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,78 @@ public static function allInArray($value, $values, $message = '')
}
}

/**
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function nullOrNotOneOf($value, $values, $message = '')
{
null === $value || static::notOneOf($value, $values, $message);
}

/**
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function allNotOneOf($value, $values, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
static::notOneOf($entry, $values, $message);
}
}

/**
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function nullOrNotInArray($value, $values, $message = '')
{
null === $value || static::notInArray($value, $values, $message);
}

/**
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*
* @return void
*/
public static function allNotInArray($value, $values, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
static::notInArray($entry, $values, $message);
}
}

/**
* @psalm-pure
*
Expand Down
4 changes: 4 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ public function getTests()
array('range', array(3, 1, 2), false),
array('oneOf', array(1, array(1, 2, 3)), true),
array('oneOf', array(1, array('1', '2', '3')), false),
array('notOneOf', array(1, array(1, 2, 3)), false),
array('notOneOf', array(1, array('1', '2', '3')), true),
array('inArray', array(1, array(1, 2, 3)), true),
array('inArray', array(1, array('1', '2', '3')), false),
array('notInArray', array(1, array(1, 2, 3)), false),
array('notInArray', array(1, array('1', '2', '3')), true),
array('contains', array('abcd', 'ab'), true),
array('contains', array('abcd', 'bc'), true),
array('contains', array('abcd', 'cd'), true),
Expand Down
47 changes: 47 additions & 0 deletions tests/static-analysis/assert-notInArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param mixed $value
*
* @return mixed
*/
function notInArray($value, array $values)
{
Assert::notInArray($value, $values);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return mixed
*/
function nullOrNotInArray($value, array $values)
{
Assert::nullOrNotInArray($value, $values);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return mixed
*/
function allNotInArray($value, array $values)
{
Assert::allNotInArray($value, $values);

return $value;
}
48 changes: 48 additions & 0 deletions tests/static-analysis/assert-notOneOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param mixed $value
*
* @return mixed
*/
function notOneOf($value, array $values)
{
Assert::notOneOf($value, $values);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return mixed
*/
function nullOrNotOneOf($value, array $values)
{
Assert::nullOrNotOneOf($value, $values);

return $value;
}

/**
* @psalm-pure
*
* @param mixed $value
*
* @return mixed
*/
function allNotOneOf($value, array $values)
{
Assert::allNotOneOf($value, $values);

return $value;
}