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: Adds support for base Carbon class #500

Merged
merged 3 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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