Skip to content

Commit c601dc9

Browse files
committed
Merge branch 'release/3.1.0'
2 parents a1a0440 + 6c6c375 commit c601dc9

13 files changed

Lines changed: 341 additions & 120 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
## [3.1.0] - 2025-02-15
9+
10+
### Added
11+
12+
- Can now provide an enum as the type of the GUID identifier.
13+
- Can now provide multiple types when checking if a GUID is of a type - the `Guid::isType()` method. It returns `true`
14+
if *any* of the provided types match the type of the GUID.
15+
- The `LazyListOfGuids` iterator now has an `only()` method. This can be provided a list of types, and the iterator will
16+
yield only GUIDs with a matching type.
17+
818
## [3.0.0] - 2025-01-29
919

1020
### Added
@@ -392,6 +402,8 @@ All notable changes to this project will be documented in this file. This projec
392402

393403
Initial release.
394404

405+
[3.1.0]: https://github.com/cloudcreativity/ddd-modules/compare/v3.0.0...v3.1.0
406+
395407
[3.0.0]: https://github.com/cloudcreativity/ddd-modules/compare/v3.0.0-rc.2...v3.0.0
396408

397409
[3.0.0-rc.2]: https://github.com/cloudcreativity/ddd-modules/compare/v3.0.0-rc.1...v3.0.0-rc.2

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
},
5858
"extra": {
5959
"branch-alias": {
60-
"dev-develop": "2.x-dev",
61-
"dev-next": "3.x-dev"
60+
"dev-develop": "3.x-dev",
61+
"dev-next": "4.x-dev"
6262
}
6363
},
6464
"minimum-stability": "stable",

src/Toolkit/Identifiers/Guid.php

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
namespace CloudCreativity\Modules\Toolkit\Identifiers;
1414

15+
use BackedEnum;
1516
use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier;
1617
use CloudCreativity\Modules\Toolkit\ContractException;
1718
use CloudCreativity\Modules\Toolkit\Contracts;
1819
use Ramsey\Uuid\UuidInterface;
20+
use UnitEnum;
1921

