Completed
Push — master ( 613542...020c5c )
by Julián
03:39
created

ParametersTrait::initializeParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
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
trait ParametersTrait
15
{
16
    /**
17
     * @var \Doctrine\Common\Collections\ArrayCollection
18
     */
19
    protected $parameters = [];
20
21
    /**
22
     * Initialize options collection.
23
     */
24
    protected function initializeParameters()
25
    {
26
        if (!$this->parameters instanceof ArrayCollection) {
27
            $this->parameters = new ArrayCollection;
28
        }
29
    }
30
31
    /**
32
     * Has parameter.
33
     *
34
     * @param string $parameter
35
     *
36
     * @return bool
37
     */
38
    public function hasParameter($parameter)
39
    {
40
        $this->initializeParameters();
41
42
        return $this->parameters->containsKey($parameter);
43
    }
44
45
    /**
46
     * Get parameter.
47
     *
48
     * @param string $parameter
49
     * @param mixed  $default
50
     *
51
     * @return mixed
52
     */
53
    public function getParameter($parameter, $default = null)
54
    {
55
        $this->initializeParameters();
56
57
        return $this->parameters->containsKey($parameter) ? $this->parameters->get($parameter) : $default;
58
    }
59
60
    /**
61
     * Get parameters.
62
     *
63
     * @return array
64
     */
65
    public function getParameters()
66
    {
67
        $this->initializeParameters();
68
69
        return $this->parameters;
70
    }
71
72
    /**
73
     * Set parameters.
74
     *
75
     * @param array $parameters
76
     *
77
     * @return $this
78
     */
79
    public function setParameters($parameters)
80
    {
81
        if (!$this->parameters instanceof ArrayCollection) {
82
            $this->parameters->clear();
83
        } else {
84
            $this->initializeParameters();
85
        }
86
87
        foreach ($parameters as $parameter => $value) {
88
            $this->parameters->set($parameter, $value);
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set parameter.
96
     *
97
     * @param string $parameter
98
     * @param mixed  $value
99
     *
100
     * @return $this
101
     */
102
    public function setParameter($parameter, $value)
103
    {
104
        $this->initializeParameters();
105
106
        $this->parameters->set($parameter, $value);
107
108
        return $this;
109
    }
110
}
111