Skip to content

Commit 5e2fab1

Browse files
Merge pull request #78 from TheDragonCode/3.x
Fixed an error in obtaining a key name from a class without public properties
2 parents 6c78e6e + 0b73bca commit 5e2fab1

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/Concerns/Arrayable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected function toArray($value): array
7676
->map(fn ($value) => Instance::of($value, Carbon::class) ? $value->toIso8601String() : $value)
7777
->map(fn ($value) => Instance::of($value, FormRequest::class) ? $value->validated() : $value)
7878
->map(fn ($value) => Instance::of($value, BackedEnum::class) ? ($value->value ?? $value->name) : $value)
79+
->map(fn ($value) => is_object($value) ? (Arr::resolve($value) ?: get_class($value)) : $value)
7980
->resolve()
8081
->toArray();
8182
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\Simple;
6+
7+
class CustomObjectWithoutProperties
8+
{
9+
}

tests/Support/KeyTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Tests\Fixtures\Enums\WithValueEnum;
1616
use Tests\Fixtures\Models\User;
1717
use Tests\Fixtures\Simple\CustomObject;
18+
use Tests\Fixtures\Simple\CustomObjectWithoutProperties;
1819
use Tests\TestCase;
1920

2021
class KeyTest extends TestCase
@@ -103,13 +104,13 @@ public function testArrayable()
103104
$this->assertSame($expected, $key);
104105
}
105106

106-
public function testCustomObject()
107+
public function testCustomObjects()
107108
{
108-
$key = Key::get(':', [new CustomObject()]);
109+
$key1 = Key::get(':', [new CustomObject()]);
110+
$key2 = Key::get(':', [new CustomObjectWithoutProperties()]);
109111

110-
$expected = 'b58721335d52d66a9486072fe3383ccf';
111-
112-
$this->assertSame($expected, $key);
112+
$this->assertSame('b58721335d52d66a9486072fe3383ccf', $key1);
113+
$this->assertSame('9be04f864d3649e49e892638361d5d49', $key2);
113114
}
114115

115116
public function testMultiObjectArrays()

0 commit comments

Comments
 (0)