TextField::getHTML()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 16
rs 8.8571
cc 5
eloc 14
nc 16
nop 1
1
<?php
2
3
namespace fieldwork\components;
4
5
class TextField extends Field
6
{
7
8
    private $default_showLabel = false;
9
10
    const ON_ENTER_NEXT   = 'next';
11
    const ON_ENTER_SUBMIT = 'submit';
12
13
    private $onEnter = '',
14
        $wrapNode = true,
15
        $mask = null;
16
17
    /**
18
     * Whether to show the field's label by default
19
     *
20
     * @param boolean $default_showLabel
21
     *
22
     * @return $this
23
     */
24
    public function setDefaultShowLabel ($default_showLabel)
25
    {
26
        $this->default_showLabel = $default_showLabel;
27
        return $this;
28
    }
29
30
    public function getAttributes ()
31
    {
32
        $att = array();
33
        if (!empty($this->onEnter))
34
            $att['data-input-action-on-enter'] = $this->onEnter;
35
        if ($this->mask !== null)
36
            $att['data-input-mask'] = $this->mask;
37
        return array_merge(parent::getAttributes(), $att);
38
    }
39
40
    public function onEnter ($action = '')
41
    {
42
        $this->onEnter = $action;
43
        return $this;
44
    }
45
46
    /**
47
     * Sets input mask for this field
48
     *
49
     * @param string $mask
50
     *
51
     * @return static
52
     */
53
    public function setMask ($mask)
54
    {
55
        $this->mask = $mask;
56
        return $this;
57
    }
58
59
    public function getClasses ()
60
    {
61
        return array_merge(
62
            parent::getClasses(), array('textfield', 'fieldwork-inputfield')
63
        );
64
    }
65
66
    /**
67
     * Sets whether to wrap the node in an input-field node
68
     *
69
     * @param boolean $wrapNode
70
     *
71
     * @return static
72
     */
73
    public function setWrapNode ($wrapNode)
74
    {
75
        $this->wrapNode = $wrapNode;
76
        return $this;
77
    }
78
79
    /**
80
     * @param bool|null $showLabel
81
     * @return string
82
     */
83
    public function getHTML ($showLabel = null)
84
    {
85
        $before = $this->wrapNode ? "<div class=\"input-field\">" : "";
86
        $after  = $this->wrapNode ? "</div>" : "";
87
        if ($showLabel === null)
88
            $showLabel = $this->default_showLabel;
89
        if ($showLabel)
90
            return sprintf("$before<input type='text' %s><label for=\"%s\">%s</label>$after",
91
                $this->getAttributesString(),
92
                $this->getId(),
93
                $this->label);
94
        else
95
            return sprintf("$before<input placeholder='%s' type='text' %s>$after",
96
                $this->label,
97
                $this->getAttributesString());
98
    }
99
100
}