From 15bed71bc6ff1a33d501fecdac10eddda79db1b0 Mon Sep 17 00:00:00 2001 From: Bert Ramakers Date: Fri, 25 Aug 2023 10:11:58 +0200 Subject: [PATCH 1/5] Introduce new defaultLiteral() method for fields --- src/Schema/Concerns/SetsValue.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Schema/Concerns/SetsValue.php b/src/Schema/Concerns/SetsValue.php index d31b775..ea72727 100644 --- a/src/Schema/Concerns/SetsValue.php +++ b/src/Schema/Concerns/SetsValue.php @@ -49,7 +49,7 @@ public function required(bool $required = true): static } /** - * Set a default value for this field. + * Set a default value for this field via a callback. */ public function default(?Closure $default): static { @@ -58,6 +58,16 @@ public function default(?Closure $default): static return $this; } + /** + * Set a literal default value for this field. + */ + public function defaultLiteral(mixed $defaultLiteral): static + { + $this->default(fn(): mixed => $defaultLiteral); + + return $this; + } + /** * Apply a transformation to the value upon deserialization. */ From e87140f9f788fa014ba16860178e2a96f280aef4 Mon Sep 17 00:00:00 2001 From: Bert Ramakers Date: Fri, 25 Aug 2023 10:12:20 +0200 Subject: [PATCH 2/5] Add test for defaultLiteral() --- tests/feature/FieldDefaultTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/feature/FieldDefaultTest.php b/tests/feature/FieldDefaultTest.php index 4592fbe..fd98a6e 100644 --- a/tests/feature/FieldDefaultTest.php +++ b/tests/feature/FieldDefaultTest.php @@ -43,6 +43,32 @@ public function test_default_value_used_if_field_not_present() ); } + public function test_default_literal_value_used_if_field_not_present() + { + $this->api->resource( + new MockResource( + 'users', + endpoints: [Create::make()], + fields: [ + Attribute::make('name') + ->writable() + ->defaultLiteral('default'), + ], + ), + ); + + $response = $this->api->handle( + $this->buildRequest('POST', '/users')->withParsedBody([ + 'data' => ['type' => 'users'], + ]), + ); + + $this->assertJsonApiDocumentSubset( + ['data' => ['attributes' => ['name' => 'default']]], + $response->getBody(), + ); + } + public function test_default_value_not_used_if_field_present() { $this->api->resource( From acac6b6559139fd689026ec77d39034bc72ddd67 Mon Sep 17 00:00:00 2001 From: Bert Ramakers Date: Fri, 25 Aug 2023 10:12:38 +0200 Subject: [PATCH 3/5] Document new defaultLiteral() method and Context argument for default() callback --- docs/fields.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/fields.md b/docs/fields.md index e5160bb..9f85f61 100644 --- a/docs/fields.md +++ b/docs/fields.md @@ -201,12 +201,22 @@ Str::make('email')->writableOnCreate(); If you would like to provide a default value to be used when creating a new resource if there is no value provided in the request, you can pass a closure to -the `default` method: +the `default` method. + +The closure will receive the current request context as an argument when called. + +```php +use Tobyz\JsonApiServer\Field\DateTime; + +DateTime::make('joinedAt')->default(fn(Context $context) => new \DateTime()); +``` + +Alternatively, you can pass a literal default value to the `defaultLiteral` method: ```php use Tobyz\JsonApiServer\Field\DateTime; -DateTime::make('joinedAt')->default(fn() => new \DateTime()); +Str::make('color')->defaultLiteral('blue'); ``` ### Required From 122e8c256418c8ea044fe55c8fc1ddb241c25ef4 Mon Sep 17 00:00:00 2001 From: bertramakers Date: Fri, 25 Aug 2023 08:13:30 +0000 Subject: [PATCH 4/5] Run Prettier --- docs/fields.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/fields.md b/docs/fields.md index 9f85f61..9298f55 100644 --- a/docs/fields.md +++ b/docs/fields.md @@ -201,7 +201,7 @@ Str::make('email')->writableOnCreate(); If you would like to provide a default value to be used when creating a new resource if there is no value provided in the request, you can pass a closure to -the `default` method. +the `default` method. The closure will receive the current request context as an argument when called. @@ -211,7 +211,8 @@ use Tobyz\JsonApiServer\Field\DateTime; DateTime::make('joinedAt')->default(fn(Context $context) => new \DateTime()); ``` -Alternatively, you can pass a literal default value to the `defaultLiteral` method: +Alternatively, you can pass a literal default value to the `defaultLiteral` +method: ```php use Tobyz\JsonApiServer\Field\DateTime; From 7af483ea7acbdd58afabdc312f3f98f5bff75b00 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 24 Sep 2023 16:38:05 +0930 Subject: [PATCH 5/5] Merge into single default method --- docs/fields.md | 22 ++++++++-------------- src/Schema/Concerns/SetsValue.php | 18 ++++++------------ tests/feature/FieldDefaultTest.php | 4 ++-- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/docs/fields.md b/docs/fields.md index 9298f55..44630b2 100644 --- a/docs/fields.md +++ b/docs/fields.md @@ -200,24 +200,18 @@ Str::make('email')->writableOnCreate(); ### Default Values If you would like to provide a default value to be used when creating a new -resource if there is no value provided in the request, you can pass a closure to -the `default` method. +resource if there is no value provided in the request, you can pass a closure or +a literal value to the `default` method. -The closure will receive the current request context as an argument when called. +A closure will receive the current request context as an argument when called. ```php -use Tobyz\JsonApiServer\Field\DateTime; - -DateTime::make('joinedAt')->default(fn(Context $context) => new \DateTime()); -``` - -Alternatively, you can pass a literal default value to the `defaultLiteral` -method: - -```php -use Tobyz\JsonApiServer\Field\DateTime; +use Tobyz\JsonApiServer\Field; -Str::make('color')->defaultLiteral('blue'); +Field\Str::make('name')->default('Anonymous'); +Field\DateTime::make('joinedAt')->default( + fn(Context $context) => new \DateTime(), +); ``` ### Required diff --git a/src/Schema/Concerns/SetsValue.php b/src/Schema/Concerns/SetsValue.php index ea72727..12384c4 100644 --- a/src/Schema/Concerns/SetsValue.php +++ b/src/Schema/Concerns/SetsValue.php @@ -49,21 +49,15 @@ public function required(bool $required = true): static } /** - * Set a default value for this field via a callback. + * Set a default value for this field. */ - public function default(?Closure $default): static + public function default(mixed $default): static { - $this->default = $default; - - return $this; - } + if (!$default instanceof Closure) { + $default = fn() => $default; + } - /** - * Set a literal default value for this field. - */ - public function defaultLiteral(mixed $defaultLiteral): static - { - $this->default(fn(): mixed => $defaultLiteral); + $this->default = $default; return $this; } diff --git a/tests/feature/FieldDefaultTest.php b/tests/feature/FieldDefaultTest.php index fd98a6e..0aa1a38 100644 --- a/tests/feature/FieldDefaultTest.php +++ b/tests/feature/FieldDefaultTest.php @@ -17,7 +17,7 @@ public function setUp(): void $this->api = new JsonApi(); } - public function test_default_value_used_if_field_not_present() + public function test_default_closure_value_used_if_field_not_present() { $this->api->resource( new MockResource( @@ -52,7 +52,7 @@ public function test_default_literal_value_used_if_field_not_present() fields: [ Attribute::make('name') ->writable() - ->defaultLiteral('default'), + ->default('default'), ], ), );