Skip to content

Commit f143c9b

Browse files
committed
Merge branch 'release/3.3.0'
2 parents 9eeaf27 + 90bd32e commit f143c9b

9 files changed

Lines changed: 217 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8-
## [3.2.0] - 2024-04-19
8+
## [3.3.0] - 2025-05-24
9+
10+
### Added
11+
12+
- New `enum_value` and `enum_string` helper functions for getting a scalar or string value from an enum.
13+
- The `Guid::fromUuid()` method now also accepts a `Uuid` object, so that it can be used to create a GUID from a UUID
14+
identifier. Previously it required a Ramsey UUID object.
15+
16+
## [3.2.0] - 2025-04-19
917

1018
### Added
1119

@@ -416,6 +424,8 @@ All notable changes to this project will be documented in this file. This projec
416424

417425
Initial release.
418426

427+
[3.3.0]: https://github.com/cloudcreativity/ddd-modules/compare/v3.2.0...v3.3.0
428+
419429
[3.2.0]: https://github.com/cloudcreativity/ddd-modules/compare/v3.1.0...v3.2.0
420430

421431
[3.1.0]: https://github.com/cloudcreativity/ddd-modules/compare/v3.0.0...v3.1.0

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
"phpunit/phpunit": "^10.5"
3434
},
3535
"autoload": {
36+
"files": [
37+
"src/Toolkit/functions.php"
38+
],
3639
"psr-4": {
3740
"CloudCreativity\\Modules\\": "src/"
3841
}

src/Toolkit/Identifiers/Guid.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
namespace CloudCreativity\Modules\Toolkit\Identifiers;
1414

15-
use BackedEnum;
1615
use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier;
1716
use CloudCreativity\Modules\Toolkit\ContractException;
1817
use CloudCreativity\Modules\Toolkit\Contracts;
1918
use Ramsey\Uuid\UuidInterface;
2019
use UnitEnum;
2120

21+
use function CloudCreativity\Modules\Toolkit\enum_string;
22+
use function CloudCreativity\Modules\Toolkit\enum_value;
23+
2224
final class Guid implements Identifier
2325
{
2426
/**
@@ -62,10 +64,10 @@ public static function fromString(UnitEnum|string $type, string $id): self
6264
* Create a GUID for a UUID.
6365
*
6466
* @param UnitEnum|string $type
65-
* @param UuidInterface|string $uuid
67+
* @param Uuid|UuidInterface|string $uuid
6668
* @return self
6769
*/
68-
public static function fromUuid(UnitEnum|string $type, UuidInterface|string $uuid): self
70+
public static function fromUuid(UnitEnum|string $type, Uuid|UuidInterface|string $uuid): self
6971
{
7072
return new self($type, Uuid::from($uuid));
7173
}
@@ -157,7 +159,7 @@ public function equals(self $other): bool
157159
*/
158160
public function toString(string $glue = ':'): string
159161
{
160-
$type = $this->type();
162+
$type = enum_string($this->type);
161163

162164
return "{$type}{$glue}{$this->id->value}";
163165
}
@@ -176,7 +178,7 @@ public function key(): string
176178
public function context(): array
177179
{
178180
return [
179-
'type' => $this->type(),
181+
'type' => enum_string($this->type),
180182
'id' => $this->id->context(),
181183
];
182184
}
@@ -190,14 +192,10 @@ public function context(): array
190192
*/
191193
public function assertType(UnitEnum|string $expected, string $message = ''): self
192194
{
193-
Contracts::assert($this->type === $expected, $message ?: sprintf(
195+
Contracts::assert($this->type === $expected, $message ?: fn () => sprintf(
194196
'Expecting type "%s", received "%s".',
195-
match (true) {
196-
$expected instanceof BackedEnum => $expected->value,
197-
$expected instanceof UnitEnum => $expected->name,
198-
default => $expected,
199-
},
200-
$this->type(),
197+
enum_string($expected),
198+
enum_string($this->type),
201199
));
202200

203201
return $this;
@@ -210,10 +208,7 @@ public function assertType(UnitEnum|string $expected, string $message = ''): sel
210208
*/
211209
public function type(): string|int
212210
{
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-
};
211+
// TODO 4.0 use enum_string() instead
212+
return enum_value($this->type);
218213
}
219214
}

src/Toolkit/Identifiers/LazyListOfGuids.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
namespace CloudCreativity\Modules\Toolkit\Identifiers;
1414

15-
use BackedEnum;
1615
use Closure;
1716
use CloudCreativity\Modules\Contracts\Toolkit\Iterables\LazyList;
1817
use CloudCreativity\Modules\Toolkit\Contracts;
1918
use CloudCreativity\Modules\Toolkit\Iterables\IsLazyList;
2019
use Generator;
2120
use UnitEnum;
2221

22+
use function CloudCreativity\Modules\Toolkit\enum_string;
23+
2324
/**
2425
* @implements LazyList<Guid>
2526
*/
@@ -49,14 +50,10 @@ public function ofOneType(UnitEnum|string $expected, string $message = ''): self
4950
{
5051
return new self(function () use ($expected, $message) {
5152
foreach ($this as $guid) {
52-
Contracts::assert($guid->isType($expected), $message ?: sprintf(
53+
Contracts::assert($guid->isType($expected), $message ?: static fn () => sprintf(
5354
'Expecting GUIDs of type "%s", found "%s".',
54-
match (true) {
55-
$expected instanceof BackedEnum => $expected->value,
56-
$expected instanceof UnitEnum => $expected->name,
57-
default => $expected,
58-
},
59-
$guid->type(),
55+
enum_string($expected),
56+
enum_string($guid->type),
6057
));
6158
yield $guid;
6259
}

src/Toolkit/functions.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Toolkit;
14+
15+
use BackedEnum;
16+
use UnitEnum;
17+
18+
if (!function_exists(__NAMESPACE__ . '\enum_value')) {
19+
/**
20+
* Return a scalar value for an enum.
21+
*
22+
* @param UnitEnum|string|int $value
23+
* @return string|int
24+
*/
25+
function enum_value(UnitEnum|string|int $value): string|int
26+
{
27+
return match (true) {
28+
$value instanceof BackedEnum => $value->value,
29+
$value instanceof UnitEnum => $value->name,
30+
default => $value,
31+
};
32+
}
33+
}
34+
35+
if (!function_exists(__NAMESPACE__ . '\enum_string')) {
36+
/**
37+
* Return a string value for an enum.
38+
*
39+
* @param UnitEnum|string $value
40+
* @return string
41+
*/
42+
function enum_string(UnitEnum|string $value): string
43+
{
44+
return match (true) {
45+
$value instanceof BackedEnum && is_string($value->value) => $value->value,
46+
$value instanceof UnitEnum => $value->name,
47+
default => $value,
48+
};
49+
}
50+
}

tests/TestBackedIntEnum.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 TestBackedIntEnum: int
16+
{
17+
case FooBar = 1;
18+
case BazBat = 2;
19+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Unit\Toolkit;
14+
15+
use CloudCreativity\Modules\Tests\TestBackedEnum;
16+
use CloudCreativity\Modules\Tests\TestBackedIntEnum;
17+
use CloudCreativity\Modules\Tests\TestUnitEnum;
18+
use PHPUnit\Framework\Attributes\DataProvider;
19+
use PHPUnit\Framework\TestCase;
20+
use UnitEnum;
21+
22+
use function CloudCreativity\Modules\Toolkit\enum_string;
23+
24+
class EnumStringTest extends TestCase
25+
{
26+
/**
27+
* @return array<string, array{0: UnitEnum|string|int, 1: string}>
28+
*/
29+
public static function valueProvider(): array
30+
{
31+
return [
32+
'string backed enum' => [
33+
TestBackedEnum::Foo,
34+
TestBackedEnum::Foo->value,
35+
],
36+
'int backed enum' => [
37+
TestBackedIntEnum::BazBat,
38+
TestBackedIntEnum::BazBat->name,
39+
],
40+
'unit enum' => [
41+
TestUnitEnum::Bat,
42+
TestUnitEnum::Bat->name,
43+
],
44+
'string' => [
45+
'foo',
46+
'foo',
47+
],
48+
];
49+
}
50+
51+
#[DataProvider('valueProvider')]
52+
public function testItReturnsScalarValue(UnitEnum|string $value, string $expected): void
53+
{
54+
$this->assertSame($expected, enum_string($value));
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Unit\Toolkit;
14+
15+
use CloudCreativity\Modules\Tests\TestBackedEnum;
16+
use CloudCreativity\Modules\Tests\TestBackedIntEnum;
17+
use CloudCreativity\Modules\Tests\TestUnitEnum;
18+
use PHPUnit\Framework\Attributes\DataProvider;
19+
use PHPUnit\Framework\TestCase;
20+
use UnitEnum;
21+
22+
use function CloudCreativity\Modules\Toolkit\enum_value;
23+
24+
class EnumValueTest extends TestCase
25+
{
26+
/**
27+
* @return array<string, array{0: UnitEnum|string|int, 1: string|int}>
28+
*/
29+
public static function valueProvider(): array
30+
{
31+
return [
32+
'string backed enum' => [
33+
TestBackedEnum::Foo,
34+
TestBackedEnum::Foo->value,
35+
],
36+
'int backed enum' => [
37+
TestBackedIntEnum::BazBat,
38+
TestBackedIntEnum::BazBat->value,
39+
],
40+
'unit enum' => [
41+
TestUnitEnum::Bat,
42+
TestUnitEnum::Bat->name,
43+
],
44+
'string' => [
45+
'foo',
46+
'foo',
47+
],
48+
'int' => [
49+
123,
50+
123,
51+
],
52+
];
53+
}
54+
55+
#[DataProvider('valueProvider')]
56+
public function testItReturnsScalarValue(UnitEnum|string|int $value, string|int $expected): void
57+
{
58+
$this->assertSame($expected, enum_value($value));
59+
}
60+
}

tests/Unit/Toolkit/Identifiers/GuidTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function testUuid(UnitEnum|string $type, string $value, UnitEnum|string $
119119

120120
$this->assertSame($type, $guid->type);
121121
$this->assertObjectEquals($uuid, $guid->id);
122+
$this->assertObjectEquals($guid, Guid::fromUuid($type, $uuid));
122123
$this->assertSame($value . ':' . $uuid->toString(), $guid->toString());
123124
$this->assertSame($value . ':' . $uuid->toString(), (string) $guid);
124125
$this->assertTrue($guid->isType($type));

0 commit comments

Comments
 (0)