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

Button::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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