Completed
Pull Request — master (#28)
by Vitaliy
02:38 queued 19s
created

FormData   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 82
ccs 20
cts 21
cp 0.9524
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A getMethod() 0 3 1
A isMethod() 0 3 1
A has() 0 3 1
A get() 0 6 2
A getData() 0 3 1
1
<?php
2
3
4
  namespace Fiv\Form;
5
6
7
  /**
8
   *
9
   */
10
  class FormData {
11
12
    CONST METHOD_GET = 'GET';
13
14
    CONST METHOD_POST = 'POST';
15
16
    /**
17
     * @var string
18
     */
19
    protected $method = null;
20
21
    /**
22
     * @var array
23
     */
24
    protected $data = [];
25
26
27
    /**
28
     * @param string $method
29
     * @param array $attributes
30
     */
31 28
    public function __construct($method, array $attributes = []) {
32 28
      if (!is_string($method)) {
33
        throw new \InvalidArgumentException('Parameter "method" should be a string,  ' . gettype($method) . ' given.');
34
      }
35 28
      $method = strtoupper($method);
36 28
      if (!in_array($method, [self::METHOD_GET, self::METHOD_POST])) {
37 1
        throw new \InvalidArgumentException('Invalid http method name.');
38
      }
39 27
      $this->method = $method;
40 27
      $this->data = $attributes;
41 27
    }
42
43
44
    /**
45
     * @return string
46
     */
47 1
    public function getMethod() {
48 1
      return $this->method;
49
    }
50
51
52
    /**
53
     * @param string $method
54
     * @return bool
55
     */
56 20
    public function isMethod($method) {
57 20
      return $this->method == mb_strtoupper($method);
58
    }
59
60
61
    /**
62
     * @param string $name
63
     * @return bool
64
     */
65 20
    public function has($name) {
66 20
      return isset($this->data[$name]);
67
    }
68
69
70
    /**
71
     * @param string $name
72
     * @param mixed $defaultValue
73
     * @return mixed|null
74
     */
75 25
    public function get($name, $defaultValue = null) {
76 25
      if (!isset($this->data[$name])) {
77 13
        return $defaultValue;
78
      }
79 23
      return $this->data[$name];
80
    }
81
82
83
    /**
84
     * @return array
85
     */
86 20
    public function getData() {
87 20
      return $this->data;
88
    }
89
90
91
  }