DropDownSelect::getOutputForEditMode()   B
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 10
nop 0
dl 0
loc 40
rs 8.9688
c 0
b 0
f 0
1
<?php
2
namespace Fwlib\Html\Generator\Element;
3
4
use Fwlib\Html\Generator\AbstractElement;
5
use Fwlib\Html\Generator\ElementMode;
6
use Fwlib\Html\Generator\Helper\GetTitleClassAndIdTrait;
7
8
/**
9
 * Drop down select box
10
 *
11
 * Easy mode for child class: extend and fill initial item array, or overwrite
12
 * its getter method for dynamic generated item array.
13
 *
14
 * @copyright   Copyright 2015 Fwolf
15
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
16
 */
17
class DropDownSelect extends AbstractElement
18
{
19
    use GetTitleClassAndIdTrait;
20
21
22
    const KEY_TAG = 'tag';
23
24
    const KEY_ITEMS = 'items';
25
26
    const KEY_PROMPT = 'prompt';
27
28
    const KEY_INVALID = 'invalid';
29
30
31
    /**
32
     * @var array
33
     */
34
    protected $initialItems = [];
35
36
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * Configs
41
     * - tag: Use div or p or none html tag in show mode.
42
     * - items: Select able items, {value: title}.
43
     * - prompt: Show welcome message like 'please select'.
44
     * - invalid: Show when value not found in items.
45
     */
46
    protected function getDefaultConfigs()
47
    {
48
        $configs = parent::getDefaultConfigs();
49
50
        return array_merge($configs, [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge($conf...ID => 'Invalid Item')); 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\Abs...ment::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...
51
            self::KEY_TAG     => 'span',
52
            self::KEY_ITEMS   => $this->getInitialItems(),
53
            self::KEY_PROMPT  => 'Please select',
54
            self::KEY_INVALID => 'Invalid Item',
55
        ]);
56
    }
57
58
59
    /**
60
     * @return  array
61
     */
62
    protected function getInitialItems()
63
    {
64
        return $this->initialItems;
65
    }
66
67
68
    /**
69
     * @return  string
70
     */
71
    protected function getInvalid()
72
    {
73
        return $this->getConfig(self::KEY_INVALID);
74
    }
75
76
77
    /**
78
     * @return  string[]
79
     */
80
    protected function getItems()
81
    {
82
        return $this->getConfig(self::KEY_ITEMS);
83
    }
84
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function getOutputForEditMode()
90
    {
91
        $items = $this->getItems();
92
        $prompt = $this->getPrompt();
93
94
        $output = "<select" .
95
            $this->getClassHtml() .
96
            $this->getIdHtml() . "\n  " .
97
            trim($this->getNameHtml()) .
98
            $this->getRawAttributesHtml() .
99
            ">";
100
101
        $titleClass = $this->getTitleClass();
102
        $titleClassHtml = $this->getClassHtml($titleClass);
103
104
        if (!empty($prompt)) {
105
            $output .= <<<TAG
106
107
  <option value=''{$titleClassHtml}>$prompt</option>
108
TAG;
109
        }
110
111
        foreach ($items as $value => $title) {
112
            $selValue = $this->getValue();
113
            // In case empty '' is for prompt, while 0 for first value
114
            $selected = (0 < strlen($selValue) && $value == $selValue)
115
                ? " selected='selected'" : '';
116
            $titleId = $this->getTitleId($value);
117
            $titleIdHtml = $this->getIdHtml($titleId);
118
            $output .= <<<TAG
119
120
  <option value='$value'{$selected}{$titleClassHtml}{$titleIdHtml}>{$title}</option>
121
TAG;
122
        }
123
124
        $output .= "
125
</select>";
126
127
        return $output;
128
    }
129
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function getOutputForShowMode()
135
    {
136
        $items = $this->getItems();
137
        $value = $this->getValue();
138
        $title = array_key_exists($value, $items)
139
            ? $items[$value]
140
            : $this->getInvalid();
141
142
        $tag = $this->getTag();
143
144
        if (empty($tag)) {
145
            return $title;
146
        }
147
148
        // Only one value will be shown, so title id not include value
149
        $output = "<input type='hidden'" .
150
            $this->getIdHtml() .
151
            $this->getNameHtml() .
152
            $this->getValueHtml(ElementMode::EDIT) .
153
            $this->getRawAttributes() .
154
            " />\n" .
155
            "<$tag" .
156
            $this->getClassHtml($this->getTitleClass()) .
157
            $this->getIdHtml($this->getTitleId()) .
158
            $this->getRawAttributesHtml() .
159
            ">" .
160
            $title .
161
            "</$tag>";
162
163
        return $output;
164
    }
165
166
167
    /**
168
     * @return  string
169
     */
170
    protected function getPrompt()
171
    {
172
        return $this->getConfig(self::KEY_PROMPT);
173
    }
174
175
176
    /**
177
     * @return  string
178
     */
179
    protected function getTag()
180
    {
181
        return $this->getConfig(self::KEY_TAG);
182
    }
183
}
184