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