|
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 !== null && $offset !== 0) { |
|
47
|
|
|
$this->db = $this->db->offset($offset); |
|
48
|
|
|
} |
|
49
|
2 |
|
if ($limit !== null) { |
|
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
|
|
|
|