-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseResult.php
118 lines (105 loc) · 2.78 KB
/
DatabaseResult.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace App\Core\Framework\Structures;
use App\Core\Framework\Structures\Collection;
class DatabaseResult
{
/**
* @var bool The result of the database operation.
*/
public bool $result;
/**
* @var string The code associated with the database operation.
*/
public string $code;
/**
* @var string The message associated with the database operation.
*/
public string $message;
/**
* @var int The number of rows affected by the database operation.
*/
public int $rowCount;
/**
* @var array The fetched data from the database operation.
*/
public array $fetch;
/**
* @var int The last inserted id from the database operation.
* If the operation is not an insert operation, this value will be -1.
*/
public int $lastInsertId;
/**
* DatabaseResult constructor.
*
* @param bool $result The result of the database operation.
* @param string $code The code associated with the database operation.
* @param string $message The message associated with the database operation.
* @param int $rowCount The number of rows affected by the database operation.
* @param array $fetch The fetched data from the database operation.
*/
public function __construct(bool $result, string $code, string $message, int $rowCount, array $fetch = [], int $lastInsertId = -1)
{
$this->result = $result;
$this->code = $code;
$this->message = $message;
$this->rowCount = $rowCount;
$this->fetch = $fetch;
$this->lastInsertId = $lastInsertId;
}
/**
* Converts the DatabaseResult object to JSON.
*
* @return string The JSON representation of the DatabaseResult object.
*/
public function __toJSON()
{
return json_encode($this);
}
/**
* Converts the DatabaseResult object to a string.
*
* @return string The message associated with the DatabaseResult object.
*/
public function __toString()
{
return $this->message;
}
/**
* Converts the DatabaseResult object to an array.
*
* @return array The array representation of the DatabaseResult object.
*/
public function __toArray()
{
return [
"result" => $this->result,
"code" => $this->code,
"message" => $this->message,
"rowCount" => $this->rowCount,
"fetch" => $this->fetch
];
}
/**
* Converts the DatabaseResult object to a Collection.
*
* @return Collection The Collection representation of the DatabaseResult object.
*/
public function __toCollection(){
$Collection = new Collection();
foreach ($this->fetch as $Row) {
$Collection->addElement($Row);
}
return $Collection;
}
public function __toArrayModel(string $modelClass){
$Collection = new Collection();
foreach ($this->fetch as $Row) {
$Model = new $modelClass();
foreach ($Row as $Key => $Value) {
$Model->$Key = $Value;
}
$Collection->addElement($Model);
}
return $Collection;
}
}