1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\DotKey\Internal; |
6
|
|
|
|
7
|
|
|
use Jasny\DotKey\ResolveException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Static class with get and put methods. |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
final class Put |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Set a value, creating a structure if needed. |
17
|
|
|
* |
18
|
|
|
* @param object|array<string,mixed> $subject |
19
|
|
|
* @param string $path |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @param string $delimiter |
22
|
|
|
* @param bool $assoc Create new structure as array. Omit to base upon subject type. |
23
|
|
|
* @param bool $copy |
24
|
|
|
* @throws ResolveException |
25
|
|
|
*/ |
26
|
36 |
|
public static function apply(&$subject, string $path, $value, string $delimiter, bool $assoc, bool $copy): void |
27
|
|
|
{ |
28
|
36 |
|
$current =& $subject; |
29
|
36 |
|
$index = Helpers::splitPath($path, $delimiter); |
30
|
|
|
|
31
|
35 |
|
while (count($index) > 1) { |
32
|
34 |
|
$key = \array_shift($index); |
33
|
|
|
|
34
|
|
|
try { |
35
|
34 |
|
$current =& Helpers::descend($current, $key, $exists, false, $copy); |
36
|
1 |
|
} catch (\Error $error) { |
37
|
1 |
|
$msg = "Unable to put '$path': error at '%s'"; |
38
|
1 |
|
throw ResolveException::create($msg, $path, $delimiter, $index, $error); |
39
|
|
|
} |
40
|
|
|
|
41
|
33 |
|
if (!$exists) { |
42
|
17 |
|
array_unshift($index, $key); |
43
|
17 |
|
break; |
44
|
|
|
} |
45
|
|
|
|
46
|
33 |
|
if (!\is_array($current) && !\is_object($current)) { |
47
|
5 |
|
break; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
34 |
|
if ($copy && is_object($current)) { |
53
|
4 |
|
$current = clone $current; |
54
|
|
|
} |
55
|
|
|
|
56
|
34 |
|
self::setValueCreate($current, $index, $value, $assoc); |
57
|
2 |
|
} catch (\Error $error) { |
58
|
2 |
|
$msg = "Unable to put '$path': error at '%s'"; |
59
|
2 |
|
throw ResolveException::create($msg, $path, $delimiter, array_slice($index, 0, -1), $error); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create property and set the value. |
65
|
|
|
* |
66
|
|
|
* @param mixed $subject |
67
|
|
|
* @param string[] $index Part or the path that doesn't exist |
68
|
|
|
* @param mixed $value |
69
|
|
|
* @param bool $assoc |
70
|
|
|
*/ |
71
|
34 |
|
protected static function setValueCreate(&$subject, array $index, $value, bool $assoc): void |
72
|
|
|
{ |
73
|
34 |
|
if (is_array($subject) || $subject instanceof \ArrayAccess) { |
74
|
17 |
|
$key = \array_shift($index); |
75
|
17 |
|
$subject[$key] = null; |
76
|
17 |
|
$subject =& $subject[$key]; |
77
|
18 |
|
} elseif (is_object($subject)) { |
78
|
13 |
|
$key = \array_shift($index); |
79
|
13 |
|
$subject->{$key} = null; |
80
|
11 |
|
$subject =& $subject->{$key}; |
81
|
|
|
} |
82
|
|
|
|
83
|
32 |
|
while ($index !== []) { |
84
|
22 |
|
$key = \array_pop($index); |
85
|
22 |
|
$value = $assoc ? [$key => $value] : (object)[$key => $value]; |
86
|
|
|
} |
87
|
|
|
|
88
|
32 |
|
$subject = $value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|