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

Add trait to use in eloquent models #144

Merged
merged 2 commits into from
Dec 11, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"satooshi/php-coveralls": "~0.6"
},
"autoload": {
"classmap": [
"src/traits"
],
"psr-0": {
"Jenssegers\\Date": "src/"
}
Expand Down
41 changes: 41 additions & 0 deletions src/traits/EloquentDateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace Jenssegers\Date\Traits;

use Jenssegers\Date\Date;

/**
* This is a trait overwrites methods to
* replace Carbon with Date.
*
* @package Jenssegers\Date
*/

trait EloquentDateHelper{

public function freshTimestamp()
{

return new Date();
}

protected function asDateTime($value)
{

if (is_numeric($value)) {

return Date::createFromTimestamp($value);

} elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {

return Date::createFromFormat('Y-m-d', $value)->startOfDay();

} elseif (! $value instanceof DateTime) {
$format = $this->getDateFormat();

return Date::createFromFormat($format, $value);
}


return Date::instance($value);
}

}