@@ -22,6 +22,11 @@ final class UuidFilter
22
22
*/
23
23
const FILTER_ERROR_FORMAT = "Value '%s' is not a valid UUID. Versions checked (%s) " ;
24
24
25
+ /**
26
+ * @var string
27
+ */
28
+ const NIL_NOT_ALLOWED_ERROR_FORMAT = "Value '%s' is nil uuid, but nil values are not allowed. " ;
29
+
25
30
/**
26
31
* @var string
27
32
*/
@@ -32,6 +37,11 @@ final class UuidFilter
32
37
*/
33
38
const UNSUPPORTED_VERSION_ERROR_FORMAT = 'Filter does not support UUID v%d ' ;
34
39
40
+ /**
41
+ * @var string
42
+ */
43
+ const NIL_UUID = '00000000-0000-0000-0000-000000000000 ' ;
44
+
35
45
/**
36
46
* @var array
37
47
* @internal
@@ -44,6 +54,7 @@ final class UuidFilter
44
54
*
45
55
* @param string|null $value The value to be filtered.
46
56
* @param bool $allowNull Flag to allow value to be null.
57
+ * @param bool $allowNil Flag to allow value to be a NIL UUID.
47
58
* @param array $versions List of specific UUID version to validate against.
48
59
*
49
60
* @return string|null
@@ -53,12 +64,17 @@ final class UuidFilter
53
64
public static function filter (
54
65
string $ value = null ,
55
66
bool $ allowNull = false ,
67
+ bool $ allowNil = false ,
56
68
array $ versions = self ::VALID_UUID_VERSIONS
57
69
) {
58
70
if (self ::valueIsNullAndValid ($ allowNull , $ value )) {
59
71
return null ;
60
72
}
61
73
74
+ if (self ::valueIsNilAndValid ($ allowNil , $ value )) {
75
+ return self ::NIL_UUID ;
76
+ }
77
+
62
78
self ::validateVersions ($ versions );
63
79
foreach ($ versions as $ version ) {
64
80
$ pattern = sprintf (self ::UUID_PATTERN_FORMAT , $ version );
@@ -85,6 +101,15 @@ private static function valueIsNullAndValid(bool $allowNull, string $value = nul
85
101
return $ allowNull === true && $ value === null ;
86
102
}
87
103
104
+ private static function valueIsNilAndValid (bool $ allowNil , string $ value = null ): bool
105
+ {
106
+ if ($ allowNil === false && $ value === self ::NIL_UUID ) {
107
+ throw new FilterException (sprintf (self ::NIL_NOT_ALLOWED_ERROR_FORMAT , $ value ));
108
+ }
109
+
110
+ return $ allowNil === true && $ value === self ::NIL_UUID ;
111
+ }
112
+
88
113
private static function validateVersions (array $ versions )
89
114
{
90
115
foreach ($ versions as $ version ) {
0 commit comments