Passed
Push — 9.x ( b642a2...28b741 )
by Andrey
15:11
created

Comparator::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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->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