-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JsonForceEmptyObjectAsArray cast
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\PostgresqlEnhanced\Eloquent\Casts; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
|
||
class JsonForceEmptyObjectAsArray implements CastsAttributes | ||
{ | ||
/** | ||
* Transform the attribute from the underlying model values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param ?string $value | ||
* | ||
* @return ?array<array-key, mixed> | ||
*/ | ||
public function get($model, string $key, mixed $value, array $attributes): ?array | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
return json_decode($value, true, flags: \JSON_THROW_ON_ERROR); | ||
} | ||
|
||
/** | ||
* Transform the attribute to its underlying model values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param array<array-key, mixed>|\Illuminate\Support\Collection<array-key, mixed>|null $value | ||
*/ | ||
public function set($model, string $key, mixed $value, array $attributes): ?string | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
return match ($casted = json_encode($value, flags: \JSON_THROW_ON_ERROR)) { | ||
'[]' => '{}', | ||
default => $casted, | ||
}; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\PostgresqlEnhanced\Tests\Eloquent; | ||
|
||
use Composer\Semver\Comparator; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Tpetry\PostgresqlEnhanced\Eloquent\Casts\JsonForceEmptyObjectAsArray; | ||
use Tpetry\PostgresqlEnhanced\Tests\TestCase; | ||
|
||
class JsonForceEmptyObjectAsArrayCastTest extends TestCase | ||
{ | ||
public function testParseFromDatabaseValue(): void | ||
{ | ||
if (Comparator::lessThan($this->app->version(), '8.0.0')) { | ||
$this->markTestSkipped('Cast classes have been added with Laravel 8.'); | ||
} | ||
|
||
$cast = new JsonForceEmptyObjectAsArray(); | ||
$model = new class extends Model { }; | ||
|
||
$this->assertNull($cast->get($model, 'column', null, [])); | ||
$this->assertEquals([], $cast->get($model, 'column', '{}', [])); | ||
$this->assertEquals(['a' => 1, 'b' => 2], $cast->get($model, 'column', '{"a":1,"b":2}', [])); | ||
$this->assertEquals(['c' => [1, 2, 3], 'd' => [4, 5, 6]], $cast->get($model, 'column', '{"c":[1,2,3],"d":[4,5,6]}', [])); | ||
$this->assertEquals([1, 2], $cast->get($model, 'column', '[1,2]', [])); | ||
$this->assertEquals([[1, 2, 3], [4, 5, 6]], $cast->get($model, 'column', '[[1,2,3],[4,5,6]]', [])); | ||
} | ||
|
||
public function testTransformToDatabaseValue(): void | ||
{ | ||
if (Comparator::lessThan($this->app->version(), '8.0.0')) { | ||
$this->markTestSkipped('Cast classes have been added with Laravel 8.'); | ||
} | ||
|
||
$cast = new JsonForceEmptyObjectAsArray(); | ||
$model = new class extends Model { }; | ||
|
||
$this->assertNull($cast->get($model, 'column', null, [])); | ||
|
||
$this->assertEquals('{}', $cast->set($model, 'column', [], [])); | ||
$this->assertEquals('{"a":1,"b":2}', $cast->set($model, 'column', ['a' => 1, 'b' => 2], [])); | ||
$this->assertEquals('{"c":[1,2,3],"d":[4,5,6]}', $cast->set($model, 'column', ['c' => [1, 2, 3], 'd' => [4, 5, 6]], [])); | ||
$this->assertEquals('[1,2]', $cast->set($model, 'column', [1, 2], [])); | ||
$this->assertEquals('[[1,2,3],[4,5,6]]', $cast->set($model, 'column', [[1, 2, 3], [4, 5, 6]], [])); | ||
|
||
$this->assertEquals('{}', $cast->set($model, 'column', collect(), [])); | ||
$this->assertEquals('{"a":1,"b":2}', $cast->set($model, 'column', collect(['a' => 1, 'b' => 2]), [])); | ||
$this->assertEquals('{"c":[1,2,3],"d":[4,5,6]}', $cast->set($model, 'column', collect(['c' => [1, 2, 3], 'd' => [4, 5, 6]]), [])); | ||
$this->assertEquals('[1,2]', $cast->set($model, 'column', collect([1, 2]), [])); | ||
$this->assertEquals('[[1,2,3],[4,5,6]]', $cast->set($model, 'column', collect([[1, 2, 3], [4, 5, 6]]), [])); | ||
} | ||
} |