Completed
Branch 2.x (b4343e)
by Julián
05:20
created

ParameterTrait::__call()   A

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 2
1
<?php
2
3
/*
4
 * Unified push notification services abstraction (http://github.com/juliangut/tify).
5
 *
6
 * @license BSD-3-Clause
7
 * @link https://github.com/juliangut/tify
8
 * @author Julián Gutiérrez <[email protected]>
9
 */
10
11
namespace Jgut\Tify;
12
13
/**
14
 * Parameter handling.
15
 */
16
trait ParameterTrait
17
{
18
    /**
19
     * Parameter alias key map.
20
     *
21
     * @var array
22
     */
23
    protected $parameterAliasMap = [];
24
25
    /**
26
     * List of parameters.
27
     *
28
     * @var array
29
     */
30
    protected $parameters = [];
31
32
    /**
33
     * Get parameters.
34
     *
35
     * @return array
36
     */
37
    public function getParameters()
38
    {
39
        return $this->parameters;
40
    }
41
42
    /**
43
     * Set parameters.
44
     *
45
     * @param array $parameters
46
     *
47
     * @return $this
48
     */
49
    public function setParameters($parameters)
50
    {
51
        $this->parameters = [];
52
53
        foreach ($parameters as $parameter => $value) {
54
            $this->setParameter($parameter, $value);
55
        }
56
57
        return $this;
58
    }
59
60
    /**
61
     * Has parameter.
62
     *
63
     * @param string $parameter
64
     *
65
     * @return bool
66
     */
67
    public function hasParameter($parameter)
68
    {
69
        return array_key_exists($this->getMappedParameter($parameter), $this->parameters);
70
    }
71
72
    /**
73
     * Easy access to parameters.
74
     *
75
     * @param string $name
76
     * @param array  $arguments
0 ignored issues
show
Documentation introduced by
Should the type for parameter $arguments not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
77
     *
78
     * @return mixed
79
     */
80
    public function __call($name, array $arguments = null)
81
    {
82
        if (preg_match('/^get([A-Z].+)$/', $name, $matches)) {
83
            return $this->getParameter(lcfirst($matches[1]));
84
        }
85
86
        return;
87
    }
88
89
    /**
90
     * Get parameter.
91
     *
92
     * @param string $parameter
93
     * @param mixed  $default
94
     *
95
     * @return mixed
96
     */
97
    public function getParameter($parameter, $default = null)
98
    {
99
        $parameter = $this->getMappedParameter($parameter);
100
101
        return array_key_exists($parameter, $this->parameters) ? $this->parameters[$parameter] : $default;
102
    }
103
104
    /**
105
     * Set parameter.
106
     *
107
     * @param string $parameter
108
     * @param mixed  $value
109
     *
110
     * @return $this
111
     */
112
    public function setParameter($parameter, $value)
113
    {
114
        $this->parameters[$this->getMappedParameter($parameter)] = $value;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Get normalized service parameter.
121
     *
122
     * @param string $parameter
123
     *
124
     * @return string
125
     */
126
    protected function getMappedParameter($parameter)
127
    {
128
        $parameter = trim($parameter);
129
130
        if (array_key_exists($parameter, $this->parameterAliasMap)) {
131
            return $this->parameterAliasMap[$parameter];
132
        }
133
134
        return $parameter;
135
    }
136
}
137