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

Button   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 7
c 3
b 2
f 0
lcom 1
cbo 3
dl 0
loc 63
ccs 16
cts 17
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 4 1
A isSubmitted() 0 6 2
A setType() 0 8 2
A render() 0 7 2
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
  }