1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uvinum\Joiner\Manipulator; |
4
|
|
|
|
5
|
|
|
use League\Pipeline\Pipeline; |
6
|
|
|
|
7
|
|
|
final class ArrayManipulator implements Manipulator |
8
|
|
|
{ |
9
|
|
|
/** @var Pipeline */ |
10
|
|
|
private $pipeline; |
11
|
|
|
|
12
|
15 |
|
public function __construct() |
13
|
|
|
{ |
14
|
15 |
|
$this->pipeline = new Pipeline(); |
15
|
15 |
|
} |
16
|
|
|
|
17
|
12 |
View Code Duplication |
public function append($key, $args) |
|
|
|
|
18
|
|
|
{ |
19
|
12 |
|
$this->pipeline = $this->pipeline->pipe( |
20
|
|
|
function ($serializedBase) use ($key, $args) { |
21
|
12 |
|
$keysDepth = \explode('>', $key); |
22
|
12 |
|
$this->setNestedValue($serializedBase, $keysDepth, $args); |
23
|
|
|
|
24
|
12 |
|
return $serializedBase; |
25
|
8 |
|
} |
26
|
4 |
|
); |
27
|
12 |
|
} |
28
|
|
|
|
29
|
13 |
|
private function setNestedValue(&$serializedBase, array $keysDepth, $value) |
30
|
|
|
{ |
31
|
12 |
|
$currentKey = \current($keysDepth); |
32
|
12 |
|
$nextKey = \next($keysDepth); |
33
|
12 |
|
if (false === $nextKey) { |
34
|
12 |
|
$serializedBase[$currentKey] = $value; |
35
|
|
|
|
36
|
13 |
|
return $serializedBase; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
return $this->setNestedValue($serializedBase[$currentKey], $keysDepth, $value); |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
View Code Duplication |
public function filter($key) |
|
|
|
|
43
|
|
|
{ |
44
|
3 |
|
$this->pipeline = $this->pipeline->pipe( |
45
|
3 |
|
function ($serializedBase) use ($key) { |
46
|
3 |
|
$keysDepth = \explode('>', $key); |
47
|
3 |
|
$this->unsetNestedValue($serializedBase, $keysDepth); |
48
|
|
|
|
49
|
3 |
|
return $serializedBase; |
50
|
2 |
|
} |
51
|
1 |
|
); |
52
|
3 |
|
} |
53
|
|
|
|
54
|
3 |
|
private function unsetNestedValue(&$serializedBase, array $keysDepth) |
55
|
|
|
{ |
56
|
3 |
|
$currentKey = \current($keysDepth); |
57
|
3 |
|
$nextKey = \next($keysDepth); |
58
|
3 |
|
if (!$nextKey) { |
59
|
3 |
|
unset($serializedBase[$currentKey]); |
60
|
|
|
|
61
|
3 |
|
return $serializedBase; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $this->unsetNestedValue($serializedBase[$currentKey], $keysDepth); |
65
|
|
|
} |
66
|
|
|
|
67
|
15 |
|
public function process($serializedBase) |
68
|
|
|
{ |
69
|
15 |
|
$result = $this->pipeline->process($serializedBase); |
70
|
15 |
|
$this->pipeline = new Pipeline(); |
71
|
|
|
|
72
|
15 |
|
return $result; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.