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\DbalInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait Result |
21
|
|
|
* @package Quantum\Libraries\Database\Sleekdb\Statements |
22
|
|
|
*/ |
23
|
|
|
trait Result |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @inheritDoc |
27
|
|
|
* @return array |
28
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
29
|
|
|
* @throws \Quantum\Exceptions\ModelException |
30
|
|
|
* @throws \SleekDB\Exceptions\IOException |
31
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
32
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
33
|
|
|
*/ |
34
|
|
|
public function get(): array |
35
|
|
|
{ |
36
|
|
|
return $this->getBuilder()->getQuery()->fetch(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
* @return DbalInterface |
42
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
43
|
|
|
* @throws \SleekDB\Exceptions\IOException |
44
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
45
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
46
|
|
|
*/ |
47
|
|
|
public function findOne(int $id): DbalInterface |
48
|
|
|
{ |
49
|
|
|
$result = $this->getOrmModel()->findById($id); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->data = $result; |
|
|
|
|
52
|
|
|
$this->modifiedFields = $result; |
|
|
|
|
53
|
|
|
$this->isNew = false; |
|
|
|
|
54
|
|
|
|
55
|
|
|
return $this; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
61
|
|
|
* @throws \SleekDB\Exceptions\IOException |
62
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
63
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
64
|
|
|
*/ |
65
|
|
|
public function findOneBy(string $column, $value): DbalInterface |
66
|
|
|
{ |
67
|
|
|
$result = $this->getOrmModel()->findOneBy([$column, '=', $value]); |
68
|
|
|
|
69
|
|
|
$this->data = $result; |
|
|
|
|
70
|
|
|
$this->modifiedFields = $result; |
|
|
|
|
71
|
|
|
$this->isNew = false; |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $this; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
79
|
|
|
* @throws \Quantum\Exceptions\ModelException |
80
|
|
|
* @throws \SleekDB\Exceptions\IOException |
81
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
82
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
83
|
|
|
*/ |
84
|
|
|
public function first(): DbalInterface |
85
|
|
|
{ |
86
|
|
|
$result = $this->getBuilder()->getQuery()->first(); |
87
|
|
|
|
88
|
|
|
$this->data = $result; |
|
|
|
|
89
|
|
|
$this->modifiedFields = $result; |
|
|
|
|
90
|
|
|
$this->isNew = false; |
|
|
|
|
91
|
|
|
|
92
|
|
|
return $this; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
98
|
|
|
* @throws \Quantum\Exceptions\ModelException |
99
|
|
|
* @throws \SleekDB\Exceptions\IOException |
100
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
101
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
102
|
|
|
*/ |
103
|
|
|
public function count(): int |
104
|
|
|
{ |
105
|
|
|
return count($this->getBuilder()->getQuery()->fetch()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
|
|
public function asArray(): array |
112
|
|
|
{ |
113
|
|
|
$result = $this->data ?: []; |
114
|
|
|
|
115
|
|
|
if (count($this->hidden) > 0 && count($result)) { |
116
|
|
|
$result = $this->setHidden($result); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function setHidden($result) |
126
|
|
|
{ |
127
|
|
|
return array_diff_key($result, array_flip($this->hidden)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|