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

Button   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.65%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 8
c 2
b 2
f 0
lcom 1
cbo 3
dl 0
loc 75
ccs 22
cts 23
cp 0.9565
rs 10

5 Methods

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