Completed
Push — master ( 52970f...c6d773 )
by Tristan
24:57 queued 10:40
created

BaseModel::freshTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model as Eloquent;
6
use Illuminate\Support\Facades\Config;
7
use Carbon\Carbon;
8
use DateTimeInterface;
9
use Jenssegers\Date\Date;
10
11
abstract class BaseModel extends Eloquent {
0 ignored issues
show
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
Coding Style introduced by
Missing doc comment for class BaseModel
Loading history...
12
    //Override date functions to return Jenssegers Data instead of Carbon
13
14
    /**
15
     * Get a fresh timestamp for the model.
16
     *
17
     * @return Date
18
     */
19 62
    public function freshTimestamp()
20
    {
21 62
        return new Date;
22
    }
23
24
    /**
25
     * Return a timestamp as DateTime object.
26
     *
27
     * @param  mixed $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
28
     * @return Date
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
29
     */
30 62
    protected function asDateTime($value)
31
    {
32 62
        $timezone = Config::get('app.timezone');
33
34
        // If this value is already a Carbon instance, we shall just return it as is.
35
        // This prevents us having to re-instantiate a Carbon instance when we know
36
        // it already is one, which wouldn't be fulfilled by the DateTime check.
37 62
        if ($value instanceof Carbon) {
38 62
            return Date::parse($value, $timezone);
39
        }
40 62
        if ($value instanceof Date) {
41
            return $value;
42
        }
43
        // If the value is already a DateTime instance, we will just skip the rest of
44
        // these checks since they will be a waste of time, and hinder performance
45
        // when checking the field. We will just return the DateTime right away.
46 62
        if ($value instanceof DateTimeInterface) {
47 24
            return new Date(
48
                //$value->format('Y-m-d H:i:s.u'), $value->getTimeZone()
49 24
                $value->format('Y-m-d H:i:s.u'), $timezone
50
            );
51
        }
52
        // If this value is an integer, we will assume it is a UNIX timestamp's value
53
        // and format a Carbon object from this timestamp. This allows flexibility
54
        // when defining your date fields as they might be UNIX timestamps here.
55 62
        if (is_numeric($value)) {
56
            return Date::createFromTimestamp($value, $timezone);
57
        }
58
        // If the value is in simply year, month, day format, we will instantiate the
59
        // Carbon instances from that format. Again, this provides for simple date
60
        // fields on the database, while still supporting Carbonized conversion.
61 62
        if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) {
62
            return Date::createFromFormat('Y-m-d', $value, $timezone)->startOfDay();
63
        }
64
        // Finally, we will just assume this date is in the format used by default on
65
        // the database connection and use that format to create the Carbon object
66
        // that is returned back out to the developers after we convert it here.
67 62
        return Date::createFromFormat($this->getDateFormat(), $value, $timezone);
68
    }
69
}
70