Skip to content

Commit

Permalink
feat: Adds support for base Carbon class (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
arxeiss committed Mar 20, 2020
1 parent 1055d4a commit 4e56118
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,15 @@ private function requireFiles(array $files): void

private function getDateClass(): string
{
return $this->dateClass ?? class_exists(\Illuminate\Support\Facades\Date::class)
? '\\'.get_class(\Illuminate\Support\Facades\Date::now())
: '\Illuminate\Support\Carbon';
if (! $this->dateClass) {
$this->dateClass = class_exists(\Illuminate\Support\Facades\Date::class)
? '\\'.get_class(\Illuminate\Support\Facades\Date::now())
: '\Illuminate\Support\Carbon';

$this->dateClass .= '|\Carbon\Carbon';
}

return $this->dateClass;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion tests/Features/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Features\Properties;

use App\User;
use Carbon\Carbon as BaseCarbon;
use Illuminate\Support\Carbon;

class ModelPropertyExtension
Expand All @@ -17,7 +18,10 @@ public function testPropertyReturnType(): int
return $this->user->id;
}

public function testDateReturnType(): ?Carbon
/**
* @return Carbon|BaseCarbon|null
*/
public function testDateReturnType()
{
return $this->user->created_at;
}
Expand All @@ -27,6 +31,7 @@ public function testWriteToProperty(): void
$this->user->created_at = 'test';
$this->user->created_at = now();
$this->user->created_at = null;
$this->user->created_at = BaseCarbon::now();
}

/** @return array<int, string> */
Expand Down

0 comments on commit 4e56118

Please sign in to comment.