Skip to content

Commit 51825de

Browse files
committed
feat: global config
1 parent e8078fb commit 51825de

File tree

10 files changed

+167
-15
lines changed

10 files changed

+167
-15
lines changed

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,12 @@
3737
"email": "gui.allegret@gmail.com"
3838
}
3939
],
40+
"extra": {
41+
"laravel": {
42+
"providers": [
43+
"Ark4ne\\JsonApi\\Providers\\LaravelJsonApiProvider"
44+
]
45+
}
46+
},
4047
"minimum-stability": "dev"
4148
}

config/jsonapi.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Describer
7+
|--------------------------------------------------------------------------
8+
|
9+
| Config for described notation
10+
|
11+
*/
12+
'describer' => [
13+
/*
14+
|--------------------------------------------------------------------------
15+
| Nullable
16+
|--------------------------------------------------------------------------
17+
|
18+
| Value nullable by default.
19+
|
20+
*/
21+
'nullable' => true,
22+
23+
/*
24+
|--------------------------------------------------------------------------
25+
| Date format
26+
|--------------------------------------------------------------------------
27+
|
28+
| Default date format.
29+
|
30+
*/
31+
'date' => DateTimeInterface::ATOM,
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Decimal precision
36+
|--------------------------------------------------------------------------
37+
|
38+
| Decimal precision for float value.
39+
| 'null' for disable.
40+
|
41+
*/
42+
'precision' => null,
43+
],
44+
45+
'relationship' => [
46+
/*
47+
|--------------------------------------------------------------------------
48+
| When Included
49+
|--------------------------------------------------------------------------
50+
|
51+
| @see whenIncluded()
52+
|
53+
| false => relationship data will always be loaded and set to relation data
54+
| true => relationship data will not be loaded until relationship is set to include
55+
|
56+
*/
57+
'when-included' => false,
58+
],
59+
];

