/
StructureCopy.php
81 lines (64 loc) · 1.88 KB
/
StructureCopy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Becklyn\YamlParameters;
use Composer\IO\IOInterface;
use Symfony\Component\Yaml\Inline;
class StructureCopy
{
/**
* @var IOInterface
*/
private $io;
/**
* @param IOInterface $io
*/
public function __construct (IOInterface $io)
{
$this->io = $io;
}
/**
* @param array $source
* @param array $target
* @return array
*/
public function copy (array $source, array $target) : array
{
$merged = [];
foreach ($source as $key => $value)
{
if (\is_array($value))
{
$currentValue = isset($target[$key]) && \is_array($target[$key])
? $target[$key]
: [];
$merged[$key] = $this->copy($value, $currentValue);
continue;
}
if (\is_scalar($value) || null === $value)
{
if (\array_key_exists($key, $target))
{
$merged[$key] = $target[$key];
}
else
{
$default = null === $value
? "~"
: Inline::dump($value);
$answer = $this->io->ask(
sprintf(
'<question>%s</question> (<comment>%s</comment>): ',
$key,
$default
),
$default
);
// parse, so that the values have the proper format like `~` being really `null` etc
$merged[$key] = Inline::parse($answer);
}
continue;
}
throw new \RuntimeException("Can only merge parameters of type array and scalar.");
}
return $merged;
}
}