Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit d5454b8

Browse files
committed
Cleanup of Data class, documentation and code duplication
1 parent 1bdab00 commit d5454b8

File tree

1 file changed

+58
-54
lines changed

1 file changed

+58
-54
lines changed

Data/Data.php

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,23 @@ class Data implements DataInterface
1919
{
2020

2121
/**
22-
* internal data storage
22+
* Internal data storage.
2323
*
2424
* @var array
2525
*/
2626
private $data = array();
2727

2828
/**
29-
* clears the data(!) content of the object
29+
* Clears the data(!) content of the object.
3030
*/
3131
public function clear()
3232
{
3333
$this->data = array();
3434
}
3535

3636
/**
37-
* generic set method for multidimensional storage
37+
* Generic set method for multidimensional storage.
38+
*
3839
* $this->set( $key1, $key2, $key3, ..., $val )
3940
*
4041
* @throws \InvalidArgumentException
@@ -62,15 +63,15 @@ public function set()
6263
$this->data = array_replace_recursive($this->data, $replace);
6364
} else {
6465
throw new \InvalidArgumentException(
65-
'-- ( ' . __FILE__ . ' ) ( ' . __LINE__ . ' ) -- Not enough Parameters, need at least key + val'
66+
'Data::set() - Not enough Parameters, need at least key + val'
6667
);
6768
}
6869

6970
return $this;
7071
}
7172

7273
/**
73-
* set list of key/value pairs via one dimensional array
74+
* Set list of key/value pairs via one dimensional array.
7475
*
7576
* @param array $param
7677
* @return $this
@@ -85,15 +86,15 @@ public function setByArray(array $param)
8586
}
8687
} else {
8788
throw new \InvalidArgumentException(
88-
'-- ( ' . __FILE__ . ' ) ( ' . __LINE__ . ' ) -- $param is empty!'
89+
'Data::setByArray() - $param is empty!'
8990
);
9091
}
9192

9293
return $this;
9394
}
9495

9596
/**
96-
* adds given object's properties to self
97+
* Adds given object's properties to self.
9798
*
9899
* @param object $param
99100
* @return $this
@@ -114,14 +115,16 @@ public function setByObject($param)
114115
}
115116
}
116117
} else {
117-
throw new \InvalidArgumentException('-- ( ' . __FILE__ . ' ) ( ' . __LINE__ . ' ) -- param is no object!');
118+
throw new \InvalidArgumentException(
119+
'Data::setByObject() - param is no object!'
120+
);
118121
}
119122

120123
return $this;
121124
}
122125

123126
/**
124-
* fill datastore from json string
127+
* Fill datastore from json string.
125128
*
126129
* @param string $json
127130
* @return $this|mixed
@@ -136,42 +139,23 @@ public function setByJson($json)
136139
}
137140

138141
/**
139-
* multidimensional getter
140-
* find a key structure in a multidimensional array and return the value
141-
* params are stackable -> get( $k1, $k2, $k3, ... )
142+
* Multidimensional getter.
143+
*
144+
* Find a key structure in a multidimensional array and return the value
145+
* params are stackable -> get( $k1, $k2, $k3, ... ).
142146
*
143147
* @return bool|mixed
144148
*/
145149
public function get()
146150
{
147-
$data = $this->data;
148-
$default = false;
149-
$args = func_get_args();
150-
151-
// check for default return value
152-
if (1 < count($args)) {
153-
$lastElm = array_pop($args);
154-
if (empty($lastElm) && !is_numeric($lastElm)) {
155-
$default = $lastElm;
156-
} else {
157-
// push the last element back into array
158-
array_push($args, $lastElm);
159-
}
160-
}
161-
162-
foreach ($args as $key) {
163-
if (array_key_exists($key, $data)) {
164-
$data = $data[$key];
165-
} else {
166-
return $default;
167-
}
168-
}
169-
170-
return $data;
151+
return self::searchArray(
152+
func_get_args(),
153+
$this->data
154+
);
171155
}
172156

173157
/**
174-
* gets key index
158+
* Return all keys of internal array's first level.
175159
*
176160
* @return array keylist
177161
*/
@@ -181,7 +165,7 @@ public function getKeys()
181165
}
182166

183167
/**
184-
* remove key from container
168+
* Remove key from container.
185169
*
186170
* @param string $key
187171
* @return $this
@@ -196,7 +180,7 @@ public function remove($key)
196180
}
197181

198182
/**
199-
* return stored data array
183+
* Return stored data array.
200184
*
201185
* @return array
202186
*/
@@ -206,6 +190,8 @@ public function toArray()
206190
}
207191

208192
/**
193+
* Convert internal data to json.
194+
*
209195
* @return string
210196
*/
211197
public function toJson()
@@ -215,7 +201,7 @@ public function toJson()
215201

216202

217203
/**
218-
* return count of all firstlevel elements
204+
* Return count of all firstlevel elements.
219205
*
220206
* @return int
221207
*/
@@ -225,7 +211,8 @@ public function count()
225211
}
226212

227213
/**
228-
* find a key in an array
214+
* Find a key in an array.
215+
*
229216
* example Data::findInArray(array(), key1, key2, key3, ..., default_return)
230217
*
231218
* @return array|bool|mixed
@@ -234,8 +221,35 @@ public static function findInArray()
234221
{
235222
$args = func_get_args();
236223
$data = array_shift($args);
237-
$default = false;
238224

225+
return self::searchArray(
226+
$args,
227+
$data
228+
);
229+
}
230+
231+
/**
232+
* Normalize a key.
233+
*
234+
* @param string $key
235+
* @return string
236+
*/
237+
public static function normalize($key)
238+
{
239+
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
240+
}
241+
242+
/**
243+
* Search an array for keys (args) and provide a default value if
244+
* last arg is some kind of empty or not numeric.
245+
*
246+
* @param array $args
247+
* @param array $data
248+
* @param bool $default
249+
* @return array|mixed
250+
*/
251+
private static function searchArray(array $args, array $data, $default = false)
252+
{
239253
// check for default return value
240254
if (1 < count($args)) {
241255
$lastElm = array_pop($args);
@@ -251,21 +265,11 @@ public static function findInArray()
251265
if (array_key_exists($key, $data)) {
252266
$data = $data[$key];
253267
} else {
254-
return $default;
268+
$data = $default;
269+
break;
255270
}
256271
}
257272

258273
return $data;
259274
}
260-
261-
/**
262-
* normalize a key
263-
*
264-
* @param string $key
265-
* @return string
266-
*/
267-
public static function normalize($key)
268-
{
269-
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
270-
}
271275
}

0 commit comments

Comments
 (0)