Issues (83)

src/HasDateTimeFormat.php (2 issues)

1
<?php
2
3
namespace LeKoala\FormElements;
4
5
use IntlDateFormatter;
6
use InvalidArgumentException;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
9
trait HasDateTimeFormat
10
{
11
    /**
12
     * @var bool
13
     */
14
    protected $html5 = true;
15
16
    /**
17
     * Override date format. If empty will default to that used by the current locale.
18
     *
19
     * @var null
20
     */
21
    protected $datetimeFormat = null;
22
23
    /**
24
     * Custom timezone
25
     *
26
     * @var string
27
     */
28
    protected $timezone = null;
29
30
    /**
31
     * Get date format in CLDR standard format
32
     *
33
     * This can be set explicitly. If not, this will be generated from the current locale
34
     * with the current date length.
35
     * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table
36
     */
37
    public function getDatetimeFormat()
38
    {
39
        if ($this->datetimeFormat) {
40
            return $this->datetimeFormat;
41
        }
42
43
        // Get from locale
44
        return $this->getFrontendFormatter()->getPattern();
45
    }
46
47
    /**
48
     * Set date format in CLDR standard format.
49
     * Only applicable with {@link setHTML5(false)}.
50
     *
51
     * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table
52
     * @param string $format
53
     * @return $this
54
     */
55
    public function setDatetimeFormat($format)
56
    {
57
        $this->datetimeFormat = $format;
0 ignored issues
show
Documentation Bug introduced by
It seems like $format of type string is incompatible with the declared type null of property $datetimeFormat.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        return $this;
59
    }
60
61
    /**
62
     * Get date formatter with the standard locale / date format
63
     *
64
     * @throws \LogicException
65
     * @return IntlDateFormatter
66
     */
67
    protected function getFrontendFormatter()
68
    {
69
        $formatter = IntlDateFormatter::create(
70
            $this->getLocale(),
0 ignored issues
show
It seems like getLocale() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $this->/** @scrutinizer ignore-call */ 
71
                   getLocale(),
Loading history...
71
            IntlDateFormatter::MEDIUM,
72
            IntlDateFormatter::MEDIUM,
73
            $this->getTimezone()
74
        );
75
76
        if ($this->datetimeFormat) {
77
            // Don't invoke getDatetimeFormat() directly to avoid infinite loop
78
            $ok = $formatter->setPattern($this->datetimeFormat);
79
            if (!$ok) {
80
                throw new InvalidArgumentException("Invalid date format {$this->datetimeFormat}");
81
            }
82
        } else {
83
            $formatter->setPattern(DBDatetime::ISO_DATETIME_NORMALISED);
84
        }
85
        return $formatter;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getTimezone()
92
    {
93
        return $this->timezone;
94
    }
95
96
    /**
97
     * @param string $timezone
98
     * @return $this
99
     */
100
    public function setTimezone($timezone)
101
    {
102
        if ($this->value && $timezone !== $this->timezone) {
103
            throw new \BadMethodCallException("Can't change timezone after setting a value");
104
        }
105
106
        $this->timezone = $timezone;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function getHTML5()
115
    {
116
        return $this->html5;
117
    }
118
119
    /**
120
     * This is required (and ignored) because DBDate use this to scaffold the field
121
     *
122
     * @param boolean $bool
123
     * @return $this
124
     */
125
    public function setHTML5($bool)
126
    {
127
        $this->html5 = $bool;
128
        return $this;
129
    }
130
}
131