ArrayTable   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 30
c 1
b 0
f 1
dl 0
loc 85
ccs 35
cts 36
cp 0.9722
rs 10
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A filterKeys() 0 8 3
A columns() 0 9 1
A filter() 0 6 1
A paginate() 0 5 1
A __construct() 0 4 1
A search() 0 6 1
A sort() 0 7 2
A run() 0 3 1
A range() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\tabletools;
6
7
use Exception;
8
9
use function array_filter;
10
use function array_map;
11
use function array_slice;
12
use function in_array;
13
use function strpos;
14
use function usort;
15
16
class ArrayTable implements TableInterface
17
{
18
    private array $data = [];
19
    private array $toReturn = [];
20
21
    /**
22
     * @param array $data Should be a 2x2 associative array.
23
     */
24 6
    public function __construct(array $data)
25
    {
26 6
        $this->data = $data;
27 6
        $this->toReturn = $data;
28 6
    }
29
30
    /**
31
     * @param string $order
32
     * @return $this
33
     * @throws Exception
34
     */
35 1
    public function sort(string $key, $order = 'ASC'): self
36
    {
37
        usort($this->toReturn, function ($a, $b) use ($order, $key) {
38 1
            return ($order === 'ASC' ? 1 : -1) * ($a[$key] <=> $b[$key]);
39 1
        });
40
41 1
        return $this;
42
    }
43
44 1
    public function columns($keys): self
45
    {
46
        //array_walk($keys, fn($key) => $this->checkKey($key));
47
48
        $this->toReturn = array_map(function (&$item) use ($keys) {
49 1
            return self::filterKeys($keys, $item);
50 1
        }, $this->toReturn);
51
52 1
        return $this;
53
    }
54
55 1
    private static function filterKeys($keys, $data): array
56
    {
57 1
        foreach ($data as $key => $value) {
58 1
            if (!in_array($key, $keys)) {
59 1
                unset($data[$key]);
60
            }
61
        }
62 1
        return $data;
63
    }
64
65 1
    public function filter(string $key, $value): self
66
    {
67
        $this->toReturn = array_filter($this->data, function ($item) use ($value, $key) {
68 1
            return $item[$key] === $value;
69 1
        });
70 1
        return $this;
71
    }
72
73 1
    public function search(string $key, $value): self
74
    {
75
        $this->toReturn = array_filter($this->data, function ($item) use ($value, $key) {
76 1
            return strpos($item[$key], $value) !== false;
77 1
        });
78 1
        return $this;
79
    }
80
81 1
    public function paginate(int $page = 0, int $pageSize = 10): self
82
    {
83 1
        $offset = $page * $pageSize;
84 1
        $this->range($offset, $pageSize);
85 1
        return $this;
86
    }
87
88 2
    public function range(int $offset, ?int $limit = null): self
89
    {
90 2
        if ($limit === null) {
91
            $this->toReturn = array_slice($this->toReturn, $offset);
92
        } else {
93 2
            $this->toReturn = array_slice($this->toReturn, $offset, $limit);
94
        }
95 2
        return $this;
96
    }
97
98 6
    public function run(): array
99
    {
100 6
        return $this->toReturn;
101
    }
102
}
103