|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\Filesystem; |
|
13
|
|
|
|
|
14
|
|
|
class DeltaAttributeDecorator |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Custom attributes. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $custom = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Decorate attributes. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $node |
|
27
|
|
|
* @param array $attributes |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function decorate($node, array $attributes = []): array |
|
32
|
|
|
{ |
|
33
|
|
|
if (0 === count($attributes)) { |
|
34
|
|
|
return $this->translateAttributes($node, $this->getAttributes($node)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this->translateAttributes($node, array_intersect_key($this->getAttributes($node), array_flip($attributes))); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Add decorator. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $attribute |
|
44
|
|
|
* @param Closure $decorator |
|
45
|
|
|
* |
|
46
|
|
|
* @return AttributeDecorator |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addDecorator(string $attribute, Closure $decorator): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->custom[$attribute] = $decorator; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get Attributes. |
|
57
|
|
|
* |
|
58
|
|
|
* @param NodeInterface |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getAttributes(array $node): array |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
|
|
'id' => (string) $node['id'], |
|
66
|
|
|
'deleted' => $node['deleted'], |
|
67
|
|
|
'changed' => $node['changed']->toDateTime()->format('c'), |
|
68
|
|
|
'path' => $node['path'], |
|
69
|
|
|
'directory' => $node['directory'], |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Execute closures. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $node |
|
77
|
|
|
* @param array $attributes |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
protected function translateAttributes(array $node, array $attributes): array |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($attributes as $key => &$value) { |
|
84
|
|
|
if ($value instanceof Closure) { |
|
|
|
|
|
|
85
|
|
|
$result = $value->call($this, $node); |
|
86
|
|
|
|
|
87
|
|
|
if (null === $result) { |
|
88
|
|
|
unset($attributes[$key]); |
|
89
|
|
|
} else { |
|
90
|
|
|
$value = $result; |
|
91
|
|
|
} |
|
92
|
|
|
} elseif ($value === null) { |
|
93
|
|
|
unset($attributes[$key]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $attributes; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.