DateTime::onView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Carbon\Carbon;
6
7
class DateTime extends Field
8
{
9
    /** @var string */
10
    protected $dateFormat = 'M j, Y';
11
12
    /** @var string */
13
    protected $timeFormat = 'g:i A';
14
15
    /** @var string */
16
    protected $dateTimeFormat = 'M j, Y g:i A';
17
18
    /**
19
     * @return array
20
     */
21
    public function onIndex(): array
22
    {
23
        $format = [
24
            self::class => $this->dateTimeFormat,
25
            Date::class => $this->dateFormat,
26
            Time::class => $this->timeFormat,
27
        ][\get_class($this)];
28
29
        $formattedValue = Carbon::parse($this->value())->format($format);
30
31
        return [
32
            'formatted' => $formattedValue,
33
        ];
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function onView(): array
40
    {
41
        return $this->onIndex();
42
    }
43
44
    /**
45
     * @param string $format
46
     *
47
     * @return self
48
     */
49
    public function setDateTimeFormat(string $format): self
50
    {
51
        $this->$dateTimeFormat = $format;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dateTimeFormat seems to be never defined.
Loading history...
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param string $dateFormat
58
     *
59
     * @return self
60
     */
61
    public function setDateFormat(string $dateFormat): self
62
    {
63
        $this->dateFormat = $dateFormat;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $timeFormat
70
     *
71
     * @return self
72
     */
73
    public function setTimeFormat(string $timeFormat): self
74
    {
75
        $this->timeFormat = $timeFormat;
76
77
        return $this;
78
    }
79
}
80