AbstractWDatePicker::getDefaultConfigs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
namespace Fwlib\Html\Generator\Element;
3
4
use Fwlib\Html\Generator\ElementMode;
5
use Fwlib\Html\Generator\Helper\GetJsLoadHtmlTrait;
6
7
/**
8
 * Select date with WDatePicker
9
 *
10
 * @copyright   Copyright 2014-2016 Fwolf
11
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
12
 */
13
abstract class AbstractWDatePicker extends PlainDate
14
{
15
    use GetJsLoadHtmlTrait;
16
17
18
    const CFG_JS_TIME_FORMAT = 'formatInJs';
19
20
    const CFG_PHP_TIME_FORMAT = 'formatInPHP';
21
22
    const CFG_MAX_DATE = 'maxDate';
23
24
    const CFG_MIN_DATE = 'minDate';
25
26
    const CFG_OPTIONS = 'options';
27
28
    const CFG_SIZE = 'size';
29
30
    const CFG_TAG = 'tag';
31
32
    const VAL_CLASS_WDATE = 'Wdate';
33
34
    const VAL_JS_TIME_FORMAT = 'yyyy-MM-dd';
35
36
    const VAL_MAX_DATE = '';
37
38
    const VAL_MIN_DATE = '';
39
40
    const VAL_SIZE = 11;
41
42
    const VAL_TAG = 'span';
43
44
45
    /**
46
     * {@inheritdoc}
47
     *
48
     * Append class Wdate of AbstractWDatePicker
49
     */
50
    public function getClass($suffix = '')
51
    {
52
        $class = parent::getClass($suffix);
53
54
        $class .= ' ' . static::VAL_CLASS_WDATE;
55
56
        return trim($class);
57
    }
58
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * Configs
64
     * - formatInJs: Date format in js, EDIT mode in common.
65
     * - formatInPHP: Date format in PHP, SHOW mode in common.
66
     * - maxDate: Same with config of WdatePicker.
67
     * - minDate: Same with config of WdatePicker.
68
     * - options: Other WdatePicker format.
69
     * - size: int, Input size, by em in common.
70
     * - tag: Html tag used in EDIT mode.
71
     */
72
    protected function getDefaultConfigs()
73
    {
74
        return array_merge(parent::getDefaultConfigs(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...G => static::VAL_TAG)); seems to be an array, but some of its elements' types (array) are incompatible with the return type of the parent method Fwlib\Html\Generator\Ele...Text::getDefaultConfigs of type array<string,null|string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
            self::CFG_JS_TIME_FORMAT  => static::VAL_JS_TIME_FORMAT,
76
            self::CFG_PHP_TIME_FORMAT => static::VAL_PHP_TIME_FORMAT,
77
            self::CFG_MAX_DATE        => static::VAL_MAX_DATE,
78
            self::CFG_MIN_DATE        => static::VAL_MIN_DATE,
79
            self::CFG_OPTIONS         => [],
80
            self::CFG_SIZE            => static::VAL_SIZE,
81
            self::CFG_TAG             => static::VAL_TAG,
82
        ]);
83
    }
84
85
86
    /**
87
     * @return  string
88
     */
89
    protected function getFormatInJs()
90
    {
91
        return $this->getConfig(self::CFG_JS_TIME_FORMAT);
92
    }
93
94
95
    /**
96
     * @return  string
97
     */
98
    protected function getFormatInPHP()
99
    {
100
        return $this->getConfig(self::CFG_PHP_TIME_FORMAT);
101
    }
102
103
104
    /**
105
     * @return  string
106
     */
107
    protected function getMaxDate()
108
    {
109
        return $this->getConfig(self::CFG_MAX_DATE);
110
    }
111
112
113
    /**
114
     * @return  string
115
     */
116
    protected function getMinDate()
117
    {
118
        return $this->getConfig(self::CFG_MIN_DATE);
119
    }
120
121
122
    /**
123
     * @return  array
124
     */
125
    protected function getOtherPickerOptions()
126
    {
127
        return $this->getConfig(self::CFG_OPTIONS);
128
    }
129
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getOutputForEditMode()
135
    {
136
        $options = $this->getPickerOptionString();
137
138
        $output = $this->getJsLoadHtml() .
139
            "<input type='text'" .
140
            $this->getClassHtml() .
141
            $this->getIdHtml() . "\n  " .
142
            trim(
143
                $this->getNameHtml() .
144
                $this->getValueHtml(ElementMode::EDIT) .
145
                " size='" . $this->getSize() . "'"
146
            ) . "\n  onfocus='WdatePicker($options);'" .
147
            $this->getRawAttributesHtml() .
148
            " />";
149
150
        return $output;
151
    }
152
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    protected function getOutputForShowMode()
158
    {
159
        $tag = $this->getTag();
160
161
        $output = "<input type='hidden'" .
162
            $this->getClassHtml() .
163
            $this->getIdHtml() . "\n  " .
164
            trim(
165
                $this->getNameHtml() .
166
                $this->getValueHtml(ElementMode::EDIT)
167
            ) . " />\n" .
168
            "<$tag" .
169
            $this->getClassHtml($this->getClass("__title")) .
170
            $this->getIdHtml($this->getId("__title")) .
171
            $this->getRawAttributesHtml() .
172
            ">" .
173
            $this->getValueHtml(ElementMode::SHOW) .
174
            "</$tag>";
175
176
        return $output;
177
    }
178
179
180
    /**
181
     * @return  string
182
     */
183
    protected function getPickerOptionString()
184
    {
185
        $options = $this->getPickerOptions();
186
187
        $jsonUtil = $this->getUtilContainer()->getJson();
188
        $optionStr = $jsonUtil->encode($options);
189
190
        return $optionStr;
191
    }
192
193
194
    /**
195
     * @return  array
196
     */
197
    protected function getPickerOptions()
198
    {
199
        $options = $this->getOtherPickerOptions();
200
201
        $options['dateFmt'] = $this->getFormatInJs();
202
203
        $maxDate = $this->getMaxDate();
204
        if (!empty($maxDate)) {
205
            $options['maxDate'] = trim($maxDate, "'\"");
206
        }
207
208
        $minDate = $this->getMinDate();
209
        if (!empty($minDate)) {
210
            $options['minDate'] = trim($minDate, "'\"");
211
        }
212
213
        return $options;
214
    }
215
216
217
    /**
218
     * @return  int
219
     */
220
    protected function getSize()
221
    {
222
        return $this->getConfig(self::CFG_SIZE);
223
    }
224
225
226
    /**
227
     * @return  string
228
     */
229
    protected function getTag()
230
    {
231
        return $this->getConfig(self::CFG_TAG);
232
    }
233
234
235
    /**
236
     * {@inheritdoc}
237
     *
238
     * Format value.
239
     */
240
    public function getValue()
241
    {
242
        $value = parent::getValue();
243
244
        if (!empty($value)) {
245
            $value = date($this->getFormatInPHP(), strtotime($value));
246
        }
247
248
        return $value;
249
    }
250
}
251