2022
final class Guid implements Identifier
2123
{
@@ -35,47 +37,47 @@ public static function from(Identifier $value): self
3537
/**
3638
* Create a GUID with an integer id.
3739
*
38-
* @param string $type
40+
* @param UnitEnum|string $type
3941
* @param int $id
40-
* @return static
42+
* @return self
4143
*/
42-
public static function fromInteger(string $type, int $id): self
44+
public static function fromInteger(UnitEnum|string $type, int $id): self
4345
{
4446
return new self($type, new IntegerId($id));
4547
}
4648

4749
/**
4850
* Create a GUID with a string id.
4951
*
50-
* @param string $type
52+
* @param UnitEnum|string $type
5153
* @param string $id
52-
* @return static
54+
* @return self
5355
*/
54-
public static function fromString(string $type, string $id): self
56+
public static function fromString(UnitEnum|string $type, string $id): self
5557
{
5658
return new self($type, new StringId($id));
5759
}
5860

5961
/**
6062
* Create a GUID for a UUID.
6163
*
62-
* @param string $type
64+
* @param UnitEnum|string $type
6365
* @param UuidInterface|string $uuid
6466
* @return self
6567
*/
66-
public static function fromUuid(string $type, UuidInterface|string $uuid): self
68+
public static function fromUuid(UnitEnum|string $type, UuidInterface|string $uuid): self
6769
{
6870
return new self($type, Uuid::from($uuid));
6971
}
7072

7173
/**
7274
* Create a GUID.
7375
*
74-
* @param string $type
76+
* @param UnitEnum|string $type
7577
* @param string|int $id
7678
* @return self
7779
*/
78-
public static function make(string $type, string|int $id): self
80+
public static function make(UnitEnum|string $type, string|int $id): self
7981
{
8082
if (is_int($id)) {
8183
return self::fromInteger($type, $id);
@@ -87,11 +89,11 @@ public static function make(string $type, string|int $id): self
8789
/**
8890
* Guid constructor.
8991
*
90-
* @param string $type
92+
* @param UnitEnum|string $type
9193
* @param StringId|IntegerId|Uuid $id
9294
*/
9395
public function __construct(
94-
public readonly string $type,
96+
public readonly UnitEnum|string $type,
9597
public readonly StringId|IntegerId|Uuid $id,
9698
) {
9799
Contracts::assert(!empty($this->type), 'Type must be a non-empty string.');
@@ -106,12 +108,18 @@ public function __toString(): string
106108
}
107109

108110
/**
109-
* @param string $type
111+
* @param UnitEnum|string ...$types
110112
* @return bool
111113
*/
112-
public function isType(string $type): bool
114+
public function isType(UnitEnum|string ...$types): bool
113115
{
114-
return $this->type === $type;
116+
foreach ($types as $type) {
117+
if ($this->type === $type) {
118+
return true;
119+
}
120+
}
121+
122+
return false;
115123
}
116124

117125
/**
@@ -149,7 +157,9 @@ public function equals(self $other): bool
149157
*/
150158
public function toString(string $glue = ':'): string
151159
{
152-
return "{$this->type}{$glue}{$this->id->value}";
160+
$type = $this->type();
161+
162+
return "{$type}{$glue}{$this->id->value}";
153163
}
154164

155165
/**
@@ -166,26 +176,44 @@ public function key(): string
166176
public function context(): array
167177
{
168178
return [
169-
'type' => $this->type,
179+
'type' => $this->type(),
170180
'id' => $this->id->context(),
171181
];
172182
}
173183

174184
/**
175185
* Assert that this GUID is of the expected type.
176186
*
177-
* @param string $expected
187+
* @param UnitEnum|string $expected
178188
* @param string $message
179189
* @return $this
180190
*/
181-
public function assertType(string $expected, string $message = ''): self
191+
public function assertType(UnitEnum|string $expected, string $message = ''): self
182192
{
183193
Contracts::assert($this->type === $expected, $message ?: sprintf(
184194
'Expecting type "%s", received "%s".',
185-
$expected,
186-
$this->type,
195+
match (true) {
196+
$expected instanceof BackedEnum => $expected->value,
197+
$expected instanceof UnitEnum => $expected->name,
198+
default => $expected,
199+
},
200+
$this->type(),
187201
));
188202

189203
return $this;
190204
}
205+
206+
/**
207+
* Get the type expressed as a string or an integer.
208+
*
209+
* @return string|int
210+
*/
211+
public function type(): string|int
212+
{
213+
return match (true) {
214+
$this->type instanceof BackedEnum => $this->type->value,
215+
$this->type instanceof UnitEnum => $this->type->name,
216+
default => $this->type,
217+
};
218+
}
191219
}

src/Toolkit/Identifiers/LazyListOfGuids.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
namespace CloudCreativity\Modules\Toolkit\Identifiers;
1414

15+
use BackedEnum;
1516
use Closure;
1617
use CloudCreativity\Modules\Contracts\Toolkit\Iterables\LazyList;
1718
use CloudCreativity\Modules\Toolkit\Contracts;
1819
use CloudCreativity\Modules\Toolkit\Iterables\IsLazyList;
1920
use Generator;
21+
use UnitEnum;
2022

2123
/**
2224
* @implements LazyList<Guid>
@@ -39,21 +41,42 @@ public function __construct(?Closure $source = null)
3941
/**
4042
* Ensure all GUIDs are of the expected type.
4143
*
42-
* @param string $expected
44+
* @param UnitEnum|string $expected
4345
* @param string $message
4446
* @return self
4547
*/
46-
public function ofOneType(string $expected, string $message = ''): self
48+
public function ofOneType(UnitEnum|string $expected, string $message = ''): self
4749
{
4850
return new self(function () use ($expected, $message) {
4951
foreach ($this as $guid) {
5052
Contracts::assert($guid->isType($expected), $message ?: sprintf(
5153
'Expecting GUIDs of type "%s", found "%s".',
52-
$expected,
53-
$guid->type,
54+
match (true) {
55+
$expected instanceof BackedEnum => $expected->value,
56+
$expected instanceof UnitEnum => $expected->name,
57+
default => $expected,
58+
},
59+
$guid->type(),
5460
));
5561
yield $guid;
5662
}
5763
});
5864
}
65+
66+
/**
67+
* Return a list that only contains the provided types.
68+
*
69+
* @param UnitEnum|string ...$types
70+
* @return self
71+
*/
72+
public function only(UnitEnum|string ...$types): self
73+
{
74+
return new self(function () use ($types) {
75+
foreach ($this as $guid) {
76+
if ($guid->isType(...$types)) {
77+
yield $guid;
78+
}
79+
}
80+
});
81+
}
5982
}

src/Toolkit/Iterables/IsLazyList.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ public function getIterator(): Generator
4040
}
4141

4242
/**
43-
* @return array<T>
43+
* @return list<T>
4444
*/
4545
public function all(): array
4646
{
47-
return iterator_to_array($this->getIterator());
47+
$all = [];
48+
49+
foreach ($this as $value) {
50+
$all[] = $value;
51+
}
52+
53+
return $all;
4854
}
4955
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace CloudCreativity\Modules\Tests\Unit\Toolkit\Loggable;
13+
namespace CloudCreativity\Modules\Tests;
1414

15-
enum TestEnum: string
15+
enum TestBackedEnum: string
1616
{
17-
case Foo = 'foo';
18-
case Bar = 'bar';
17+
case Foo = 'foo!';
18+
case Bar = 'bar!';
1919
}

tests/TestUnitEnum.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2025 Cloud Creativity Limited
5+
*
6+
* Use of this source code is governed by an MIT-style
7+
* license that can be found in the LICENSE file or at
8+
* https://opensource.org/licenses/MIT.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace CloudCreativity\Modules\Tests;
14+
15+
enum TestUnitEnum
16+
{
17+
case Baz;
18+
case Bat;
19+
}

0 commit comments

Comments
 (0)