ParameterTrait::hasParameter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
14
/**
15
 * Parameter handling
16
 */
17
trait ParameterTrait
18
{
19
    /**
20
     * @var \Doctrine\Common\Collections\ArrayCollection
21
     */
22
    protected $parameters;
23
24
    /**
25
     * Initialize parameters collection.
26
     */
27
    protected function initializeParameters()
28
    {
29
        if ($this->parameters === null) {
30
            $this->parameters = new ArrayCollection;
31
        }
32
    }
33
34
    /**
35
     * Get parameters.
36
     *
37
     * @return array
38
     */
39
    public function getParameters()
40
    {
41
        if (!$this->parameters instanceof ArrayCollection) {
42
            return [];
43
        }
44
45
        return $this->parameters->toArray();
46
    }
47
48
    /**
49
     * Set parameters.
50
     *
51
     * @param array $parameters
52
     *
53
     * @return $this
54
     */
55
    public function setParameters($parameters)
56
    {
57
        if ($this->parameters instanceof ArrayCollection) {
58
            $this->parameters->clear();
59
        } else {
60
            $this->initializeParameters();
61
        }
62
63
        foreach ($parameters as $parameter => $value) {
64
            $this->setParameter(trim($parameter), $value);
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * Has parameter.
72
     *
73
     * @param string $parameter
74
     *
75
     * @return bool
76
     */
77
    public function hasParameter($parameter)
78
    {
79
        if (!$this->parameters instanceof ArrayCollection) {
80
            return false;
81
        }
82
83
        return $this->parameters->containsKey($parameter);
84
    }
85
86
    /**
87
     * Get parameter.
88
     *
89
     * @param string $parameter
90
     * @param mixed  $default
91
     *
92
     * @return mixed
93
     */
94
    public function getParameter($parameter, $default = null)
95
    {
96
        if (!$this->parameters instanceof ArrayCollection) {
97
            return $default;
98
        }
99
100
        return $this->parameters->containsKey($parameter) ? $this->parameters->get($parameter) : $default;
101
    }
102
103
    /**
104
     * Set parameter.
105
     *
106
     * @param string $parameter
107
     * @param mixed  $value
108
     *
109
     * @return $this
110
     */
111
    public function setParameter($parameter, $value)
112
    {
113
        $this->initializeParameters();
114
115
        $this->parameters->set(trim($parameter), $value);
116
117
        return $this;
118
    }
119
}
120