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\Idiorm\Statements; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
18
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait Result |
22
|
|
|
* @package Quantum\Libraries\Database |
23
|
|
|
*/ |
24
|
|
|
trait Result |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @throws DatabaseException |
30
|
|
|
*/ |
31
|
|
|
public function get(): array |
32
|
|
|
{ |
33
|
|
|
return array_map(function ($element) { |
34
|
|
|
$item = clone $this; |
35
|
|
|
$item->updateOrmModel($element); |
|
|
|
|
36
|
|
|
return $item; |
37
|
|
|
}, $this->getOrmModel()->find_many()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
* @throws DatabaseException |
43
|
|
|
*/ |
44
|
|
|
public function findOne(int $id): DbalInterface |
45
|
|
|
{ |
46
|
|
|
$ormObject = $this->getOrmModel()->find_one($id); |
47
|
|
|
|
48
|
|
|
if ($ormObject) { |
49
|
|
|
$this->updateOrmModel($ormObject); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
* @throws DatabaseException |
58
|
|
|
*/ |
59
|
|
|
public function findOneBy(string $column, $value): DbalInterface |
60
|
|
|
{ |
61
|
|
|
$ormObject = $this->getOrmModel()->where($column, $value)->find_one(); |
62
|
|
|
if ($ormObject) { |
63
|
|
|
$this->updateOrmModel($ormObject); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
* @throws DatabaseException |
72
|
|
|
*/ |
73
|
|
|
public function first(): DbalInterface |
74
|
|
|
{ |
75
|
|
|
$ormObject = $this->getOrmModel()->find_one(); |
76
|
|
|
if ($ormObject) { |
77
|
|
|
$this->updateOrmModel($ormObject); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
* @throws DatabaseException |
86
|
|
|
*/ |
87
|
|
|
public function count(): int |
88
|
|
|
{ |
89
|
|
|
return $this->getOrmModel()->count(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
* @throws DatabaseException |
95
|
|
|
*/ |
96
|
|
|
public function asArray(): array |
97
|
|
|
{ |
98
|
|
|
$result = $this->getOrmModel()->as_array(); |
99
|
|
|
|
100
|
|
|
if (count($this->hidden) > 0) { |
101
|
|
|
$result = $this->setHidden($result); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $result |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function setHidden($result): array |
112
|
|
|
{ |
113
|
|
|
return array_diff_key($result, array_flip($this->hidden)); |
114
|
|
|
} |
115
|
|
|
} |