Ressource   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setParameters() 0 4 2
A getParameters() 0 3 1
A getParameter() 0 11 3
A setParameter() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
4
namespace DansMaCulotte\Monetico\Resources;
5
6
use DansMaCulotte\Monetico\Exceptions\Exception;
7
8
class Ressource
9
{
10
    /** @var array */
11
    protected $parameters = [];
12
13
    /** @var array */
14
    protected $keys = [];
15
16
    /**
17
     * Ressource constructor.
18
     * @param array $fields
19
     * @throws Exception
20
     */
21
    public function __construct(array $fields = [])
22
    {
23
        $this->setParameters($fields);
24
    }
25
26
    /**
27
     * @param string $name
28
     * @return mixed
29
     * @throws Exception
30
     */
31
    public function getParameter(string $name)
32
    {
33
        if (!in_array($name, $this->keys, true)) {
34
            throw Exception::invalidResourceParameter($name);
35
        }
36
37
        if (isset($this->parameters[$name]) === false) {
38
            return null;
39
        }
40
41
        return $this->parameters[$name];
42
    }
43
44
    /**
45
     * @param string $name
46
     * @param mixed $value
47
     * @throws Exception
48
     */
49
    public function setParameter(string $name, $value = null): void
50
    {
51
        if (!in_array($name, $this->keys, true)) {
52
            throw Exception::invalidResourceParameter($name);
53
        }
54
55
        $this->parameters[$name] = $value;
56
    }
57
58
    /**
59
     * @param array $fields
60
     * @throws Exception
61
     */
62
    public function setParameters(array $fields): void
63
    {
64
        foreach ($fields as $fieldKey => $fieldValue) {
65
            $this->setParameter($fieldKey, $fieldValue);
66
        }
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getParameters(): array
73
    {
74
        return $this->parameters;
75
    }
76
}
77