Container   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 9
Bugs 3 Features 1
Metric Value
wmc 13
c 9
b 3
f 1
lcom 1
cbo 1
dl 0
loc 191
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 7 1
A prepend() 0 7 1
A replaceWith() 0 7 1
A __construct() 0 17 2
A replace() 0 7 1
A html() 0 7 1
A val() 0 7 1
A remove() 0 6 1
A animate() 0 6 1
A removeClass() 0 7 1
A addClass() 0 7 1
A attr() 0 8 1
1
<?php
2
3
namespace DM\AjaxCom\Responder\Container;
4
5
use DM\AjaxCom\Responder\AbstractResponder;
6
7
class Container extends AbstractResponder
8
{
9
    const OBJECT_IDENTIFIER = 'container';
10
11
    const OPTION_VALUE = 'value';
12
    const OPTION_TARGET = 'target';
13
    const OPTION_METHOD = 'method';
14
    const OPTION_ANIMATE = 'animate';
15
    const OPTION_REMOVE_CLASS = 'removeClass';
16
    const OPTION_ADD_CLASS = 'addClass';
17
    const OPTION_ATTR = 'attr';
18
19
    /**
20
     * Constructor
21
     *
22
     * @param string $identifier
23
     */
24
    public function __construct($identifier = null)
25
    {
26
        $this->registerOption(self::OPTION_VALUE);
27
        $this->registerOption(self::OPTION_TARGET);
28
        $this->registerOption(self::OPTION_METHOD);
29
        $this->registerOption(self::OPTION_ANIMATE);
30
        $this->registerOption(self::OPTION_REMOVE_CLASS);
31
        $this->registerOption(self::OPTION_ADD_CLASS);
32
        $this->registerOption(self::OPTION_ATTR);
33
34
35
        if ($identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identifier of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
36
            $this->setOption(self::OPTION_TARGET, $identifier);
37
        }
38
39
        $this->animate();
40
    }
41
42
    /**
43
     * Append html to container
44
     *
45
     * @var string $html
46
     * @return Container
47
     */
48
49
    public function append($html)
50
    {
51
        $this->setOption(self::OPTION_VALUE, $html);
52
        $this->setOption(self::OPTION_METHOD, 'append');
53
54
        return $this;
55
    }
56
57
    /**
58
     * Prepend html to container
59
     *
60
     * @var string $html
61
     * @return Container
62
     */
63
    public function prepend($html)
64
    {
65
        $this->setOption(self::OPTION_VALUE, $html);
66
        $this->setOption(self::OPTION_METHOD, 'prepend');
67
68
        return $this;
69
    }
70
71
    /**
72
     * Replace container html with this html and animate
73
     *
74
     * @var string $html
75
     * @return Container
76
     */
77
    public function replaceWith($html)
78
    {
79
        $this->setOption(self::OPTION_VALUE, $html);
80
        $this->setOption(self::OPTION_METHOD, 'replaceWith');
81
82
        return $this;
83
    }
84
85
    /**
86
     * Replace container html with this html without animation
87
     * Keeps element display property
88
     *
89
     * @var string $html
90
     * @return Container
91
     */
92
    public function replace($html)
93
    {
94
        $this->setOption(self::OPTION_VALUE, $html);
95
        $this->setOption(self::OPTION_METHOD, 'replace');
96
97
        return $this;
98
    }
99
100
    /**
101
     * Sets inner html of container
102
     *
103
     * @var string $html
104
     * @return Container
105
     */
106
    public function html($html)
107
    {
108
        $this->setOption(self::OPTION_VALUE, $html);
109
        $this->setOption(self::OPTION_METHOD, 'html');
110
111
        return $this;
112
    }
113
114
    /**
115
     * Set value of container object
116
     *
117
     * @var string $html
118
     * @return Container
119
     */
120
    public function val($html)
121
    {
122
        $this->setOption(self::OPTION_VALUE, $html);
123
        $this->setOption(self::OPTION_METHOD, 'val');
124
125
        return $this;
126
    }
127
128
    /**
129
     * Remove container
130
     *
131
     * @return Container
132
     */
133
    public function remove()
134
    {
135
        $this->setOption(self::OPTION_METHOD, 'remove');
136
137
        return $this;
138
    }
139
140
141
    /**
142
     * Enable/disable animation effect
143
     *
144
     * @var bool $enable
145
     * @return Container
146
     */
147
    public function animate($enable = false)
148
    {
149
        $this->setOption(self::OPTION_ANIMATE, (bool)$enable);
150
151
        return $this;
152
    }
153
154
    /**
155
     * Remove CSS class from container
156
     *
157
     * @param string $class
158
     * @return Container
159
     */
160
    public function removeClass($class)
161
    {
162
        $this->setOption(self::OPTION_METHOD, "removeClass");
163
        $this->setOption(self::OPTION_REMOVE_CLASS, $class);
164
165
        return $this;
166
    }
167
168
    /**
169
     * Add CSS class to container
170
     *
171
     * @param string $class
172
     * @return Container
173
     */
174
    public function addClass($class)
175
    {
176
        $this->setOption(self::OPTION_METHOD, "addClass");
177
        $this->setOption(self::OPTION_ADD_CLASS, $class);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Changes attribute value of container
184
     *
185
     * @param $attribute
186
     * @param null $value
187
     * @return $this
188
     */
189
    public function attr($attribute, $value)
190
    {
191
        $this->setOption(self::OPTION_METHOD, "attr");
192
        $this->setOption(self::OPTION_ATTR, $attribute);
193
        $this->setOption(self::OPTION_VALUE, $value);
194
195
        return $this;
196
    }
197
}
198