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