|
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
|
|
|
public function handle(): array |
|
30
|
|
|
{ |
|
31
|
|
|
$this->log('Merging source and target arrays...'); |
|
32
|
|
|
|
|
33
|
|
|
return $this->full ? $this->source : array_merge($this->target, $this->source, $this->not_replace); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function key(string $key): Contract |
|
37
|
|
|
{ |
|
38
|
|
|
$this->key = $key; |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function full(bool $full): Contract |
|
44
|
|
|
{ |
|
45
|
|
|
$this->full = $full; |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function source(array $array): Contract |
|
51
|
|
|
{ |
|
52
|
|
|
$this->source = $array; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function target(array $array): Contract |
|
58
|
|
|
{ |
|
59
|
|
|
$this->target = $array; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function toArray(): array |
|
65
|
|
|
{ |
|
66
|
|
|
$this->findNotReplace(); |
|
67
|
|
|
$this->splitNotSortable(); |
|
68
|
|
|
|
|
69
|
|
|
$array = $this->sort($this->handle()); |
|
70
|
|
|
$excludes = $this->sort($this->excludes); |
|
71
|
|
|
|
|
72
|
|
|
$this->log('Merging the main array with excluded data...'); |
|
73
|
|
|
|
|
74
|
|
|
return array_merge($array, $excludes); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function splitNotSortable(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->log('Splitting main arrays into excludes...'); |
|
80
|
|
|
|
|
81
|
|
|
if (! empty($this->excludes)) { |
|
82
|
|
|
$this->ranExcludes(); |
|
83
|
|
|
$this->extractExcludes(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function findNotReplace(): void |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->full) { |
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->not_replace = $this->reserved($this->target, $this->key); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function ranExcludes(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->log('Retrieving values from arrays...'); |
|
99
|
|
|
|
|
100
|
|
|
$this->excludes = $this->getExcludedValues($this->source, $this->target, $this->excludes); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function extractExcludes(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->log('Extracting extended data from source and target arrays ...'); |
|
106
|
|
|
|
|
107
|
|
|
$keys = array_keys($this->excludes); |
|
108
|
|
|
|
|
109
|
|
|
$this->source = Arr::except($this->source, $keys); |
|
110
|
|
|
$this->target = Arr::except($this->target, $keys); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function getExcludedValues(array $source, array $target, array $keys): array |
|
114
|
|
|
{ |
|
115
|
|
|
$this->log('Retrieving values from arrays by the "' . implode('", "', $keys) . '" key...'); |
|
116
|
|
|
|
|
117
|
|
|
$excluded_source = Arr::only($source, $keys); |
|
118
|
|
|
$excluded_target = Arr::only($target, $keys); |
|
119
|
|
|
|
|
120
|
|
|
return array_merge($excluded_target, $excluded_source); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function sort(array $array): array |
|
124
|
|
|
{ |
|
125
|
|
|
$this->log('Sorting array...'); |
|
126
|
|
|
|
|
127
|
|
|
return Arr::ksort($array); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|