Skip to content

Commit 9e603da

Browse files
committed
fix: ObjectCast implementation
But I don't know it makes sense.
1 parent 3bb61a8 commit 9e603da

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

system/Entity/Cast/ObjectCast.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ public static function toDatabase($value, array $params = [])
4040
}
4141

4242
// @TODO How to implement?
43-
return serialize($value);
43+
return serialize((array) $value);
4444
}
4545

4646
/**
4747
* {@inheritDoc}
4848
*/
49-
public static function fromDatabase($value, array $params = []): array
49+
public static function fromDatabase($value, array $params = []): object
5050
{
5151
if (! is_string($value)) {
5252
self::invalidTypeValueError($value);
5353
}
5454

5555
// @TODO How to implement?
56-
if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
57-
$value = unserialize($value);
56+
if ((strpos($value, 'a:') === 0)) {
57+
return (object) unserialize($value, ['allowed_classes' => false]);
5858
}
5959

60-
return (array) $value;
60+
self::invalidTypeValueError($value);
6161
}
6262
}

tests/system/Entity/EntityLiveTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
namespace CodeIgniter\Entity;
1313

14-
use CodeIgniter\Database\Exceptions\DatabaseException;
1514
use CodeIgniter\I18n\Time;
1615
use CodeIgniter\Model;
1716
use CodeIgniter\Test\CIUnitTestCase;
1817
use CodeIgniter\Test\DatabaseTestTrait;
1918
use Config\Services;
19+
use stdClass;
2020

2121
/**
2222
* @internal
@@ -85,8 +85,6 @@ public function testEntityReturnsValuesWithCorrectTypes()
8585
*/
8686
public function testCastObject(): void
8787
{
88-
$this->expectException(DatabaseException::class);
89-
9088
$entity = new class () extends Entity {
9189
protected $casts = [
9290
'id' => 'int',
@@ -103,5 +101,10 @@ public function testCastObject(): void
103101
};
104102
$entity->fill(['username' => 'johnsmith', 'active' => false, 'memo' => ['foo', 'bar']]);
105103
$model->save($entity);
104+
105+
$user = $model->asObject(get_class($entity))->find(1);
106+
107+
$this->assertInstanceOf(stdClass::class, $user->memo);
108+
$this->assertSame(['foo', 'bar'], (array) $user->memo);
106109
}
107110
}

0 commit comments

Comments
 (0)