Passed
Pull Request — main (#84)
by Andrey
196:26 queued 181:26
created

Comparator::splitNotSortable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
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;
0 ignored issues
show
Bug introduced by
The trait Helldar\LaravelLangPublisher\Concerns\Logger requires the property $output which is not provided by Helldar\LaravelLangPubli...\Comparators\Comparator.
Loading history...
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->prepareExcludes();
83
            $this->ranExcludes();
84
            $this->extractExcludes();
85
        }
86
    }
87
88
    protected function findNotReplace(): void
89
    {
90
        if ($this->full) {
91
            return;
92
        }
93
94
        $this->not_replace = $this->reserved($this->target, $this->key);
95
    }
96
97
    protected function ranExcludes(): void
98
    {
99
        $this->log('Retrieving values from arrays...');
100
101
        foreach ($this->excludes as $key => &$value) {
102
            $value = $this->getFallbackValue($this->source, $this->target, $key);
103
        }
104
    }
105
106
    protected function extractExcludes(): void
107
    {
108
        $this->log('Extracting extended data from source and target arrays ...');
109
110
        $keys = array_keys($this->excludes);
111
112
        $this->source = Arr::except($this->source, $keys);
113
        $this->target = Arr::except($this->target, $keys);
114
    }
115
116
    protected function getFallbackValue(array $source, array $target, string $key): array
117
    {
118
        $this->log('Retrieving values from arrays by the "' . $key . '" key...');
119
120
        return Arr::get($target, $key) ?: Arr::get($source, $key, []);
121
    }
122
123
    protected function sort(array $array): array
124
    {
125
        $this->log('Sorting array...');
126
127
        return Arr::ksort($array);
128
    }
129
130
    protected function prepareExcludes(): void
131
    {
132
        $this->log('Exchanges all keys with their associated values in an array...');
133
134
        $this->excludes = array_flip($this->excludes);
135
    }
136
}
137