Skip to content

Commit 0852dd4

Browse files
committed
refactor: upgrade to the new version
Closes #24
1 parent 9237c3d commit 0852dd4

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/Json.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ public function exists(): bool
4747
/**
4848
* Get the contents of the JSON file.
4949
*
50-
* @param bool $associative If the returned object will be converted to an associative array.
50+
* @param bool $asObject If true and the value is an array, it is returned as an object.
5151
*
5252
* @throws GetFileException if the file could not be read.
5353
* @throws JsonErrorException if the file contains invalid JSON.
5454
*
5555
* @return mixed the contents of the JSON file.
5656
*/
57-
public function get(bool $associative = true): mixed
57+
public function get(bool $asObject = false): mixed
5858
{
5959
$json = @file_get_contents($this->filepath);
6060

6161
$json === false && throw new GetFileException($this->filepath);
6262

63-
$array = json_decode($json, $associative);
63+
$array = json_decode($json, !$asObject);
6464

6565
json_last_error() && throw new JsonErrorException();
6666

@@ -269,7 +269,7 @@ public function unshift(mixed $content, string $dot = null): mixed
269269
*/
270270
protected function modifyArrayByDot(string $type, array &$array, string $dot, mixed $content = []): mixed
271271
{
272-
$keys = explode('.', (string) $dot);
272+
$keys = explode('.', $dot);
273273

274274
if ($type == 'unset') {
275275
$last = array_pop($keys);
@@ -279,26 +279,30 @@ protected function modifyArrayByDot(string $type, array &$array, string $dot, mi
279279
$array = &$array[$key];
280280
}
281281

282-
if ($type == 'merge') {
283-
$this->failIfNotArray('merge', $array, $dot);
284-
$array = array_merge($array, (array) $content);
285-
} elseif ($type == 'pop') {
286-
$this->failIfNotArray('pop', $array, $dot);
287-
return array_pop($array);
288-
} elseif ($type == 'push') {
289-
$this->failIfNotArray('push', $array, $dot);
290-
array_push($array, is_object($content) ? (array) $content : $content);
291-
} elseif ($type == 'set') {
292-
$array = $content;
293-
} elseif ($type == 'shift') {
294-
$this->failIfNotArray('shift', $array, $dot);
295-
return array_shift($array);
296-
} elseif ($type == 'unset') {
297-
unset($array[$last]);
298-
} elseif ($type == 'unshift') {
299-
$this->failIfNotArray('unshift', $array, $dot);
300-
array_unshift($array, is_object($content) ? (array) $content : $content);
282+
if (!str_contains($type, 'set')) {
283+
$this->failIfNotArray($type, $array, $dot);
301284
}
285+
286+
switch ($type) {
287+
case 'merge':
288+
$array = array_merge($array, (array) $content);
289+
break;
290+
case 'pop':
291+
return array_pop($array);
292+
case 'push':
293+
return array_push($array, is_object($content) ? (array) $content : $content);
294+
case 'set':
295+
$array = $content;
296+
break;
297+
case 'shift':
298+
return array_shift($array);
299+
case 'unset':
300+
unset($array[$last]);
301+
break;
302+
case 'unshift':
303+
return array_unshift($array, is_object($content) ? (array) $content : $content);
304+
}
305+
302306
return null;
303307
}
304308

0 commit comments

Comments
 (0)