Date::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
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
3
namespace Postpay\Serializers;
4
5
use Datetime;
6
use JsonSerializable;
7
8
class Date implements JsonSerializable
9
{
10
    /**
11
     * @const string Default datetime format.
12
     */
13
    const DEFAULT_DATETIME_FORMAT = DateTime::ISO8601;
14
15
    /**
16
     * @const string Default date format.
17
     */
18
    const DEFAULT_DATE_FORMAT = 'Y-m-d';
19
20
    /**
21
     * @var string The stored value.
22
     */
23
    public $value;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param string $value
29
     */
30 3
    public function __construct($value)
31
    {
32 3
        $this->value = $value;
33 3
    }
34
35
    /**
36
     * Creates an instance using the specified datetime format.
37
     *
38
     * @param DateTime $value
39
     * @param string   $format
40
     *
41
     * @return self
42
     */
43 3
    public static function fromDateTime(
44
        $value,
45
        $format = self::DEFAULT_DATETIME_FORMAT
46
    ) {
47 3
        return new self($value->format($format));
48
    }
49
50
    /**
51
     * Creates an instance using the specified date format.
52
     *
53
     * @param DateTime $value
54
     * @param string   $format
55
     *
56
     * @return self
57
     */
58 1
    public static function fromDate(
59
        $value,
60
        $format = self::DEFAULT_DATE_FORMAT
61
    ) {
62 1
        return self::fromDateTime($value, $format);
63
    }
64
65
    /**
66
     * Converts to DateTime type.
67
     *
68
     * @param string $format
69
     *
70
     * @return DateTime
71
     */
72 2
    public function toDateTime($format = self::DEFAULT_DATETIME_FORMAT)
73
    {
74 2
        return DateTime::createFromFormat($format, $this->value);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 1
    public function jsonSerialize()
81
    {
82 1
        return $this->value;
83
    }
84
}
85