Completed
Branch 09branch (946dde)
by Anton
05:16
created

AbstractTimestamp::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * spiral
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\Models\Accessors;
9
10
use Spiral\Models\AccessorInterface;
11
12
/**
13
 * Provides shared functionality for time based fields in both ORM and ODM components.
14
 *
15
 * You can also implement entity specific timezone using entity context.
16
 */
17
abstract class AbstractTimestamp extends \DateTime implements AccessorInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    const DEFAULT_FORMAT = 'F d, Y H:m';
23
24
    /**
25
     * @var \DateTimeInterface
26
     */
27
    private $initial = false;
28
29
    /**
30
     * @param string $value
31
     * @param array  $context
32
     */
33
    public function __construct($value, array $context)
34
    {
35
        //Datetime accessor default timezone is same as currently in application
36
        parent::__construct(
37
            "@" . $this->fetchTimestamp($value)
38
        );
39
40
        $this->initial = clone $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function setValue($data)
47
    {
48
        $this->setTimestamp($this->fetchTimestamp($data));
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @return \DateTimeInterface
55
     */
56
    public function packValue()
57
    {
58
        return clone $this;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function hasChanges(): bool
65
    {
66
        return $this->initial->getTimestamp() != $this->getTimestamp();
67
    }
68
69
    /**
70
     * Reset state.
71
     */
72
    public function flushChanges()
73
    {
74
        $this->initial = clone $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function jsonSerialize()
81
    {
82
        return $this->format(DATE_ISO8601);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function __toString()
89
    {
90
        return $this->format(static::DEFAULT_FORMAT);
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function __debugInfo()
97
    {
98
        return [
99
            'date'     => $this->format(DATE_ISO8601),
100
            'timezone' => $this->getTimezone()->getName()
101
        ];
102
    }
103
104
    /**
105
     * Fetch timestamp from input value.
106
     *
107
     * @param mixed $value
108
     *
109
     * @return int
110
     */
111
    abstract protected function fetchTimestamp($value): int;
112
113
    /**
114
     * Helper method, convert input variable to a valid timestamp. Can accept timestamp (will be
115
     * converted to an int) or valid datetime string. Timezone of input datetime string can be set.
116
     *
117
     * @param string|int    $datetime Timestamp or valid datetime string.
118
     * @param \DateTimeZone $timezone Source timezone.
119
     *
120
     * @return int|null
121
     */
122
    protected function castTimestamp($datetime, \DateTimeZone $timezone = null)
123
    {
124
        if ($datetime instanceof \DateTime) {
125
            return $datetime->getTimestamp();
126
        }
127
128
        if (!is_scalar($datetime)) {
129
            return null;
130
        }
131
132
        if (is_numeric($datetime)) {
133
            //Nothing to do
134
            return (int)$datetime;
135
        }
136
137
        try {
138
            return (new \DateTime($datetime, $timezone))->getTimestamp();
139
        } catch (\Exception $e) {
140
            //Error parsing
141
            return null;
142
        }
143
    }
144
}