Button   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
c 2
b 1
f 0
lcom 3
cbo 2
dl 0
loc 70
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isClicked() 0 4 1
A setIcon() 0 5 1
A setGlyphIcon() 0 5 1
A restoreValue() 0 4 1
A getAttributes() 0 6 1
A getClasses() 0 9 2
A getHTML() 0 7 3
A getJsonData() 0 6 1
1
<?php
2
3
namespace fieldwork\components;
4
5
use fieldwork\methods\Method;
6
7
class Button extends Field
8
{
9
10
    const TYPE_SUBMIT = "submit";
11
    const TYPE_BUTTON = "button";
12
13
    private $type, $icon, $glyphIcon, $isClicked = false;
14
15
    public function __construct ($name, $label, $value = "", $type = Button::TYPE_BUTTON)
16
    {
17
        parent::__construct($name, $label, $value);
18
        $this->type        = $type;
19
        $this->collectData = false;
20
    }
21
22
    public function isClicked ()
23
    {
24
        return $this->isClicked;
25
    }
26
27
    public function setIcon ($file)
28
    {
29
        $this->icon = $file;
30
        return $this;
31
    }
32
33
    public function setGlyphIcon ($code)
34
    {
35
        $this->glyphIcon = $code;
36
        return $this;
37
    }
38
39
    public function restoreValue (Method $method, $sanitize = true)
40
    {
41
        $this->isClicked = $method->hasValue($this->getName());
42
    }
43
44
    public function getAttributes ()
45
    {
46
        return array_merge(parent::getAttributes(), array(
47
            'type' => $this->type
48
        ));
49
    }
50
51
    public function getClasses ()
52
    {
53
        return array_merge(
54
            parent::getClasses(), array(
55
                'button',
56
                $this->type === self::TYPE_SUBMIT ? 'btn btn-primary' : 'btn-default'
57
            )
58
        );
59
    }
60
61
    public function getHTML ($showLabel = true)
62
    {
63
        $icon      = ($this->icon == '' ? '' : '<img class="button-icon" src="' . $this->icon . '"> ');
64
        $glyphIcon = !empty($this->glyphIcon) ? '<i class="button-icon icon-' . $this->glyphIcon . '"></i> ' : '';
65
        return
66
            "<button " . $this->getAttributesString() . ">" . $icon . $glyphIcon . $this->label . "</button>";
67
    }
68
69
    public function getJsonData ()
70
    {
71
        return array_merge(parent::getJsonData(), array(
72
            'isButton' => true
73
        ));
74
    }
75
76
}