Skip to content

Commit 5aeb8b2

Browse files
committed
chore: Allow identities to be null
1 parent 3d5b760 commit 5aeb8b2

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/Contracts/EntityMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function repository(): ?string;
3434
*
3535
* @phpstan-param array<string, mixed>|EntityType $data
3636
*
37-
* @return int|string
37+
* @return int|string|null
3838
*/
39-
public function identity(array|object $data): int|string;
39+
public function identity(array|object $data): int|string|null;
4040
}

src/Support/BaseEntityMapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public function repository(): ?string
5252
*
5353
* @phpstan-param array<string, mixed>|EntityType $data
5454
*
55-
* @return int|string
55+
* @return int|string|null
5656
*/
57-
public function identity(object|array $data): int|string
57+
public function identity(object|array $data): int|string|null
5858
{
5959
if (is_object($data)) {
6060
if (method_exists($data, 'hasId') && ! $data->hasId()) {
61-
throw new RuntimeException('Cannot get identity from an entity without an ID');
61+
return null;
6262
}
6363

6464
if (property_exists($data, 'id')) {
@@ -77,7 +77,7 @@ public function identity(object|array $data): int|string
7777
}
7878

7979
if (! isset($data['id'])) {
80-
throw new RuntimeException('Cannot get identity from an entity without an ID');
80+
return null;
8181
}
8282

8383
assert(is_string($data['id']) || is_int($data['id']), 'The ID must be an int or string');

0 commit comments

Comments
 (0)