Completed
Push — master ( 9be2e3...3a8fde )
by Gaël
14:21
created

Ressource::setParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 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)) {
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): void
50
    {
51
        if (!in_array($name, $this->keys)) {
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
            if (!in_array($fieldKey, $this->keys)) {
66
                throw Exception::invalidResourceParameter($fieldKey);
67
            }
68
69
            $this->parameters[$fieldKey] = $fieldValue;
70
        }
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getParameters(): array
77
    {
78
        return $this->parameters;
79
    }
80
}
81