1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Services\Comparators; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Logger; |
6
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Reservation; |
7
|
|
|
use Helldar\LaravelLangPublisher\Contracts\Comparator as Contract; |
8
|
|
|
use Helldar\Support\Concerns\Makeable; |
9
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
10
|
|
|
|
11
|
|
|
abstract class Comparator implements Contract |
12
|
|
|
{ |
13
|
|
|
use Logger; |
|
|
|
|
14
|
|
|
use Makeable; |
15
|
|
|
use Reservation; |
16
|
|
|
|
17
|
|
|
protected $key; |
18
|
|
|
|
19
|
|
|
protected $full = false; |
20
|
|
|
|
21
|
|
|
protected $source; |
22
|
|
|
|
23
|
|
|
protected $target; |
24
|
|
|
|
25
|
|
|
protected $excludes = []; |
26
|
|
|
|
27
|
|
|
protected $not_replace = []; |
28
|
|
|
|
29
|
11 |
|
public function handle(): array |
30
|
|
|
{ |
31
|
11 |
|
$this->log('Merging source and target arrays...'); |
32
|
|
|
|
33
|
11 |
|
return $this->full ? $this->source : array_merge($this->target, $this->source, $this->not_replace); |
34
|
|
|
} |
35
|
|
|
|
36
|
11 |
|
public function key(string $key): Contract |
37
|
|
|
{ |
38
|
11 |
|
$this->key = $key; |
39
|
|
|
|
40
|
11 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
11 |
|
public function full(bool $full): Contract |
44
|
|
|
{ |
45
|
11 |
|
$this->full = $full; |
46
|
|
|
|
47
|
11 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
11 |
|
public function source(array $array): Contract |
51
|
|
|
{ |
52
|
11 |
|
$this->source = $array; |
53
|
|
|
|
54
|
11 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
11 |
|
public function target(array $array): Contract |
58
|
|
|
{ |
59
|
11 |
|
$this->target = $array; |
60
|
|
|
|
61
|
11 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
11 |
|
public function toArray(): array |
65
|
|
|
{ |
66
|
11 |
|
$this->findNotReplace(); |
67
|
11 |
|
$this->splitNotSortable(); |
68
|
|
|
|
69
|
11 |
|
$array = $this->sort($this->handle()); |
70
|
11 |
|
$excludes = $this->sort($this->excludes); |
71
|
|
|
|
72
|
11 |
|
$this->log('Merging the main array with excluded data...'); |
73
|
|
|
|
74
|
11 |
|
return array_merge($array, $excludes); |
75
|
|
|
} |
76
|
|
|
|
77
|
11 |
|
protected function splitNotSortable(): void |
78
|
|
|
{ |
79
|
11 |
|
$this->log('Splitting main arrays into excludes...'); |
80
|
|
|
|
81
|
11 |
|
if (! empty($this->excludes)) { |
82
|
11 |
|
$this->prepareExcludes(); |
83
|
11 |
|
$this->ranExcludes(); |
84
|
11 |
|
$this->extractExcludes(); |
85
|
|
|
} |
86
|
11 |
|
} |
87
|
|
|
|
88
|
11 |
|
protected function findNotReplace(): void |
89
|
|
|
{ |
90
|
11 |
|
if ($this->full) { |
91
|
1 |
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
$this->not_replace = $this->reserved($this->target, $this->key); |
95
|
10 |
|
} |
96
|
|
|
|
97
|
11 |
|
protected function ranExcludes(): void |
98
|
|
|
{ |
99
|
11 |
|
$this->log('Retrieving values from arrays...'); |
100
|
|
|
|
101
|
11 |
|
foreach ($this->excludes as $key => &$value) { |
102
|
11 |
|
$value = $this->getFallbackValue($this->source, $this->target, $key); |
103
|
|
|
} |
104
|
11 |
|
} |
105
|
|
|
|
106
|
11 |
|
protected function extractExcludes(): void |
107
|
|
|
{ |
108
|
11 |
|
$this->log('Extracting extended data from source and target arrays ...'); |
109
|
|
|
|
110
|
11 |
|
$keys = array_keys($this->excludes); |
111
|
|
|
|
112
|
11 |
|
$this->source = Arr::except($this->source, $keys); |
113
|
11 |
|
$this->target = Arr::except($this->target, $keys); |
114
|
11 |
|
} |
115
|
|
|
|
116
|
11 |
|
protected function getFallbackValue(array $source, array $target, string $key): array |
117
|
|
|
{ |
118
|
11 |
|
$this->log('Retrieving values from arrays by the "' . $key . '" key...'); |
119
|
|
|
|
120
|
11 |
|
return Arr::get($target, $key) ?: Arr::get($source, $key, []); |
121
|
|
|
} |
122
|
|
|
|
123
|
11 |
|
protected function sort(array $array): array |
124
|
|
|
{ |
125
|
11 |
|
$this->log('Sorting array...'); |
126
|
|
|
|
127
|
11 |
|
return Arr::ksort($array); |
128
|
|
|
} |
129
|
|
|
|
130
|
11 |
|
protected function prepareExcludes(): void |
131
|
|
|
{ |
132
|
11 |
|
$this->log('Exchanges all keys with their associated values in an array...'); |
133
|
|
|
|
134
|
11 |
|
$this->excludes = array_flip($this->excludes); |
135
|
11 |
|
} |
136
|
|
|
} |
137
|
|
|
|