Passed
Push — master ( 807ca0...642002 )
by Midori
01:31
created

ResultSet   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
dl 0
loc 72
ccs 0
cts 46
cp 0
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 11 1
A getResultSet() 0 39 2
A getResultArray() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\nanodb;
6
7
use midorikocak\querymaker\QueryInterface;
8
9
use function array_column;
10
use function array_shift;
11
use function ceil;
12
use function preg_match;
13
use function reset;
14
15
class ResultSet
16
{
17
    private int $total;
18
    private array $items;
19
    private int $pageSize;
20
    private int $page;
21
    private ?int $offset;
22
    private ?int $limit;
23
    private ?string $orderBy;
24
    private ?string $order;
25
    private string $tableName;
26
27
    /**
28
     * @return static
29
     */
30
    public static function getResultSet(
31
        Database $db,
32
        QueryInterface $query
33
    ): self {
34
        $db->query($query);
35
        $db->execute();
36
        $items = $db->fetchAll();
37
38
        $counterQuery = $query->count();
39
        $db->query($counterQuery);
40
        $db->execute();
41
42
        $counted = array_column($db->fetchAll(), 'COUNT(*)');
43
44
        $resultSet = new self();
45
        $resultSet->total = (int) reset($counted);
46
        $resultSet->items = $items;
47
48
        $queryString = $query->getQuery();
49
        preg_match('/FROM (\S*)( WHERE)?/', $queryString, $tableMatch);
50
51
        preg_match('/OFFSET (\d+)/', $queryString, $offsetMatch);
52
        preg_match('/LIMIT (\d+)/', $queryString, $limitMatch);
53
        preg_match('/ORDER BY (\S+)( ASC| DESC)/', $queryString, $orderMatch);
54
55
        array_shift($offsetMatch);
56
        array_shift($limitMatch);
57
        array_shift($orderMatch);
58
59
        $resultSet->limit = (int) (reset($limitMatch) ?? 0);
60
        $resultSet->offset = (int) (reset($offsetMatch) ?? 0);
61
        $resultSet->orderBy = reset($orderMatch) !== false ? reset($orderMatch) : null;
62
        $resultSet->order = $orderMatch[1] ?? null;
63
        $resultSet->tableName = $tableMatch[1] ?? '';
64
65
        $resultSet->pageSize = $resultSet->limit ?? $resultSet->total;
66
67
        $resultSet->page = (int) ceil($resultSet->offset / $resultSet->pageSize);
68
        return $resultSet;
69
    }
70
71
    public static function getResultArray(Database $db, QueryInterface $query): array
72
    {
73
        return self::getResultSet($db, $query)->toArray();
74
    }
75
76
    public function toArray(): array
77
    {
78
        return [
79
            'total' => $this->total,
80
            'items' => $this->items,
81
            'pageSize' => $this->pageSize,
82
            'page' => $this->page,
83
            'offset' => $this->offset,
84
            'limit' => $this->limit,
85
            'orderBy' => $this->orderBy,
86
            'tableName' => $this->tableName,
87
        ];
88
    }
89
}
90