DateTime   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 71
ccs 0
cts 18
cp 0
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onIndex() 0 12 1
A setDateFormat() 0 5 1
A setTimeFormat() 0 5 1
A setDateTimeFormat() 0 5 1
A onView() 0 3 1
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