Completed
Push — master ( 69a150...63893a )
by Shcherbak
02:37
created

Button::isSubmitted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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