Skip to content

Commit 9d69780

Browse files
committed
Make types separate from attributes
1 parent 49aa96e commit 9d69780

37 files changed

+625
-583
lines changed

src/Laravel/EloquentResource.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
use Tobyz\JsonApiServer\Resource\Paginatable;
1919
use Tobyz\JsonApiServer\Resource\Resource;
2020
use Tobyz\JsonApiServer\Resource\Updatable;
21-
use Tobyz\JsonApiServer\Schema\Field\DateTime;
21+
use Tobyz\JsonApiServer\Schema\Field\Attribute;
2222
use Tobyz\JsonApiServer\Schema\Field\Field;
2323
use Tobyz\JsonApiServer\Schema\Field\Relationship;
2424
use Tobyz\JsonApiServer\Schema\Field\ToMany;
25+
use Tobyz\JsonApiServer\Schema\Type\DateTime;
2526

2627
abstract class EloquentResource extends Resource implements
2728
Findable,
@@ -151,7 +152,11 @@ public function setValue(object $model, Field $field, mixed $value, Context $con
151152
// dates in the database. Since the API can receive dates in any
152153
// timezone, we will need to convert it to the app's configured
153154
// timezone ourselves before storage.
154-
if ($field instanceof DateTime && $value instanceof \DateTimeInterface) {
155+
if (
156+
$field instanceof Attribute &&
157+
$field->type instanceof DateTime &&
158+
$value instanceof \DateTimeInterface
159+
) {
155160
$value = \DateTime::createFromInterface($value)->setTimezone(
156161
new \DateTimeZone(config('app.timezone')),
157162
);

src/Schema/Field/Attribute.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,52 @@
22

33
namespace Tobyz\JsonApiServer\Schema\Field;
44

5+
use Tobyz\JsonApiServer\Context;
6+
use Tobyz\JsonApiServer\Schema\Type\TypeInterface;
7+
58
class Attribute extends Field
69
{
10+
public ?TypeInterface $type = null;
11+
12+
public function type(?TypeInterface $type): static
13+
{
14+
$this->type = $type;
15+
16+
return $this;
17+
}
18+
19+
public function serializeValue(mixed $value, Context $context): mixed
20+
{
21+
if ($this->nullable && $value === null) {
22+
return null;
23+
}
24+
25+
if ($this->type) {
26+
$value = $this->type->serialize($value);
27+
}
28+
29+
return parent::serializeValue($value, $context);
30+
}
31+
32+
public function deserializeValue(mixed $value, Context $context): mixed
33+
{
34+
if ($this->nullable && $value === null) {
35+
return null;
36+
}
37+
38+
if ($this->type) {
39+
$value = $this->type->deserialize($value);
40+
}
41+
42+
return parent::deserializeValue($value, $context);
43+
}
44+
45+
public function validateValue(mixed $value, callable $fail, Context $context): void
46+
{
47+
if ($value !== null && $this->type) {
48+
$this->type->validate($value, $fail);
49+
}
50+
51+
parent::validateValue($value, $fail, $context);
52+
}
753
}

src/Schema/Field/Boolean.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Schema/Field/BooleanDateTime.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
namespace Tobyz\JsonApiServer\Schema\Field;
44

55
use Tobyz\JsonApiServer\Context;
6+
use Tobyz\JsonApiServer\Schema\Type\Boolean;
67

7-
class BooleanDateTime extends Boolean
8+
class BooleanDateTime extends Attribute
89
{
10+
public function __construct(string $name)
11+
{
12+
parent::__construct($name);
13+
14+
$this->type(Boolean::make());
15+
}
16+
917
public function setValue(mixed $model, mixed $value, Context $context): void
1018
{
1119
parent::setValue($model, $value ? new \DateTime() : null, $context);

src/Schema/Field/Date.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Schema/Field/DateTime.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Schema/Field/Integer.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Schema/Field/Number.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)