Completed
Push — master ( e46569...299406 )
by Shcherbak
02:51 queued 01:05
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
    protected $attributes = [
27 4
      'type' => self::TYPE_BUTTON,
28 4
    ];
29 4
30
31
    /**
32
     * @return $this
33
     */
34
    public function handle(FormData $request) {
35 2
      $this->isSubmitted = $request->get($this->getName()) !== null;
36 2
      return $this;
37 2
    }
38
39
40
    /**
41
     * @return bool
42
     */
43
    public function isSubmitted() {
44 2
      if ($this->getAttribute('type') != self::TYPE_SUBMIT) {
45 2
        return false;
46
      }
47
      return $this->isSubmitted;
48 2
    }
49
50
51
    /**
52
     * @param string $type
53
     * @return $this
54
     */
55
    public function setType($type) {
56 4
      if (!in_array($type, [
57 4
        self::TYPE_BUTTON,
58 4
        self::TYPE_RESET,
59 4
        self::TYPE_SUBMIT,
60 4
      ])
61
      ) {
62
        throw new \InvalidArgumentException('Invalid button type: ' . $type);
63 1
      }
64
      $this->setAttribute('type', $type);
65 3
      return $this;
66 3
    }
67
68
69
    /**
70
     * @return string
71
     */
72
    public function render() {
73 1
      $html = '<button ' . Html::renderAttributes($this->getAttributes()) . ' >';
74 1
      $html .= ($this->getText() ?: 'Button');
75 1
      $html .= '</button>';
76 1
77
      return $html;
78 1
    }
79
80
  }