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

ArrayProcessor::unique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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