-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,4 @@ jobs: | |
with: | ||
files: ./coverage.xml | ||
flags: unittests | ||
name: codecov-Dt0 | ||
name: codecov-laravel-dt0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/laravel-dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Laravel\Casts; | ||
|
||
use fab2s\Dt0\Exception\Dt0Exception; | ||
use fab2s\Dt0\Laravel\Dt0; | ||
use fab2s\Dt0\Laravel\Exceptions\NotNullableException; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
use JsonException; | ||
|
||
class Dt0Cast implements CastsAttributes | ||
{ | ||
/** | ||
* @var class-string<Dt0> | ||
*/ | ||
protected string $dt0Class; | ||
protected bool $isNullable = false; | ||
|
||
/** | ||
* @param class-string<Dt0> $enumClass | ||
* @param string[] ...$options | ||
*/ | ||
public function __construct(string $enumClass, ...$options) | ||
{ | ||
$this->dt0Class = $enumClass; | ||
|
||
$this->isNullable = in_array('nullable', $options); | ||
} | ||
|
||
/** | ||
* Cast the given value. | ||
* | ||
* @param Model $model | ||
* | ||
* @throws NotNullableException | ||
* @throws JsonException | ||
* @throws Dt0Exception | ||
*/ | ||
public function get($model, string $key, $value, array $attributes): ?Dt0 | ||
{ | ||
return $this->getDt0($model, $key, $value); | ||
} | ||
|
||
/** | ||
* Prepare the given value for storage. | ||
* | ||
* @param Model $model | ||
* | ||
* @throws Dt0Exception | ||
* @throws NotNullableException | ||
* @throws JsonException | ||
*/ | ||
public function set($model, string $key, $value, array $attributes): ?string | ||
{ | ||
return $this->getDt0($model, $key, $value)?->toJson(); | ||
} | ||
|
||
/** | ||
* @throws Dt0Exception | ||
* @throws NotNullableException | ||
* @throws JsonException | ||
*/ | ||
protected function getDt0(Model $model, string $key, mixed $value): ?Dt0 | ||
{ | ||
if ($value === null) { | ||
return $this->isNullable ? null : throw NotNullableException::make($key, $model); | ||
} | ||
|
||
return $this->dt0Class::from($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/laravel-dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Laravel; | ||
|
||
use fab2s\Dt0\Dt0 as BaseDt0; | ||
use fab2s\Dt0\Laravel\Casts\Dt0Cast; | ||
use Illuminate\Contracts\Database\Eloquent\Castable; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
|
||
abstract class Dt0 extends BaseDt0 implements Arrayable, Castable | ||
{ | ||
public static function castUsing(array $arguments): CastsAttributes | ||
{ | ||
return new Dt0Cast(static::class, ...$arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/laravel-dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Laravel\Exceptions; | ||
|
||
use fab2s\Dt0\Exception\Dt0Exception; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class NotNullableException extends Dt0Exception | ||
{ | ||
public static function make(string $field, Model $model): self | ||
{ | ||
$modelClass = get_class($model); | ||
|
||
return (new self("Field {$field} is not nullable in model {$modelClass}")) | ||
->setContext([ | ||
'model' => $modelClass, | ||
'data' => $model->toArray(), | ||
]) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters