Skip to content

Commit

Permalink
允许 FieldTypeTransform 无法转换时返回 null
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Aug 17, 2024
1 parent d68e185 commit 23f01ce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/model/contracts/FieldTypeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

interface FieldTypeTransform
{
public static function get(mixed $value, Model $model): static;
public static function get(mixed $value, Model $model): ?static;

/**
* @return static|mixed
Expand Down
18 changes: 17 additions & 1 deletion tests/orm/ModelFieldTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ModelFieldTypeTest extends TestCase
public static function setUpBeforeClass(): void
{
Db::execute('DROP TABLE IF EXISTS `test_field_type`;');
Db::execute(<<<SQL
Db::execute(
<<<SQL
CREATE TABLE `test_field_type` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`t_json` json DEFAULT NULL,
Expand Down Expand Up @@ -65,4 +66,19 @@ public function testFieldReadAndWrite()
$this->assertEquals((string) new TestFieldPhpDTO(40, 'eee'), (string) $result->t_php);
$this->assertEquals($result->id, $result->t_php->getId());
}

/**
* @depends testFieldTypeSelect
*/
public function testFieldReadInvalid()
{

$model = new FieldTypeModel([
'id' => 1,
't_json' => '???Invalid',
't_php' => '???Invalid',
]);
$this->assertNull($model->t_json);
$this->assertNull($model->t_php);
}
}
8 changes: 5 additions & 3 deletions tests/stubs/TestFieldJsonDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public function __construct(
{
}

public static function fromData(array|string $data): static
public static function fromData(array|string $data): ?static
{
if (is_string($data)) {
$data = json_decode($data, true);
if (empty($data)) {
return null;
}
}
return new self(...$data);
}
Expand All @@ -33,7 +36,7 @@ public function __toString(): string
return json_encode($this);
}

public static function get(mixed $value, Model $model): static
public static function get(mixed $value, Model $model): ?static
{
return static::fromData($value);
}
Expand All @@ -43,4 +46,3 @@ public static function set($value, $model): string
return (string) $value;
}
}

15 changes: 11 additions & 4 deletions tests/stubs/TestFieldPhpDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ public function getId(): ?int
return $this->id;
}

public static function fromData(string $data): static
public static function fromData(string $data): ?static
{
return unserialize($data);
$result = @unserialize($data);
if (empty($result)) {
return null;
}

return $result;
}

public function __serialize(): array
Expand All @@ -44,9 +49,12 @@ public function __toString(): string
return serialize($this);
}

public static function get(mixed $value, Model $model): static
public static function get(mixed $value, Model $model): ?static
{
$d = static::fromData($value);
if (empty($d)) {
return null;
}
$d->id = $model->getData('id');
return $d;
}
Expand All @@ -56,4 +64,3 @@ public static function set($value, $model): string
return (string) $value;
}
}

0 comments on commit 23f01ce

Please sign in to comment.