Completed
Pull Request — master (#44)
by Vitaliy
02:58
created

Button::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\FormData;
6
7
  class Button extends BaseElement {
8
9
    /**
10
     * just a button
11
     */
12
    const TYPE_BUTTON = 'button';
13
14
    /**
15
     * reset value from each element in form
16
     */
17
    const TYPE_RESET = 'reset';
18
19
    /**
20
     * submit form
21
     */
22
    const TYPE_SUBMIT = 'submit';
23
24
    protected $isSubmitted = false;
25
26
27 4
    public function __construct() {
28 4
      $this->setAttribute('type', self::TYPE_BUTTON);
29 4
    }
30
31
32
    /**
33
     * @return $this
34
     */
35 2
    public function handle(FormData $request) {
36 2
      $this->isSubmitted = $request->get($this->getName()) !== null;
37 2
      return $this;
38
    }
39
40
41
    /**
42
     * @return bool
43
     */
44 2
    public function isSubmitted() {
45 2
      if ($this->getAttribute('type') != self::TYPE_SUBMIT) {
46
        return false;
47
      }
48 2
      return $this->isSubmitted;
49
    }
50
51
52
    /**
53
     * @param string $type
54
     * @return $this
55
     */
56 4
    public function setType($type) {
57 4
      if (!in_array($type, [
58 4
        self::TYPE_BUTTON,
59 4
        self::TYPE_RESET,
60 4
        self::TYPE_SUBMIT,
61
      ])
62
      ) {
63 1
        throw new \InvalidArgumentException('Invalid button type: ' . $type);
64
      }
65 3
      $this->setAttribute('type', $type);
66 3
      return $this;
67
    }
68
69
70
    /**
71
     * @return string
72
     */
73 1
    public function render() {
74 1
      $html = '<button ' . Html::renderAttributes($this->getAttributes()) . ' >';
75 1
      $html .= ($this->getText() ?: 'Button');
76 1
      $html .= '</button>';
77
78 1
      return $html;
79
    }
80
81
  }