Data::setParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Data.php
4
 *
5
 * @copyright	More in license.md
6
 * @license		https://www.ipublikuj.eu
7
 * @author		Adam Kadlec <[email protected]>
8
 * @package		iPublikuj:Widgets!
9
 * @subpackage	Entities
10
 * @since		5.0
11
 *
12
 * @date		15.09.14
13
 */
14
15
namespace IPub\Widgets\Entities;
16
17
use Nette;
18
use Nette\Utils;
19
20
class Data implements IData
21
{
22
	/**
23
	 * Implement nette smart magic
24
	 */
25
	use Nette\SmartObject;
26
27
	/**
28
	 * @var string
29
	 */
30
	protected $title;
31
32
	/**
33
	 * @var string
34
	 */
35
	protected $description;
36
37
	/**
38
	 * @var string
39
	 */
40
	protected $position;
41
42
	/**
43
	 * @var int
44
	 */
45
	protected $priority;
46
47
	/**
48
	 * @var bool
49
	 */
50
	protected $status;
51
52
	/**
53
	 * @var array
54
	 */
55
	protected $params = [];
56
57
	/**
58
	 * @param array $data
59
	 */
60
	public function __construct(array $data = NULL)
61
	{
62
		if ($data != NULL) {
63
			foreach ($data as $property => $value) {
64
				if (property_exists($this, $property)) {
65
					$this->{$property} = $value;
66
				}
67
			}
68
		}
69
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public function getTitle() : ?string
75
	{
76
		return $this->title;
77
	}
78
79
	/**
80
	 * {@inheritdoc}
81
	 */
82
	public function getDescription() : ?string
83
	{
84
		return $this->description;
85
	}
86
87
	/**
88
	 * {@inheritdoc}
89
	 */
90
	public function getPosition() : string
91
	{
92
		return $this->position;
93
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98
	public function getPriority() : int
99
	{
100
		return $this->priority;
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function getStatus() : bool
107
	{
108
		return $this->status ? TRUE : FALSE;
109
	}
110
111
	/**
112
	 * {@inheritdoc}
113
	 */
114
	public function getStyle() : string
115
	{
116
		return $this->getParam('template.style', 'default');
117
	}
118
119
	/**
120
	 * {@inheritdoc}
121
	 */
122
	public function getBadge() : ?string
123
	{
124
		return $this->getParam('template.badge', NULL);
125
	}
126
127
	/**
128
	 * {@inheritdoc}
129
	 */
130
	public function getIcon() : ?string
131
	{
132
		return $this->getParam('template.icon', NULL);
133
	}
134
135
	/**
136
	 * {@inheritdoc}
137
	 */
138
	public function setParams(array $params) : void
139
	{
140
		$this->params = $params;
141
	}
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146
	public function getParams() : array
147
	{
148
		return $this->params;
149
	}
150
151
	/**
152
	 * {@inheritdoc}
153
	 */
154
	public function setParam(string $key, ?string $value = NULL) : void
155
	{
156
		$keys = explode('.', $key);
157
158
		if (count($keys) > 1) {
159
			$val = &$this->params;
160
			$last = array_pop($keys);
161
162
			foreach ($keys as $key) {
163
				if (!isset($val[$key]) || !is_array($val[$key])) {
164
									$val[$key] = array();
165
				}
166
167
				$val = &$val[$key];
168
			}
169
170
			$val[$last] = $value;
171
172
		} else {
173
			$this->params[$keys[0]] = $value;
174
		}
175
	}
176
177
	/**
178
	 * {@inheritdoc}
179
	 */
180
	public function getParam(string $key, $default = NULL) : ?string
181
	{
182
		$keys = explode('.', $key);
183
184
		if (array_key_exists($keys[0], $this->params)) {
185
			if (is_array($this->params[$keys[0]]) || $this->params[$keys[0]] instanceof Utils\ArrayHash) {
0 ignored issues
show
Bug introduced by
The class Nette\Utils\ArrayHash does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
186
				$val = NULL;
187
188
				foreach ($keys as $key) {
189
					if (isset($val)) {
190
						if (isset($val[$key])) {
191
							$val = $val[$key];
192
						} else {
193
							$val = NULL;
194
						}
195
196
					} else {
197
						$val = isset($this->params[$key]) ? $this->params[$key] : $default;
198
					}
199
				}
200
201
				return (isset($val) ? $val : $default);
202
203
			} else {
204
				return trim($this->params[$keys[0]]);
205
			}
206
		}
207
208
		return $default;
209
	}
210
}
211