Completed
Push — master ( 020c5c...9dbce5 )
by Julián
08:49
created

ParameterTrait::getParameters()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 3
nc 1
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
/**
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
        $this->initializeParameters();
42
43
        return $this->parameters->toArray();
44
    }
45
46
    /**
47
     * Set parameters.
48
     *
49
     * @param array $parameters
50
     *
51
     * @return $this
52
     */
53
    public function setParameters($parameters)
54
    {
55
        if ($this->parameters instanceof ArrayCollection) {
56
            $this->parameters->clear();
57
        } else {
58
            $this->initializeParameters();
59
        }
60
61
        foreach ($parameters as $parameter => $value) {
62
            $this->parameters->set(trim($parameter), $value);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Has parameter.
70
     *
71
     * @param string $parameter
72
     *
73
     * @return bool
74
     */
75
    public function hasParameter($parameter)
76
    {
77
        $this->initializeParameters();
78
79
        return $this->parameters->containsKey($parameter);
80
    }
81
82
    /**
83
     * Get parameter.
84
     *
85
     * @param string $parameter
86
     * @param mixed  $default
87
     *
88
     * @return mixed
89
     */
90
    public function getParameter($parameter, $default = null)
91
    {
92
        $this->initializeParameters();
93
94
        return $this->parameters->containsKey($parameter) ? $this->parameters->get($parameter) : $default;
95
    }
96
97
    /**
98
     * Set parameter.
99
     *
100
     * @param string $parameter
101
     * @param mixed  $value
102
     *
103
     * @return $this
104
     */
105
    public function setParameter($parameter, $value)
106
    {
107
        $this->initializeParameters();
108
109
        $this->parameters->set(trim($parameter), $value);
110
111
        return $this;
112
    }
113
}
114