forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventModel.php
More file actions
160 lines (126 loc) · 3.87 KB
/
EventModel.php
File metadata and controls
160 lines (126 loc) · 3.87 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests\Support\Models;
use CodeIgniter\Model;
class EventModel extends Model
{
protected $table = 'user';
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $dateFormat = 'datetime';
protected $allowedFields = [
'name',
'email',
'country',
'deleted_at',
];
protected $beforeInsert = ['beforeInsertMethod'];
protected $afterInsert = ['afterInsertMethod'];
protected $beforeInsertBatch = ['beforeInsertBatchMethod'];
protected $afterInsertBatch = ['afterInsertBatchMethod'];
protected $beforeUpdate = ['beforeUpdateMethod'];
protected $afterUpdate = ['afterUpdateMethod'];
protected $beforeUpdateBatch = ['beforeUpdateBatchMethod'];
protected $afterUpdateBatch = ['afterUpdateBatchMethod'];
protected $beforeDelete = ['beforeDeleteMethod'];
protected $afterDelete = ['afterDeleteMethod'];
protected $beforeFind = ['beforeFindMethod'];
protected $afterFind = ['afterFindMethod'];
// Cache of the most recent eventData from a trigger
public $eventData;
// Testing directive to set $returnData on beforeFind event
public $beforeFindReturnData = false;
// Holds stuff for testing events
protected $tokens = [];
protected function beforeInsertMethod(array $data)
{
$this->tokens[] = 'beforeInsert';
$this->eventData = $data;
return $data;
}
protected function afterInsertMethod(array $data)
{
$this->tokens[] = 'afterInsert';
$this->eventData = $data;
return $data;
}
protected function beforeInsertBatchMethod(array $data)
{
$this->tokens[] = 'beforeInsertBatch';
$this->eventData = $data;
return $data;
}
protected function afterInsertBatchMethod(array $data)
{
$this->tokens[] = 'afterInsertBatch';
$this->eventData = $data;
return $data;
}
protected function beforeUpdateMethod(array $data)
{
$this->tokens[] = 'beforeUpdate';
$this->eventData = $data;
return $data;
}
protected function afterUpdateMethod(array $data)
{
$this->tokens[] = 'afterUpdate';
$this->eventData = $data;
return $data;
}
protected function beforeUpdateBatchMethod(array $data)
{
$this->tokens[] = 'beforeUpdateBatch';
$this->eventData = $data;
return $data;
}
protected function afterUpdateBatchMethod(array $data)
{
$this->tokens[] = 'afterUpdateBatch';
$this->eventData = $data;
return $data;
}
protected function beforeDeleteMethod(array $data)
{
$this->tokens[] = 'beforeDelete';
$this->eventData = $data;
return $data;
}
protected function afterDeleteMethod(array $data)
{
$this->tokens[] = 'afterDelete';
$this->eventData = $data;
return $data;
}
/**
* @param array<string, mixed> $data
*/
protected function beforeFindMethod(array $data)
{
$this->tokens[] = 'beforeFind';
$this->eventData = $data;
if ($this->beforeFindReturnData) {
$data['data'] = 'foobar';
$data['returnData'] = true;
}
return $data;
}
protected function afterFindMethod(array $data)
{
$this->tokens[] = 'afterFind';
$this->eventData = $data;
return $data;
}
public function hasToken(string $token)
{
return in_array($token, $this->tokens, true);
}
}