Completed
Pull Request — master (#44)
by Vitaliy
04:21
created

Button   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 58
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 11 2
A render() 0 7 1
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\FormData;
6
7
  class Button extends BaseElement {
8
9
    // just a button
10
    const TYPE_BUTTON = 'button';
11
12
    // reset value from each element in form
13
    const TYPE_RESET = 'reset';
14
15
    // submit form
16
    const TYPE_SUBMIT = 'submit';
17
18
    protected $isSubmitted = false;
19
20
21 4
    public function __construct() {
22 4
      $this->setAttribute('type', self::TYPE_BUTTON);
23 4
    }
24
25
26
    /**
27
     * @return $this
28
     */
29 2
    public function handle(FormData $request) {
30 2
      $this->isSubmitted = $request->get($this->getName()) !== null;
31 2
      return $this;
32
    }
33
34
35 2
    public function isSubmitted() {
36 2
      if ($this->getAttribute('type') != self::TYPE_SUBMIT) {
37
        return false;
38
      }
39 2
      return $this->isSubmitted;
40
    }
41
42
43 4
    public function setType(string $type) {
44 4
      if (!in_array($type, [
45 4
        self::TYPE_BUTTON,
46 4
        self::TYPE_RESET,
47 4
        self::TYPE_SUBMIT,
48
      ])
49
      ) {
50 1
        throw new \InvalidArgumentException('Invalid button type: ' . $type);
51
      }
52 3
      $this->setAttribute('type', $type);
53 3
    }
54
55
56 1
    public function render() : string {
57 1
      $html = '<button ' . Html::renderAttributes($this->getAttributes()) . ' >';
58 1
      $html .= ($this->getText() ?? 'Submit');
59 1
      $html .= '</button>';
60
61 1
      return $html;
62
    }
63
64
  }