Skip to content

Commit 97f76b6

Browse files
committed
fix: don't break on safe int to string (and vv) property assignments
1 parent a4a0094 commit 97f76b6

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/Models/Model.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,29 @@ public function toArray(): array
5959
*/
6060
protected static function resolvePropertyValue(string $property, $value)
6161
{
62-
$className = static::getPropertyClassName($property);
62+
$type = static::getPropertyType($property);
6363

64-
if ($className === null) {
64+
if ($type === null) {
6565
return $value;
6666
}
6767

68-
if (is_array($value) && is_a($className, self::class, true)) {
69-
return $className::fromArray($value);
68+
if ($type === 'int' && is_string($value) && is_numeric($value)) {
69+
return (int) $value;
7070
}
7171

72-
if (is_array($value) && is_a($className, ModelCollection::class, true)) {
73-
return $className::fromArray($value);
72+
if ($type === 'string' && is_int($value)) {
73+
return (string) $value;
7474
}
7575

76-
if (is_a($className, DateTimeInterface::class, true)) {
76+
if (is_array($value) && is_a($type, self::class, true)) {
77+
return $type::fromArray($value);
78+
}
79+
80+
if (is_array($value) && is_a($type, ModelCollection::class, true)) {
81+
return $type::fromArray($value);
82+
}
83+
84+
if (is_a($type, DateTimeInterface::class, true)) {
7785
if (is_int($value) || is_float($value)) {
7886
return Carbon::createFromTimestamp($value);
7987
}
@@ -86,7 +94,7 @@ protected static function resolvePropertyValue(string $property, $value)
8694
return $value;
8795
}
8896

89-
protected static function getPropertyClassName(string $property): ?string
97+
protected static function getPropertyType(string $property): ?string
9098
{
9199
$reflectionClass = new ReflectionClass(static::class);
92100
$reflectionProperty = $reflectionClass->getProperty($property);

tests/ModelTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Vazaha\Mastodon\Collections\FieldCollection;
9+
use Vazaha\Mastodon\Models\AccountModel;
10+
use Vazaha\Mastodon\Models\FieldModel;
11+
use Vazaha\Mastodon\Models\RoleModel;
12+
13+
class ModelTest extends TestCase
14+
{
15+
public function testModelsWithEncapsulatedModels(): void
16+
{
17+
$array = [
18+
'moved' => [
19+
'id' => '123',
20+
],
21+
'fields' => [
22+
[
23+
'name' => 'name1',
24+
'value' => 'value1',
25+
],
26+
],
27+
];
28+
29+
$account = AccountModel::fromArray($array);
30+
self::assertInstanceOf(FieldCollection::class, $account->fields);
31+
self::assertInstanceOf(FieldModel::class, $account->fields->first());
32+
self::assertSame('name1', $account->fields->first()->name);
33+
self::assertInstanceOf(AccountModel::class, $account->moved);
34+
self::assertSame('123', $account->moved->id);
35+
}
36+
37+
public function testAssignStringifiedIntToIntPropertyShouldNotBreak(): void
38+
{
39+
$array = [
40+
'permissions' => '1234',
41+
];
42+
$model = RoleModel::fromArray($array);
43+
self::assertSame(1234, $model->permissions);
44+
}
45+
46+
public function testAssignIntToStringPropertyShouldNotBreak(): void
47+
{
48+
$array = [
49+
'id' => 1234,
50+
];
51+
$model = RoleModel::fromArray($array);
52+
self::assertSame('1234', $model->id);
53+
}
54+
}

0 commit comments

Comments
 (0)