Button::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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
      'type' => self::TYPE_BUTTON,
28
    ];
29
30
31
    /**
32
     * @return $this
33
     */
34
    public function handle(FormData $request) {
35
      $this->isSubmitted = $request->has($this->getName());
36
      return $this;
37
    }
38
39
40
    public function isSubmitted() : bool {
41
      if ($this->getAttribute('type') !== self::TYPE_SUBMIT) {
42
        return false;
43
      }
44
      return $this->isSubmitted;
45
    }
46
47
48
    public function setType(string $type) : self {
49
      if (!in_array($type, [self::TYPE_BUTTON, self::TYPE_RESET, self::TYPE_SUBMIT])) {
50
        throw new \InvalidArgumentException('Invalid button type: ' . $type);
51
      }
52
      $this->setAttribute('type', $type);
53
54
      return $this;
55
    }
56
57
58
    /**
59
     * @return string
60
     */
61
    public function render() {
62
      $html = '<button ' . Html::renderAttributes($this->getAttributes()) . ' >';
63
      $html .= ($this->getText() ?: 'Button');
64
      $html .= '</button>';
65
66
      return $html;
67
    }
68
69
  }