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