Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a way for Cast classes to express when an attribute should be unset (removed) #6

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/Casts/CastFloatOrUnset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Carsdotcom\LaravelJsonModel\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;

class CastFloatOrUnset implements ShouldUnset, CastsInboundAttributes
{
public static function shouldUnset(mixed $value): bool
{
return ($value === null);
}

public function set($model, string $key, $value, array $attributes)
{
if ($this->shouldUnset($value)) {
return null;
}
return (float)$value;
}
}
21 changes: 21 additions & 0 deletions app/Casts/CastNonEmptyStringOrUnset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Carsdotcom\LaravelJsonModel\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;

class CastNonEmptyStringOrUnset implements ShouldUnset, CastsInboundAttributes
{
public static function shouldUnset(mixed $value): bool
{
return ($value === null || $value === '');
}

public function set($model, string $key, $value, array $attributes)
{
if ($this->shouldUnset($value)) {
return null;
}
return (string)$value;
}
}
21 changes: 21 additions & 0 deletions app/Casts/CastStringOrUnset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Carsdotcom\LaravelJsonModel\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;

class CastStringOrUnset implements ShouldUnset, CastsInboundAttributes
{
public static function shouldUnset(mixed $value): bool
{
return ($value === null);
}

public function set($model, string $key, $value, array $attributes)
{
if ($this->shouldUnset($value)) {
return null;
}
return (string)$value;
}
}
8 changes: 8 additions & 0 deletions app/Casts/ShouldUnset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Carsdotcom\LaravelJsonModel\Casts;

interface ShouldUnset
{
static function shouldUnset(mixed $value): bool;
}
21 changes: 21 additions & 0 deletions app/JsonModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ArrayAccess;
use Carsdotcom\JsonSchemaValidation\Exceptions\JsonSchemaValidationException;
use Carsdotcom\JsonSchemaValidation\Traits\ValidatesWithJsonSchema;
use Carsdotcom\LaravelJsonModel\Casts\ShouldUnset;
use Carsdotcom\LaravelJsonModel\Contracts\CanCascadeEvents;
use Carsdotcom\JsonSchemaValidation\Contracts\CanValidate;
use Carsdotcom\LaravelJsonModel\Helpers\ClassUsesTrait;
Expand Down Expand Up @@ -621,4 +622,24 @@ public function hasJsonModelAttributes(): bool
{
return (new ClassUsesTrait())(class: $this, trait: HasJsonModelAttributes::class);
}

/**
* Extends setAttribute to cooperate with casts that implement ShouldUnset.
* If the cast wishes, we'll *remove* the attribute from the Json representation.
* This lets us implement things like PATCH {"zip":null} to mean "remove ZIP, return to default behavior"
*/
use HasAttributes {
setAttribute as protected eloquentSetAttribute;
}

public function setAttribute($key, $value) {
$castType = $this->hasCast($key) ? $this->getCastType($key) : null;
if (is_a($castType, ShouldUnset::class, true) && $castType::shouldUnset($value)) {
unset($this->attributes[$key]);
return $this;
}

$this->eloquentSetAttribute($key, $value);
return $this;
}
}
76 changes: 76 additions & 0 deletions tests/Unit/JsonModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
namespace Tests\Unit;

use Carsdotcom\JsonSchemaValidation\Exceptions\JsonSchemaValidationException;
use Carsdotcom\LaravelJsonModel\Casts\CastFloatOrUnset;
use Carsdotcom\LaravelJsonModel\Casts\CastNonEmptyStringOrUnset;
use Carsdotcom\LaravelJsonModel\Casts\CastStringOrUnset;
use Carsdotcom\LaravelJsonModel\CollectionOfJsonModels;
use Carsdotcom\JsonSchemaValidation\SchemaValidator as SchemaValidator;
use Carsdotcom\LaravelJsonModel\JsonModel;
Expand Down Expand Up @@ -849,6 +852,79 @@ public function testSafeUpdateRecursive(): void
self::assertSame('CA', $jsonModel->address->country, 'attribute is invalid, revert to previous value');
self::assertSame('Jeremy', $jsonModel->first_name, 'attribute is valid, set');
}

public function testCastFloatOrUnset(): void
{
$jsonModel = new class extends JsonModel{
protected $casts = [
'fou' => CastFloatOrUnset::class
];
};

self::assertFalse(isset($jsonModel->fou), "Initializes unset");
$jsonModel->fou = null;
self::assertFalse(isset($jsonModel->fou), "Stays unset on initialize to null");
$jsonModel->fou = 6.9;
self::assertSame(6.9, $jsonModel->fou, "Float is unchanged");
$jsonModel->fou = '6';
self::assertSame(6.0, $jsonModel->fou, "String is cast");
$jsonModel->fou = 9;
self::assertSame(9.0, $jsonModel->fou, "int is cast");
self::assertStringContainsString('"fou":9', $jsonModel->toJson());
$jsonModel->fou = null;
self::assertFalse(isset($jsonModel->fou), "Set to null unsets attribute");
self::assertStringNotContainsString('fou', $jsonModel->toJson());
}

public function testCastStringOrUnset(): void
{
$jsonModel = new class extends JsonModel{
protected $casts = [
'sou' => CastStringOrUnset::class
];
};

self::assertFalse(isset($jsonModel->sou), "Initializes unset");
$jsonModel->sou = null;
self::assertFalse(isset($jsonModel->sou), "Stays unset on initialize to null");
$jsonModel->sou = 'happy happy';
self::assertSame('happy happy', $jsonModel->sou, "String is unchanged");
$jsonModel->sou = 6;
self::assertSame('6', $jsonModel->sou, "int is cast");
$jsonModel->sou = false;
self::assertSame('', $jsonModel->sou, "bool is cast");
self::assertStringContainsString('"sou":""', $jsonModel->toJson());
$jsonModel->sou = null;
self::assertFalse(isset($jsonModel->sou), "Set to null unsets attribute");
self::assertStringNotContainsString('sou', $jsonModel->toJson());
}

public function testCastNonEmptyStringOrUnset(): void
{
$jsonModel = new class extends JsonModel{
protected $casts = [
'sou' => CastNonEmptyStringOrUnset::class
];
};

self::assertFalse(isset($jsonModel->sou), "Initializes unset");
$jsonModel->sou = null;
self::assertFalse(isset($jsonModel->sou), "Stays unset on initialize to null");
$jsonModel->sou = 'happy happy';
self::assertSame('happy happy', $jsonModel->sou, "String is unchanged");

$jsonModel->sou = '';
self::assertFalse(isset($jsonModel->sou), "Set to empty string unsets attribute");
self::assertStringNotContainsString('sou', $jsonModel->toJson());

$jsonModel->sou = 6;
self::assertSame('6', $jsonModel->sou, "int is cast");
self::assertStringContainsString('"sou":"6"', $jsonModel->toJson());

$jsonModel->sou = null;
self::assertFalse(isset($jsonModel->sou), "Set to null unsets attribute");
self::assertStringNotContainsString('sou', $jsonModel->toJson());
}
}
/**
* These are classes and models needed to test inheritance, etc.
Expand Down