Passed
Pull Request — main (#93)
by Andrey
29:42 queued 14:37
created

Comparator::getExcludedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 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->ranExcludes();
83 11
            $this->extractExcludes();
84 11
        }
85
    }
86 11
87
    protected function findNotReplace(): void
88 11
    {
89
        if ($this->full) {
90 11
            return;
91 1
        }
92
93
        $this->not_replace = $this->reserved($this->target, $this->key);
94 10
    }
95 10
96
    protected function ranExcludes(): void
97 11
    {
98
        $this->log('Retrieving values from arrays...');
99 11
100
        $this->excludes = $this->getExcludedValues($this->source, $this->target, $this->excludes);
101 11
    }
102 11
103
    protected function extractExcludes(): void
104 11
    {
105
        $this->log('Extracting extended data from source and target arrays ...');
106 11
107
        $keys = array_keys($this->excludes);
108 11
109
        $this->source = Arr::except($this->source, $keys);
110 11
        $this->target = Arr::except($this->target, $keys);
111
    }
112 11
113 11
    protected function getExcludedValues(array $source, array $target, array $keys): array
114 11
    {
115
        $this->log('Retrieving values from arrays by the "' . implode('", "', $keys) . '" key...');
116 11
117
        $excluded_source = Arr::only($source, $keys);
118 11
        $excluded_target = Arr::only($target, $keys);
119
120 11
        return array_merge($excluded_target, $excluded_source);
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