Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor
/.idea
composer.lock
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $model = new TestModel();

$model->carbon_interval = now()->subHours(3)->diffAsCarbonInterval();

$model->save(); // Saved as `P3H`
$model->save(); // Saved as `PT3H`
$model->fresh();

$model->carbon_interval; // Instance of `CarbonInterval`
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
],
"homepage": "https://github.com/atymic/laravel-dateinterval-cast",
"require": {
"php": "^7.3",
"laravel/framework": "^7.0 || ^8.0"
"php": "^7.3|^8.0.2|^8.1|^8.2|^8.3|^8.4",
"laravel/framework": "^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^5.0 || ^6.0",
"phpstan/phpstan": "^0.12.9"
"orchestra/testbench": "^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
"phpstan/phpstan": "^0.12.9",
"livewire/livewire": "^3"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
3 changes: 3 additions & 0 deletions src/Cast/CarbonIntervalCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class CarbonIntervalCast extends DateIntervalCast
*/
public function get($model, string $key, $value, array $attributes)
{
if (is_null($value)) {
return;
}
try {
return CarbonInterval::create($value);
} catch (\Exception $e) {
Expand Down
7 changes: 6 additions & 1 deletion src/Cast/DateIntervalCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class DateIntervalCast implements CastsAttributes
*/
public function get($model, string $key, $value, array $attributes)
{
if (is_null($value)) {
return;
}
try {
return new \DateInterval($value);
} catch (\Exception $e) {
Expand All @@ -39,9 +42,11 @@ public function get($model, string $key, $value, array $attributes)
*/
public function set($model, string $key, $value, array $attributes)
{
if (is_null($value)) {
return;
}
try {
$value = is_string($value) ? CarbonInterval::create($value) : $value;

return [$key => CarbonInterval::getDateIntervalSpec($value)];
} catch (\Exception $e) {
throw InvalidIsoDuration::make($value, $e);
Expand Down
66 changes: 66 additions & 0 deletions src/Synth/CarbonIntervalSynth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Atymic\DateIntervalCast\Synth;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;

class CarbonIntervalSynth extends Synth {
public static $types = [
'native' => \DateInterval::class,
'carbon' => CarbonInterval::class,
];

public static $key = 'cbnint';

static function match($target) {
foreach (static::$types as $type => $class) {
if ($target instanceof $class) return true;
}

return false;
}

function dehydrate($target) {
return [
$target instanceof CarbinInterval ? $target->spec() : $this->getDateIntervalSpec($target),
['type' => array_search(get_class($target), static::$types)],
];
}

function hydrate($value, $meta) {
return new static::$types[$meta['type']]($value);
}

protected function getDateIntervalSpec(\DateInterval $interval): string
{
$spec = 'P';

if ($interval->y) {
$spec .= $interval->y . 'Y';
}
if ($interval->m) {
$spec .= $interval->m . 'M';
}
if ($interval->d) {
$spec .= $interval->d . 'D';
}

if ($interval->h || $interval->i || $interval->s) {
$spec .= 'T';
if ($interval->h) {
$spec .= $interval->h . 'H';
}
if ($interval->i) {
$spec .= $interval->i . 'M';
}
if ($interval->s) {
$spec .= $interval->s . 'S';
}
}

return $spec;
}
}
22 changes: 22 additions & 0 deletions tests/CastsIntervalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public function testDateIntervalCast()
$this->assertInstanceOf(\DateInterval::class, $model->date_interval);
$this->assertDatabaseHas('test', ['id' => $model->id, 'date_interval' => 'P1D']);
}
public function testForNullableColumnsDateIntervalCastIsSkippedIfColumnValueIsNull()
{
$model = new TestEloquentModelWithCustomCasts();
$model->date_interval = null;
$this->assertNotInstanceOf(\DateInterval::class, $model->date_interval);
$this->assertNull($model->getAttributes()['date_interval']);
$model->save();
$model->fresh();
$this->assertNotInstanceOf(\DateInterval::class, $model->date_interval);
$this->assertDatabaseHas('test', ['id' => $model->id, 'date_interval' => null]);
}

public function testCarbonIntervalCast()
{
Expand All @@ -43,6 +54,17 @@ public function testCarbonIntervalCast()
$this->assertInstanceOf(CarbonInterval::class, $model->carbon_interval);
$this->assertDatabaseHas('test', ['id' => $model->id, 'carbon_interval' => 'P4D']);
}
public function testForNullableColumnsCarbonIntervalCastIsSkippedIfColumnValueIsNull()
{
$model = new TestEloquentModelWithCustomCasts();
$model->carbon_interval = null;
$this->assertNotInstanceOf(CarbonInterval::class, $model->carbon_interval);
$this->assertSame(null, $model->getAttributes()['carbon_interval']);
$model->save();
$model->fresh();
$this->assertNotInstanceOf(CarbonInterval::class, $model->carbon_interval);
$this->assertDatabaseHas('test', ['id' => $model->id, 'carbon_interval' => null]);
}

public function testThrowsExceptionOnInvalidDateInterval()
{
Expand Down
69 changes: 69 additions & 0 deletions tests/Synth/CarbonIntervalSynthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use PHPUnit\Framework\TestCase;
use Atymic\DateIntervalCast\Synth\CarbonIntervalSynth;
use Carbon\CarbonInterval;

class CarbonIntervalSynthTest extends TestCase
{
public function testMatch()
{
// Mock the Synth class constructor
$synth = $this->createPartialMock(CarbonIntervalSynth::class, ['__construct']);

$carbonInterval = CarbonInterval::days(2);
$dateInterval = new \DateInterval('P2D');

$this->assertTrue($synth::match($carbonInterval), "Should match CarbonInterval");
$this->assertTrue($synth::match($dateInterval), "Should match DateInterval");
$this->assertFalse($synth::match('Not an interval'), "Should not match non-interval values");
}

public function testDehydrate()
{
$synth = $this->createPartialMock(CarbonIntervalSynth::class, ['__construct']);

$carbonInterval = CarbonInterval::days(2);
$expectedCarbonDehydration = [
'P2D',
['type' => 'carbon'],
];

$this->assertEquals(
$expectedCarbonDehydration,
$synth->dehydrate($carbonInterval),
"Dehydration of CarbonInterval should produce expected output"
);

$dateInterval = new \DateInterval('P2D');
$expectedDateDehydration = [
'P2D',
['type' => 'native'],
];

$this->assertEquals(
$expectedDateDehydration,
$synth->dehydrate($dateInterval),
"Dehydration of DateInterval should produce expected output"
);
}

public function testHydrate()
{
$synth = $this->createPartialMock(CarbonIntervalSynth::class, ['__construct']);

$carbonMeta = ['type' => 'carbon'];
$carbonValue = 'P2D';
$hydratedCarbon = $synth->hydrate($carbonValue, $carbonMeta);

$this->assertInstanceOf(CarbonInterval::class, $hydratedCarbon, "Hydrated object should be an instance of CarbonInterval");
$this->assertEquals(CarbonInterval::days(2), $hydratedCarbon, "Hydrated CarbonInterval should match the expected interval");

$dateMeta = ['type' => 'native'];
$dateValue = 'P2D';
$hydratedDate = $synth->hydrate($dateValue, $dateMeta);

$this->assertInstanceOf(\DateInterval::class, $hydratedDate, "Hydrated object should be an instance of DateInterval");
$this->assertEquals(new \DateInterval('P2D'), $hydratedDate, "Hydrated DateInterval should match the expected interval");
}
}