ParameterHolder::addParametersIfNot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PEIP\Data;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * ParameterHolder
15
 * Class to act as a simple holder for parameters as key/value pairs.
16
 * Can act as a base class for classes dealing with parameters.
17
 *
18
 * @author Timo Michna <timomichna/yahoo.de>
19
 * @implements \PEIP\INF\Data\ParameterHolder
20
 */
21
class ParameterHolder implements \PEIP\INF\Data\ParameterHolder
22
{
23
    protected $parameters = [];
24
25
    /**
26
     * Checks wether a given parameter is set.
27
     *
28
     * @param string $key name of parameter
29
     *
30
     * @return bool wether parameter is set
31
     */
32
    public function hasParameter($key)
33
    {
34
        return array_key_exists($key, $this->parameters);
35
    }
36
37
    /**
38
     * returns a given parameter value.
39
     *
40
     * @param string $key name of parameter
41
     *
42
     * @return
43
     */
44
    public function getParameter($key)
45
    {
46
        return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : null;
47
    }
48
49
    /**
50
     * Deletes a given parameter.
51
     *
52
     * @param string $key name of parameter
53
     *
54
     * @return
55
     */
56
    public function deleteParameter($key)
57
    {
58
        unset($this->parameters[$key]);
59
    }
60
61
    /**
62
     * Sets the value for a given parameter.
63
     *
64
     * @param string $key   name of parameter
65
     * @param string $value value to set for the parameter
66
     *
67
     * @return
68
     */
69
    public function setParameter($key, $value)
70
    {
71
        $this->parameters[$key] = $value;
72
    }
73
74
    /**
75
     * Sets all parameters as key/value pairs.
76
     *
77
     * @param array $parameters parameters as key/value pairs
78
     *
79
     * @return
80
     */
81
    public function setParameters(array $parameters)
82
    {
83
        $this->parameters = $parameters;
84
    }
85
86
    /**
87
     * returns parameters as key/value pairs.
88
     *
89
     * @return array $parameters parameters as key/value pairs
90
     */
91
    public function getParameters()
92
    {
93
        return $this->parameters;
94
    }
95
96
    /**
97
     * Adds parameters as key/value pairs.
98
     * Overwrites value for a parameter if allready been set.
99
     *
100
     * @param array $parameters parameters as key/value pairs
101
     *
102
     * @return
103
     */
104
    public function addParameters(array $parameters)
105
    {
106
        $this->parameters = array_merge($this->parameters, $parameters);
107
        array_unique($this->parameters);
108
    }
109
110
    /**
111
     * Adds parameters as key/value pairs only if parameter has not allready
112
     * been set.
113
     *
114
     * @param array $parameters parameters as key/value pairs
115
     *
116
     * @return
117
     */
118
    public function addParametersIfNot(array $parameters)
119
    {
120
        $this->parameters = array_merge($parameters, $this->parameters);
121
        array_unique($this->parameters);
122
    }
123
}
124