-
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
9 changed files
with
293 additions
and
22 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
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', | ||
]; | ||
} |
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,116 @@ | ||
<?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; | ||
|
||
use fab2s\Math\Laravel\Exception\NotNullableException; | ||
use fab2s\Math\Laravel\MathCast; | ||
use fab2s\Math\Math; | ||
use fab2s\Math\Tests\Laravel\Artifacts\CastModel; | ||
use Orchestra\Testbench\TestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
class MathCastTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
// Turn on error reporting | ||
error_reporting(E_ALL); | ||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @throws NotNullableException | ||
*/ | ||
#[DataProvider('castProvider')] | ||
public function test_math_cast_get( | ||
Math|string|int|float|null $value, | ||
Math|string|null $expected, | ||
array $options = [], | ||
): void { | ||
$cast = new MathCast(...$options); | ||
|
||
switch (true) { | ||
case is_object($expected): | ||
$this->assertTrue($expected->eq($cast->get(new CastModel, 'key', $value, []))); | ||
break; | ||
case is_string($expected): | ||
$this->expectException(NotNullableException::class); | ||
$cast->get(new CastModel, 'key', $value, []); | ||
break; | ||
case $expected === null: | ||
$this->assertNull($cast->get(new CastModel, 'key', $value, [])); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* @throws NotNullableException | ||
*/ | ||
#[DataProvider('castProvider')] | ||
public function test_math_cast_set( | ||
Math|string|int|float|null $value, | ||
Math|string|null $expected, | ||
array $options = [], | ||
): void { | ||
$cast = new MathCast(...$options); | ||
|
||
switch (true) { | ||
case is_object($expected): | ||
$this->assertSame((string) $expected, $cast->set(new CastModel, 'key', $value, [])); | ||
break; | ||
case is_string($expected): | ||
$this->expectException(NotNullableException::class); | ||
$cast->set(new CastModel, 'key', $value, []); | ||
break; | ||
case $expected === null: | ||
$this->assertSame(null, $cast->set(new CastModel, 'key', $value, [])); | ||
break; | ||
} | ||
} | ||
|
||
public static function castProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'value' => null, | ||
'expected' => null, | ||
'options' => ['nullable'], | ||
], | ||
[ | ||
'value' => Math::number(42.42), | ||
'expected' => Math::number(42.42), | ||
'options' => ['nullable'], | ||
], | ||
[ | ||
'value' => Math::number(42.42), | ||
'expected' => Math::number(42.42), | ||
], | ||
[ | ||
'value' => null, | ||
'expected' => NotNullableException::class, | ||
], | ||
[ | ||
'value' => '42.4200000', | ||
'expected' => Math::number(42.42), | ||
'options' => ['nullable'], | ||
], | ||
[ | ||
'value' => 42.42, | ||
'expected' => Math::number(42.42), | ||
'options' => ['nullable'], | ||
], | ||
[ | ||
'value' => 42, | ||
'expected' => Math::number(42), | ||
'options' => ['nullable'], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.