Completed
Push — 15.x ( 00a1cc...df859a )
by Tim
01:21
created

ParamsTrait::getParam()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Configuration;
22
23
use JMS\Serializer\Annotation\Type;
24
use JMS\Serializer\Annotation\SerializedName;
25
26
/**
27
 * A trait implementation that provides parameter handling.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-configuration-jms
33
 * @link      http://www.techdivision.com
34
 */
35
trait ParamsTrait
36
{
37
38
    /**
39
     * The array with the formatter's params.
40
     *
41
     * @var array
42
     * @Type("array")
43
     */
44
    protected $params = array();
45
46
    /**
47
     * The delimiter for the params that should be converted into an array.
48
     *
49
     * @var string
50
     * @Type("string")
51
     * @SerializedName("param-delimiter")
52
     */
53
    protected $paramDelimiter = ',';
54
55
    /**
56
     * Return's the parameter delimiter.
57
     *
58
     * @return string The delimiter
59
     */
60
    public function getParamDelimiter()
61
    {
62
        return $this->paramDelimiter;
63
    }
64
65
    /**
66
     * Return's the array with the params.
67
     *
68
     * @return array The params
69
     */
70
    public function getParams()
71
    {
72
73
        // initialize the array for the params
74
        $params = array();
75
76
        // prepare the params, e. g. explode them into an array
77
        foreach (array_keys($this->params) as $paramKey) {
78
            $params[$paramKey] = $this->getParam($paramKey);
79
        }
80
81
        // return the params
82
        return $params;
83
    }
84
85
    /**
86
     * Query whether or not the param with the passed name exists.
87
     *
88
     * @param string $name The name of the param to be queried
89
     *
90
     * @return boolean TRUE if the requested param exists, else FALSE
91
     */
92
    public function hasParam($name)
93
    {
94
        return array_key_exists($name, $this->getParams());
95
    }
96
97
    /**
98
     * Return's the param with the passed name.
99
     *
100
     * @param string $name         The name of the param to return
101
     * @param mixed  $defaultValue The default value if the param doesn't exists
102
     *
103
     * @return string The requested param
104
     * @throws \Exception Is thrown, if the requested param is not available
105
     */
106
    public function getParam($name, $defaultValue = null)
107
    {
108
109
        // query whether or not, the param with the passed name is set
110
        if (isset($this->params[$name])) {
111
            // load the value from the array
112
            $value = $this->params[$name];
113
            // query whether or not, the value contains a comma => if yes, we explode it into an array
114
            if (is_string($value) && stripos($value, $delimiter = $this->getParamDelimiter())) {
115
                $value = explode($delimiter, $value);
116
            }
117
118
            // return the found value
119
            return $value;
120
        }
121
122
        // if not, query we query if a default value has been passed
123
        if ($defaultValue != null) {
124
            return $defaultValue;
125
        }
126
127
        // throw an exception if neither the param exists or a default value has been passed
128
        throw new \Exception(sprintf('Requested param %s not available', $name));
129
    }
130
}
131