Completed
Push — master ( e46569...299406 )
by Shcherbak
02:51 queued 01:05
created

Button   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.65%

Importance

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

4 Methods

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