Passed
Push — 9.x ( 571ce0...ec3d6f )
by Andrey
11:20
created

ArrayProcessor::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
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
    public function keysAsString(): self
15
    {
16
        $this->keys_as_string = true;
17
18
        return $this;
19
    }
20
21
    public function of(array $items): self
22
    {
23
        $this->items = $this->stringingKeys($items);
24
25
        return $this;
26
    }
27
28
    public function push($value): self
29
    {
30
        array_push($this->items, $value);
31
32
        return $this;
33
    }
34
35
    public function merge(array $array): self
36
    {
37
        $array = $this->stringingKeys($array);
38
39
        foreach ($array as $key => $value) {
40
            $this->items[$key] = $value;
41
        }
42
43
        return $this;
44
    }
45
46
    public function unique(): self
47
    {
48
        $this->items = array_unique($this->items);
49
50
        return $this;
51
    }
52
53
    public function values(): self
54
    {
55
        $this->items = array_values($this->items);
56
57
        return $this;
58
    }
59
60
    public function sort(): self
61
    {
62
        $this->items = ArrHelper::sort($this->items);
63
64
        return $this;
65
    }
66
67
    public function toArray(): array
68
    {
69
        return $this->items;
70
    }
71
72
    protected function stringingKeys(array $array): array
73
    {
74
        if (! $this->keys_as_string) {
75
            return $array;
76
        }
77
78
        return ArrHelper::renameKeys($array, static function ($key) {
79
            return (string) $key;
80
        });
81
    }
82
}
83