Completed
Pull Request — master (#62)
by Tim
28:18
created

ParamsTrait::getParam()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 15
cp 0
rs 8.439
cc 5
eloc 10
nc 4
nop 2
crap 30
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\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-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli\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-cli-simple
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
        if ($paramsAvailable = reset($this->params)) {
78
            foreach ($paramsAvailable as $paramKey => $paramValue) {
79
                $params[$paramKey] = $this->getParam($paramKey);
80
            }
81
        }
82
83
        // return the params
84
        return $params;
85
    }
86
87
    /**
88
     * Query whether or not the param with the passed name exists.
89
     *
90
     * @param string $name The name of the param to be queried
91
     *
92
     * @return boolean TRUE if the requested param exists, else FALSE
93
     */
94
    public function hasParam($name)
95
    {
96
        return array_key_exists($name, $this->getParams());
97
    }
98
99
    /**
100
     * Return's the param with the passed name.
101
     *
102
     * @param string $name         The name of the param to return
103
     * @param mixed  $defaultValue The default value if the param doesn't exists
104
     *
105
     * @return string The requested param
106
     * @throws \Exception Is thrown, if the requested param is not available
107
     */
108
    public function getParam($name, $defaultValue = null)
109
    {
110
111
        // load the params
112
        $params = reset($this->params);
113
114
        // query whether or not, the param with the passed name is set
115
        if (is_array($params) && isset($params[$name])) {
116
            // load the value from the array
117
            $value = $params[$name];
118
            // query whether or not, the value contains a comma
119
            // => if yes, we explode it into an array
120
            if (stripos($value, $delimiter = $this->getParamDelimiter())) {
121
                $value = explode($delimiter, $value);
122
            }
123
124
            // return the found value
125
            return $value;
126
        }
127
128
        // if not, query we query if a default value has been passed
129
        if ($defaultValue != null) {
130
            return $defaultValue;
131
        }
132
133
        // throw an exception if neither the param exists or a default value has been passed
134
        throw new \Exception(sprintf('Requested param %s not available', $name));
135
    }
136
}
137