Passed
Push — main ( 545ade...3cfdef )
by Andrey
79:39 queued 77:29
created

ArrayProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 72
ccs 30
cts 30
cp 1
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A of() 0 5 1
A merge() 0 9 2
A unique() 0 5 1
A keysAsString() 0 5 1
A push() 0 5 1
A stringingKeys() 0 8 2
A toArray() 0 3 1
A sort() 0 5 1
A values() 0 5 1
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\Support\Facades\Helpers\Arr as ArrHelper;
6
use Illuminate\Contracts\Support\Arrayable;
7
8
class ArrayProcessor implements Arrayable
9
{
10
    protected $items = [];
11
12
    protected $keys_as_string = false;
13
14 10
    public function keysAsString(): self
15
    {
16 10
        $this->keys_as_string = true;
17
18 10
        return $this;
19
    }
20
21 18
    public function of(array $items): self
22
    {
23 18
        $this->items = $this->stringingKeys($items);
24
25 18
        return $this;
26
    }
27
28 2
    public function push($value): self
29
    {
30 2
        array_push($this->items, $value);
31
32 2
        return $this;
33
    }
34
35 11
    public function merge(array $array): self
36
    {
37 11
        $array = $this->stringingKeys($array);
38
39 11
        foreach ($array as $key => $value) {
40 11
            $this->items[$key] = $value;
41
        }
42
43 11
        return $this;
44
    }
45
46 18
    public function unique(): self
47
    {
48 18
        $this->items = array_unique($this->items);
49
50 18
        return $this;
51
    }
52
53 18
    public function values(): self
54
    {
55 18
        $this->items = array_values($this->items);
56
57 18
        return $this;
58
    }
59
60 2
    public function sort(): self
61
    {
62 2
        $this->items = ArrHelper::sort($this->items);
63
64 2
        return $this;
65
    }
66
67 18
    public function toArray(): array
68
    {
69 18
        return $this->items;
70
    }
71
72 18
    protected function stringingKeys(array $array): array
73
    {
74 18
        if (! $this->keys_as_string) {
75 18
            return $array;
76
        }
77
78 10
        return ArrHelper::renameKeys($array, static function ($key) {
79 10
            return (string) $key;
80 10
        });
81
    }
82
}
83