-
Notifications
You must be signed in to change notification settings - Fork 1
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
11 changed files
with
327 additions
and
28 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
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
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/Math. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/Math | ||
* 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\Math\Laravel\Exception; | ||
|
||
use fab2s\ContextException\ContextException; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class NotNullableException extends ContextException | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/Math. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/Math | ||
* 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\Math\Laravel; | ||
|
||
use fab2s\Math\Laravel\Exception\NotNullableException; | ||
use fab2s\Math\Math; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class MathCast implements CastsAttributes | ||
{ | ||
protected bool $isNullable = false; | ||
|
||
public function __construct(...$options) | ||
{ | ||
$this->isNullable = in_array('nullable', $options); | ||
} | ||
|
||
/** | ||
* Cast the given value. | ||
* | ||
* @param Model $model | ||
* | ||
* @throws NotNullableException | ||
*/ | ||
public function get($model, string $key, $value, array $attributes): ?Math | ||
{ | ||
return Math::isNumber($value) ? Math::number($value) : $this->handleNullable($model, $key); | ||
} | ||
|
||
/** | ||
* Prepare the given value for storage. | ||
* | ||
* @param Model $model | ||
* | ||
* @throws NotNullableException | ||
*/ | ||
public function set($model, string $key, $value, array $attributes): ?string | ||
{ | ||
return Math::isNumber($value) ? (string) Math::number($value) : $this->handleNullable($model, $key); | ||
} | ||
|
||
/** | ||
* @throws NotNullableException | ||
*/ | ||
protected function handleNullable(Model $model, string $key) | ||
{ | ||
return $this->isNullable ? null : throw NotNullableException::make($key, $model); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/Math. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/Math | ||
* 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\Math\Tests\Laravel\Artifacts; | ||
|
||
use fab2s\Math\Laravel\MathCast; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class CastModel extends Model | ||
{ | ||
protected $table = 'table'; | ||
protected $guarded = []; | ||
protected $casts = [ | ||
'not_nullable' => MathCast::class, | ||
'nullable' => MathCast::class . ':nullable', | ||
]; | ||
} |
Oops, something went wrong.