Base::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Base file.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
/**
15
 * Base class.
16
 */
17
class Base
18
{
19
	/** @var array Values */
20
	protected $value;
21 5920
22
	/**
23 5920
	 * Constructor.
24 5920
	 *
25
	 * @param array $values
26
	 */
27
	public function __construct($values = [])
28
	{
29
		$this->value = $values;
30
	}
31
32
	/**
33
	 * Function to get the value if its safe to use for SQL Query (column).
34 2
	 *
35
	 * @param string $key
36 2
	 * @param bool   $skipEmtpy Skip the check if string is empty
37
	 *
38
	 * @return mixed Value for the given key
39
	 */
40
	public function getForSql($key, $skipEmtpy = true)
41
	{
42
		return Purifier::purifySql($this->get($key), $skipEmtpy);
43
	}
44
45
	/**
46 5927
	 * Function to get the value for a given key.
47
	 *
48 5927
	 * @param string $key
49
	 *
50
	 * @return mixed Value for the given key
51
	 */
52
	public function get($key)
53
	{
54
		return $this->value[$key] ?? null;
55
	}
56
57
	/**
58
	 * Function to get the html encoded value for a given key.
59
	 *
60
	 * @param string $key
61
	 *
62
	 * @return mixed
63
	 */
64
	public function getForHtml($key)
65
	{
66
		return Purifier::encodeHtml($this->get($key));
67
	}
68
69
	/**
70
	 * Function to get the array values for a given key.
71 1
	 *
72
	 * @param string $key
73 1
	 * @param array  $value
74
	 *
75
	 * @return array
76 1
	 */
77
	public function getArray($key, $value = [])
78
	{
79 1
		if (!isset($this->value[$key])) {
80
			return $value;
81 1
		}
82
		if (\is_string($this->value[$key]) && (0 === strpos($this->value[$key], '[') || 0 === strpos($this->value[$key], '{'))) {
83
			$value = Json::decode($this->value[$key]);
84
		} else {
85
			$value = (array) $this->value[$key];
86
		}
87
		return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value also could return the type string which is incompatible with the documented return type array.
Loading history...
88
	}
89
90
	/**
91
	 * Function to set the value for a given key.
92 106
	 *
93
	 * @param string $key
94 106
	 * @param mixed  $value
95 106
	 *
96
	 * @return $this
97
	 */
98
	public function set($key, $value)
99
	{
100
		$this->value[$key] = $value;
101
		return $this;
102
	}
103
104
	/**
105 96
	 * Function to set all the values.
106
	 *
107 96
	 * @param mixed $values
108
	 *
109 96
	 * @return $this
110
	 */
111
	public function setData($values)
112
	{
113
		$this->value = $values;
114
115
		return $this;
116
	}
117 24
118
	/**
119 24
	 * Function to get all the values of the Object.
120
	 *
121
	 * @return array
122
	 */
123
	public function getData()
124
	{
125
		return $this->value;
126
	}
127
128
	/**
129 5802
	 * Function to check if the key exists.
130
	 *
131 5802
	 * @param string $key
132
	 *
133
	 * @return bool
134
	 */
135
	public function has($key)
136
	{
137
		return isset($this->value[$key]);
138
	}
139
140
	/**
141 47
	 * Function to check if the key is empty.
142
	 *
143 47
	 * @param string $key
144
	 *
145
	 * @return bool
146
	 */
147
	public function isEmpty($key)
148
	{
149
		return empty($this->value[$key]);
150
	}
151
152
	/**
153
	 * Function to remove the value.
154
	 *
155
	 * @param string $key
156
	 */
157
	public function remove($key)
158
	{
159
		unset($this->value[$key]);
160
	}
161
162
	/**
163
	 * Function to get keys.
164
	 *
165
	 * @return string[]
166
	 */
167
	public function getKeys()
168
	{
169
		return array_keys($this->value);
170
	}
171
}
172