1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Sleekdb\Statements; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\PaginatorInterface; |
18
|
|
|
use Quantum\Libraries\Database\Sleekdb\Paginator; |
19
|
|
|
use Quantum\Libraries\Database\DbalInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait Result |
23
|
|
|
* @package Quantum\Libraries\Database\Sleekdb\Statements |
24
|
|
|
*/ |
25
|
|
|
trait Result |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @return array |
30
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
31
|
|
|
* @throws \Quantum\Exceptions\ModelException |
32
|
|
|
* @throws \SleekDB\Exceptions\IOException |
33
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
34
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
35
|
|
|
*/ |
36
|
|
|
public function get(): array |
37
|
|
|
{ |
38
|
|
|
$result = array_map(function ($element) { |
39
|
|
|
$item = clone $this; |
40
|
|
|
$item->data = $element; |
|
|
|
|
41
|
|
|
$item->modifiedFields = $element; |
|
|
|
|
42
|
|
|
$item->isNew = false; |
|
|
|
|
43
|
|
|
return $item; |
44
|
|
|
}, $this->getBuilder()->getQuery()->fetch()); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param int $perPage |
51
|
|
|
* @param int $currentPage |
52
|
|
|
* @return PaginatorInterface |
53
|
|
|
*/ |
54
|
|
|
public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
55
|
|
|
{ |
56
|
|
|
return new Paginator($this, $perPage, $currentPage); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
* @return DbalInterface |
62
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
63
|
|
|
* @throws \SleekDB\Exceptions\IOException |
64
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
65
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
66
|
|
|
*/ |
67
|
|
|
public function findOne(int $id): DbalInterface |
68
|
|
|
{ |
69
|
|
|
$result = $this->getOrmModel()->findById($id); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->data = $result; |
|
|
|
|
72
|
|
|
$this->modifiedFields = $result; |
|
|
|
|
73
|
|
|
$this->isNew = false; |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $this; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
81
|
|
|
* @throws \SleekDB\Exceptions\IOException |
82
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
83
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
84
|
|
|
*/ |
85
|
|
|
public function findOneBy(string $column, $value): DbalInterface |
86
|
|
|
{ |
87
|
|
|
$result = $this->getOrmModel()->findOneBy([$column, '=', $value]); |
88
|
|
|
|
89
|
|
|
$this->data = $result; |
|
|
|
|
90
|
|
|
$this->modifiedFields = $result; |
|
|
|
|
91
|
|
|
$this->isNew = false; |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $this; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
99
|
|
|
* @throws \Quantum\Exceptions\ModelException |
100
|
|
|
* @throws \SleekDB\Exceptions\IOException |
101
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
102
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
103
|
|
|
*/ |
104
|
|
|
public function first(): DbalInterface |
105
|
|
|
{ |
106
|
|
|
$result = $this->getBuilder()->getQuery()->first(); |
107
|
|
|
|
108
|
|
|
$this->data = $result; |
|
|
|
|
109
|
|
|
$this->modifiedFields = $result; |
|
|
|
|
110
|
|
|
$this->isNew = false; |
|
|
|
|
111
|
|
|
|
112
|
|
|
return $this; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
118
|
|
|
* @throws \Quantum\Exceptions\ModelException |
119
|
|
|
* @throws \SleekDB\Exceptions\IOException |
120
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
121
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
122
|
|
|
*/ |
123
|
|
|
public function count(): int |
124
|
|
|
{ |
125
|
|
|
return count($this->getBuilder()->getQuery()->fetch()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritDoc |
130
|
|
|
*/ |
131
|
|
|
public function asArray(): array |
132
|
|
|
{ |
133
|
|
|
$result = $this->data ?: []; |
134
|
|
|
|
135
|
|
|
if (count($this->hidden) > 0 && count($result)) { |
136
|
|
|
$result = $this->setHidden($result); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
|
|
public function setHidden($result) |
146
|
|
|
{ |
147
|
|
|
return array_diff_key($result, array_flip($this->hidden)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|