Skip to content

Commit 1158d50

Browse files
committed
feat!: improve id matching methods on the id contract
1 parent f6fed2a commit 1158d50

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ All notable changes to this project will be documented in this file. This projec
99

1010
- The `once` method has been added to the server repository interface. This previously existed on the concrete class,
1111
but has now been added to the interface.
12+
- The schema `ID` contract now has a `matchAll()` method for matching multiple ids at once.
13+
14+
### Changed
15+
16+
- **BREAKING** The `match()` method on the schema `ID` contract now accepts an optional delimiter as its second
17+
argument. When receiving a delimiter, the input value can have one-to-many ids separated by the provided delimiter.
1218

1319
## Unreleased
1420

src/Contracts/Schema/ID.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,22 @@ public function pattern(): string;
3131
/**
3232
* Does the value match the pattern?
3333
*
34+
* If a delimiter is provided, the value can hold one-to-many ids separated
35+
* by the provided delimiter.
36+
*
3437
* @param string $value
38+
* @param string $delimiter
39+
* @return bool
40+
*/
41+
public function match(string $value, string $delimiter = ''): bool;
42+
43+
/**
44+
* Do all the values match the pattern?
45+
*
46+
* @param array<string> $values
3547
* @return bool
3648
*/
37-
public function match(string $value): bool;
49+
public function matchAll(array $values): bool;
3850

3951
/**
4052
* Does the resource accept client generated ids?

src/Core/Schema/Concerns/MatchesIds.php

+33-4
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,42 @@ public function matchCase(): static
7979
}
8080

8181
/**
82-
* Does the value match the ID's pattern?
82+
* Does the value match the pattern?
8383
*
84-
* @param string $resourceId
84+
* If a delimiter is provided, the value can hold one-to-many ids separated
85+
* by the provided delimiter.
86+
*
87+
* @param string $value
88+
* @param string $delimiter
89+
* @return bool
90+
*/
91+
public function match(string $value, string $delimiter = ''): bool
92+
{
93+
if (strlen($delimiter) > 0) {
94+
$delimiter = preg_quote($delimiter);
95+
return 1 === preg_match(
96+
"/^{$this->pattern}({$delimiter}{$this->pattern})*$/{$this->flags}",
97+
$value,
98+
);
99+
}
100+
101+
return 1 === preg_match("/^{$this->pattern}$/{$this->flags}", $value);
102+
}
103+
104+
/**
105+
* Do all the values match the pattern?
106+
*
107+
* @param array<string> $values
85108
* @return bool
86109
*/
87-
public function match(string $resourceId): bool
110+
public function matchAll(array $values): bool
88111
{
89-
return 1 === preg_match("/^{$this->pattern}$/{$this->flags}", $resourceId);
112+
foreach ($values as $value) {
113+
if ($this->match($value) === false) {
114+
return false;
115+
}
116+
}
117+
118+
return true;
90119
}
91120
}

tests/Unit/Schema/Concerns/MatchesIdsTest.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public function testItIsNumeric(): void
2727

2828
$this->assertSame('[0-9]+', $id->pattern());
2929
$this->assertTrue($id->match('1234'));
30+
$this->assertTrue($id->match('1234,5678,90', ','));
31+
$this->assertTrue($id->match('1234-5678-90', '-'));
32+
$this->assertTrue($id->match('1234', ','));
33+
$this->assertTrue($id->matchAll(['1234', '5678', '90']));
3034
$this->assertFalse($id->match('123A45'));
35+
$this->assertFalse($id->match('1234,567E,1234', ','));
36+
$this->assertFalse($id->matchAll(['1234', '5678', '90E']));
3137
}
3238

3339
/**
@@ -39,10 +45,21 @@ public function testItIsUuid(): void
3945
use MatchesIds;
4046
};
4147

48+
$uuids = [
49+
'1e1cc75c-dc37-488d-b862-828529088261',
50+
'fca1509e-9178-45fd-8a2b-ae819d34f7e6',
51+
'2935a487-85e1-4f3c-b585-cd64e9a776f3',
52+
];
53+
4254
$this->assertSame($id, $id->uuid());
4355
$this->assertSame('[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}', $id->pattern());
44-
$this->assertTrue($id->match('fca1509e-9178-45fd-8a2b-ae819d34f7e6'));
56+
$this->assertTrue($id->match($uuids[0]));
57+
$this->assertTrue($id->match(implode(',', $uuids), ','));
58+
$this->assertTrue($id->match($uuids[0], ','));
59+
$this->assertTrue($id->matchAll($uuids));
4560
$this->assertFalse($id->match('fca1509e917845fd8a2bae819d34f7e6'));
61+
$this->assertFalse($id->match(implode(',', $invalid = [...$uuids, 'fca1509e917845fd8a2bae819d34f7e6']), ','));
62+
$this->assertFalse($id->matchAll($invalid));
4663
}
4764

4865
/**
@@ -54,10 +71,20 @@ public function testItIsUlid(): void
5471
use MatchesIds;
5572
};
5673

74+
$ulids = [
75+
'01HT4PA8AZC8Q30ZGC5PEWZP0E',
76+
'01HT4QSVZXQX89AZNSXGYYB3PB',
77+
'01HT4QT51KE7NJ12SDS48N3CWB',
78+
];
79+
5780
$this->assertSame($id, $id->ulid());
5881
$this->assertSame('[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}', $id->pattern());
59-
$this->assertTrue($id->match('01HT4PA8AZC8Q30ZGC5PEWZP0E'));
82+
$this->assertTrue($id->match($ulids[0]));
83+
$this->assertTrue($id->match(implode(',', $ulids), ','));
84+
$this->assertTrue($id->matchAll($ulids));
6085
$this->assertFalse($id->match('01HT4PA8AZC8Q30ZGC5PEWZP0'));
86+
$this->assertFalse($id->match(implode(',', $invalid = [...$ulids, '01HT4PA8AZC8Q30ZGC5PEWZP0']), ','));
87+
$this->assertFalse($id->matchAll($invalid));
6188
}
6289

6390
/**

0 commit comments

Comments
 (0)