-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathModel.php
More file actions
129 lines (110 loc) · 2.57 KB
/
Model.php
File metadata and controls
129 lines (110 loc) · 2.57 KB
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
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Leaf\Auth;
use Leaf\Date;
use Leaf\Db;
use PDOStatement;
/**
* Auth User Model
* ----
* Tiny model class for the user. Provides bare minimum
* functionality for user and user data manipulation.
*
* @since 3.0.0
*/
class Model
{
/**
* User Information
*/
protected User $user;
/**
* DB table name
*/
protected string $table;
/**
* DB connection
*/
protected Db $db;
/**
* User data to save
*/
protected array $dataToSave = [];
public function __construct($data)
{
$this->db = $data['db'];
$this->user = $data['user'];
$this->table = $data['table'];
}
/**
* Create a new model resource
*
* @param array $data Data to be inserted
*
* @return PDOStatement|null
*/
public function create(array $data): ?PDOStatement
{
$data['user_id'] = $this->user->id();
if (Config::get('timestamps')) {
$now = (new Date())->tick()->format(Config::get('timestamps.format'));
$data['created_at'] = $now;
$data['updated_at'] = $now;
}
return $this->db->insert($this->table)->params($data)->execute();
}
/**
* Update a model resource
*
* @param array $data Data to be updated
*
* @return Db
*/
public function update(array $data): Db
{
$data['updated_at'] = (new Date())->tick()->format(Config::get('timestamps.format'));
return $this->db->update($this->table)
->params($data)
->where('user_id', $this->user->id());
}
/**
* Delete a model resource
*
* @return Db
*/
public function delete(): Db
{
return $this->db->delete($this->table)
->where('user_id', $this->user->id());
}
/**
* Get a model resource
*
* @param string $columns Columns to search by separated by commas or *
*
* @return Db
*/
public function table($columns = '*'): Db
{
return $this->db->select($this->table, $columns)
->where('user_id', $this->user->id());
}
/**
* Save a model resource
*
* @return PDOStatement|null
*/
public function save(): ?PDOStatement
{
$success = $this->create($this->dataToSave);
$this->dataToSave = [];
return $success;
}
public function __set($name, $value)
{
$this->dataToSave[$name] = $value;
}
public function __call($name, $arguments)
{
return $this->table()->$name(...$arguments);
}
}