Completed
Push — master ( 0ba3a1...e9d1e1 )
by Midori
01:23
created

DatabaseTable::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\tabletools;
6
7
use midorikocak\nanodb\Database;
8
9
class DatabaseTable implements TableInterface
10
{
11
    private Database $db;
12
    private string $name;
13
14 6
    public function __construct(string $name, Database $db)
15
    {
16 6
        $this->db = $db->select($name);
17 6
        $this->name = $name;
18 6
    }
19
20 1
    public function sort(string $key, $order = 'ASC'): self
21
    {
22 1
        $this->db = $this->db->orderBy($key, $order);
23 1
        return $this;
24
    }
25
26 1
    public function columns($keys): self
27
    {
28 1
        $this->db = $this->db->select($this->name, $keys);
29 1
        return $this;
30
    }
31
32 1
    public function filter(string $key, $value): self
33
    {
34 1
        $this->db->where($key, $value);
35 1
        return $this;
36
    }
37
38 1
    public function search(string $key, $value): self
39
    {
40
        // $this->db = $this->db->where($key, $value, 'LIKE');
41 1
        return $this;
42
    }
43
44 2
    public function range(int $offset, ?int $limit = null): self
45
    {
46 2
        if ($offset !== 0) {
47
            $this->db = $this->db->offset($offset);
48
        }
49 2
        if ($limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
50 2
            $this->db = $this->db->limit($limit);
51
        }
52
53 2
        return $this;
54
    }
55
56 1
    public function paginate(int $page = 0, int $pageSize = 10): self
57
    {
58 1
        $offset = $page * $pageSize;
59 1
        $limit = $pageSize;
60
61 1
        return $this->range($offset, $limit);
62
    }
63
64 6
    public function run(): array
65
    {
66 6
        return $this->db->fetchAll();
67
    }
68
}
69