Component::isValid()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
namespace fieldwork\components;
4
5
abstract class Component
6
{
7
8
    const RM_NONE        = 'none';
9
    const RM_HIDDENFIELD = 'hidden';
10
    const RM_DEFAULT     = 'default';
11
12
    protected
13
        $parent,
14
        $children         = array(),
15
        $customAttributes = array();
16
    private
17
        $slug,
18
        $active           = true,
19
        $customClasses    = array();
20
21
    public function __construct ($slug)
22
    {
23
        $this->slug = $slug;
24
    }
25
26
    /**
27
     * Resets the component
28
     */
29
    public function reset ()
30
    {
31
        foreach ($this->getChildren(false) as $child)
32
            /* @var $child Component */
33
            $child->reset();
34
    }
35
36
    /**
37
     * Gets the HTML markup of the component
38
     *
39
     * @param bool $showLabel
40
     *
41
     * @return mixed
42
     */
43
    public abstract function getHTML ($showLabel = true);
44
45
    /**
46
     * Echoes the HTML markup of the component
47
     * @return $this
48
     */
49
    public final function output ()
50
    {
51
        echo $this->getHTML(true);
52
        return $this;
53
    }
54
55
    public abstract function isValid ();
56
57
    public function isVisible ()
58
    {
59
        return true;
60
    }
61
62
    protected function add (Component $component)
63
    {
64
        $this->children[] = $component;
65
    }
66
67
    public function renderWhenHidden ()
68
    {
69
        return Field::RM_HIDDENFIELD;
70
    }
71
72
    public function renderHiddenField ()
73
    {
74
        return "<input type='hidden'" . $this->getAttributesString() . ">";
75
    }
76
77
    protected function getCustomClasses ()
78
    {
79
        return $this->customClasses;
80
    }
81
82
    /**
83
     * Adds component to given parent component
84
     *
85
     * @param Component $parent
86
     *
87
     * @return $this
88
     */
89
    public function addTo (Component $parent)
90
    {
91
        $parent->add($this);
92
        $this->parent = $parent;
93
        return $this;
94
    }
95
96
    /**
97
     * Adds class(es) to this component's node
98
     *
99
     * @param string|array $class
100
     *
101
     * @return $this
102
     */
103
    public function addClass ($class)
104
    {
105
        $targetArray = &$this->customClasses;
106
        if (!is_array($class))
107
            $targetArray[] = $class;
108
        else
109
            $targetArray = array_merge($targetArray, $class);
110
        return $this;
111
    }
112
113
    /**
114
     * Sets a custom attribute
115
     *
116
     * @param string $attr  Attribute name
117
     * @param string $value Attribtue value
118
     *
119
     * @return $this
120
     */
121
    public function attr ($attr, $value)
122
    {
123
        $this->customAttributes[$attr] = $value;
124
        return $this;
125
    }
126
127
    /**
128
     * Sets whether the component is active
129
     *
130
     * @param boolean $active
131
     *
132
     * @return $this
133
     */
134
    public function setActive ($active = true)
135
    {
136
        $this->active = $active;
137
        return $this;
138
    }
139
140
    /**
141
     * Checks whether the component is active
142
     * @return boolean
143
     */
144
    public function isActive ()
145
    {
146
        return $this->active;
147
    }
148
149
    /**
150
     * Returns a flat list of the component's enabled children
151
     *
152
     * @param boolean $recursive
153
     * @param boolean $includeInactiveFields whether to include inactive fields
154
     *
155
     * @return array
156
     */
157
    public function getChildren ($recursive = true, $includeInactiveFields = false)
158
    {
159
        $children = array();
160
        foreach ($this->children as $component)
161
            /* @var $component Component */
162
            if ($component->isActive() || $includeInactiveFields) {
163
                array_push($children, $component);
164
                if ($recursive)
165
                    $children = array_merge($children, $component->getChildren(true, $includeInactiveFields));
166
            }
167
        return $children;
168
    }
169
170
    /**
171
     * Check if given component is child
172
     *
173
     * @param Component $child     component to search for
174
     * @param boolean   $recursive whether or not to search recursively
175
     *
176
     * @return boolean
177
     */
178
    public function hasChild ($child, $recursive = true)
179
    {
180
        foreach ($this->children as $component)
181
            /* @var $component Component */
182
            if ($component == $child || ($recursive && $component->hasChild($child, true)))
183
                return true;
184
        return false;
185
    }
186
187
    /**
188
     * Gets all HTML attributes
189
     * @return array array of attributes
190
     */
191
    public function getAttributes ()
192
    {
193
        return array_merge(
194
            $this->customAttributes, array('class' => implode(' ', $this->getClasses())));
195
    }
196
197
    /**
198
     * Gets all HTML attributes
199
     * @return string attributes as string
200
     */
201
    public function getAttributesString ()
202
    {
203
        $attributePairs = array();
204
        foreach ($this->getAttributes() as $attr => $value)
205
            $attributePairs[] = sprintf('%s="%s"', $attr, str_replace('"', '&quot;', $value));
206
        return implode(' ', $attributePairs);
207
    }
208
209
    /**
210
     * Gets HTML class attribute
211
     * @return array array of classes
212
     */
213
    public function getClasses ()
214
    {
215
        return array_merge(array(
216
            'jfcomponent'
217
        ), $this->customClasses);
218
    }
219
220
    public function getLocalSlug ()
221
    {
222
        return $this->slug;
223
    }
224
225
    public function getGlobalSlug ()
226
    {
227
        if ($this->parent instanceof Component)
228
            return $this->parent->getGlobalSlug() . '-' . $this->slug;
229
        else
230
            return $this->slug;
231
    }
232
233
    /**
234
     * Gets an array of the custom classes, each of them prefixed with wrap-
235
     * @return array
236
     */
237
    public function getWrapperClasses ()
238
    {
239
        $wrapperClasses = [];
240
        foreach ($this->customClasses as $customClass)
241
            $wrapperClasses[] = "wraps-$customClass";
242
        return $wrapperClasses;
243
    }
244
245
    /**
246
     * @return Component
247
     */
248
    public function getRoot ()
249
    {
250
        if ($this->parent instanceof Component)
251
            return $this->parent->getRoot();
252
        else
253
            return $this;
254
    }
255
256
}