From 567380828666c624127fefca5260447b2adffc22 Mon Sep 17 00:00:00 2001 From: twosee Date: Tue, 23 Apr 2019 14:54:20 +0800 Subject: [PATCH] Merge 3503dd9220ac1d6ae550516d18fbd9f348e8563c --- src/Assert.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Assert.php b/src/Assert.php index 3018cb50..f66f96e9 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -51,6 +51,7 @@ * @method static bool nullOrIp($value, $message = '') * @method static bool nullOrIpv4($value, $message = '') * @method static bool nullOrIpv6($value, $message = '') + * @method static bool nullOrUniqueValues($values, $message = '') * @method static bool nullOrEq($value, $expect, $message = '') * @method static bool nullOrNotEq($value, $expect, $message = '') * @method static bool nullOrSame($value, $expect, $message = '') @@ -130,6 +131,7 @@ * @method static bool allIp($values, $message = '') * @method static bool allIpv4($values, $message = '') * @method static bool allIpv6($values, $message = '') + * @method static bool allUniqueValues($values, $message = '') * @method static bool allEq($values, $expect, $message = '') * @method static bool allNotEq($values, $expect, $message = '') * @method static bool allSame($values, $expect, $message = '') @@ -544,6 +546,22 @@ public static function ipv6($value, $message = ''): bool return true; } + public static function uniqueValues(array $values, $message = ''): bool + { + $allValues = count($values); + $uniqueValues = count(array_unique($values)); + if ($allValues !== $uniqueValues) { + $difference = $allValues - $uniqueValues; + static::reportInvalidArgument(sprintf( + $message ?: 'Expected an array of unique values, but %s of them %s duplicated', + $difference, + (1 === $difference ? 'is' : 'are') + )); + return false; + } + return true; + } + public static function eq($value, $expect, $message = ''): bool { if ($expect != $value) {