readme.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ A Lightweight [{JSON:API}](https://jsonapi.org/) Resource for Laravel.
1111
composer require ark4ne/laravel-json-api
1212
```
1313

14+
# Config
15+
| Path | Type | Description |
16+
|------------------------------|--------------------------|-----------------------------------------------------------------------------------------|
17+
| `describer.nullable` | `bool` | For describer notation, defined if a value is nullable by default. |
18+
| `describer.date` | `string` datetime format | For describer notation, defined default date time format. |
19+
| `describer.precision` | `int` \ `null` | For describer notation, decimal precision for float value. `null` for disable rounding. |
20+
| `relationship.when-included` | `bool` | Allow to disabled by default the loading of relationship data. |
21+
1422
# Usage
15-
This package is an specialisation of Laravel's `JsonResource` class.
23+
This package is a specialisation of Laravel's `JsonResource` class.
1624
All the underlying API's are still there, thus in your controller you can still interact
1725
with `JsonApiResource` classes as you would with the base `JsonResource` class
1826

@@ -42,7 +50,7 @@ class UserFetchRequest extends FormRequest
4250
}
4351
```
4452

45-
`Rules\Includes` will validate the include to exactly match the UserResource schema (determined by the relationships).
53+
`Rules\Includes` will validate the `include` to exactly match the UserResource schema (determined by the relationships).
4654

4755

4856
### Fields validation
@@ -62,7 +70,7 @@ class UserFetchRequest extends FormRequest
6270
}
6371
```
6472

65-
`Rules\Fields` will validate the fields to exactly match the UserResource schema (determined by the attributes and relationships).
73+
`Rules\Fields` will validate the `fields` to exactly match the UserResource schema (determined by the attributes and relationships).
6674

6775

6876
### Customize validation message
@@ -381,13 +389,15 @@ UserResource::collection(User::all()); // => JsonApiCollection
381389
## Described notation
382390

383391
### Value methods
384-
| Method | Description |
385-
|-----------|--------------------------|
386-
| `bool` | Cast to boolean |
387-
| `integer` | Cast to integer |
388-
| `float` | Cast to float |
389-
| `array` | Cast to array |
390-
| `mixed` | Don't cast, return as is |
392+
| Method | Description |
393+
|-----------|------------------------------------------|
394+
| `bool` | Cast to boolean |
395+
| `integer` | Cast to integer |
396+
| `float` | Cast to float |
397+
| `string` | Cast to string |
398+
| `date` | Cast to date, allow to use custom format |
399+
| `array` | Cast to array |
400+
| `mixed` | Don't cast, return as is |
391401

392402
### Relation methods
393403
| Method | Description |

src/Descriptors/Relations/Relation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Ark4ne\JsonApi\Descriptors\Describer;
66
use Ark4ne\JsonApi\Resources\Relationship;
7+
use Ark4ne\JsonApi\Support\Config;
78
use Closure;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Http\Request;
@@ -17,7 +18,7 @@ abstract class Relation extends Describer
1718
{
1819
protected ?Closure $links = null;
1920
protected ?Closure $meta = null;
20-
protected bool $whenIncluded = false;
21+
protected bool $whenIncluded;
2122

2223
/**
2324
* @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $related
@@ -27,6 +28,7 @@ public function __construct(
2728
protected string $related,
2829
protected null|string|Closure $relation
2930
) {
31+
$this->whenIncluded = Config::$whenIncluded;
3032
}
3133

3234
/**

src/Descriptors/Values/Value.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

55
use Ark4ne\JsonApi\Descriptors\Describer;
6+
use Ark4ne\JsonApi\Support\Config;
67
use Ark4ne\JsonApi\Support\Fields;
78
use Closure;
89
use Illuminate\Database\Eloquent\Model;
@@ -14,11 +15,12 @@
1415
*/
1516
abstract class Value extends Describer
1617
{
17-
protected bool $nullable = true;
18+
protected bool $nullable;
1819

1920
public function __construct(
2021
protected null|string|Closure $attribute
2122
) {
23+
$this->nullable = Config::$nullable;
2224
}
2325

2426
public function retriever(): string|Closure|null

src/Descriptors/Values/ValueDate.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5+
use Ark4ne\JsonApi\Support\Config;
6+
use Closure;
57
use DateTime;
68
use DateTimeInterface;
79

@@ -11,7 +13,14 @@
1113
*/
1214
class ValueDate extends Value
1315
{
14-
protected string $format = DateTimeInterface::ATOM;
16+
protected string $format;
17+
18+
public function __construct(string|Closure|null $attribute)
19+
{
20+
parent::__construct($attribute);
21+
22+
$this->format = Config::$date;
23+
}
1524

1625
public function format(string $format): static
1726
{
@@ -24,7 +33,7 @@ public function format(string $format): static
2433
*/
2534
public function value(mixed $of): string
2635
{
27-
if($of === null) {
36+
if ($of === null) {
2837
return (new DateTime("@0"))->format($this->format);
2938
}
3039
if ($of instanceof DateTimeInterface) {

src/Descriptors/Values/ValueFloat.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
namespace Ark4ne\JsonApi\Descriptors\Values;
44

5+
use Ark4ne\JsonApi\Support\Config;
6+
use Closure;
7+
58
/**
69
* @template T as \Illuminate\Database\Eloquent\Model
710
* @extends Value<T>
811
*/
912
class ValueFloat extends Value
1013
{
11-
protected int $precision;
14+
protected int|null $precision;
15+
16+
public function __construct(string|Closure|null $attribute)
17+
{
18+
parent::__construct($attribute);
19+
20+
$this->precision = Config::$precision;
21+
}
1222

1323
public function precision(int $precision): static
1424
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Providers;
4+
5+
use Ark4ne\JsonApi\Support\Config;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class LaravelJsonApiProvider extends ServiceProvider
9+
{
10+
public function register(): void
11+
{
12+
$this->publishes([
13+
__DIR__ . '/../../config/jsonapi.php' => config_path('jsonapi.php')
14+
], 'config');
15+
}
16+
17+
public function boot(): void
18+
{
19+
Config::boot();
20+
}
21+
}

src/Support/Config.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Support;
4+
5+
use DateTimeInterface;
6+
7+
class Config
8+
{
9+
public static bool $nullable = true;
10+
11+
public static string $date = DateTimeInterface::ATOM;
12+
13+
public static bool $whenIncluded = false;
14+
15+
public static int|null $precision = null;
16+
17+
public static function boot(): void
18+
{
19+
self::$nullable = config('jsonapi.describer.nullable', self::$nullable);
20+
self::$date = config('jsonapi.describer.date', self::$date);
21+
self::$precision = config('jsonapi.describer.precision', self::$precision);
22+
self::$whenIncluded = config('jsonapi.relationship.when-included', self::$whenIncluded);
23+
}
24+
}

tests/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Test;
44

5+
use Ark4ne\JsonApi\Providers\LaravelJsonApiProvider;
56
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Orchestra\Testbench\TestCase as BaseTestCase;
78

89
class TestCase extends BaseTestCase
910
{
1011
use RefreshDatabase;
12+
13+
protected function getPackageProviders($app): array
14+
{
15+
return [
16+
LaravelJsonApiProvider::class,
17+
];
18+
}
1119
}

0 commit comments

Comments
 (0)