-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
110 lines (87 loc) · 2.87 KB
/
Copy pathModel.php
File metadata and controls
110 lines (87 loc) · 2.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
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseModel;
class RolesModel extends BaseModel {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Model
$this->init('roles');
}
/**
* Process a record
*
* @param array $record
* @return array
*/
protected function process(array $record): array
{
// Call the parent constructor
$record = parent::process($record);
// Process the JSON fields
if(!is_array($record['users'])){
$record['users'] = json_decode($record['users'] ?? "[]", true);
}
if(!is_array($record['groups'])){
$record['groups'] = json_decode($record['groups'] ?? "[]", true);
}
if(!is_array($record['permissions'])){
$record['permissions'] = json_decode($record['permissions'] ?? "[]", true);
}
// Return the processed record
return $record;
}
/**
* Update a record
*
* @param int $id
* @param array $data
* @return int
*/
public function update(int $id, array $data): int
{
// Sanitize the Data
foreach($data as $key => $value){
// Add exceptions for specific fields
if(in_array($key, ['users','groups']) && is_array($value)){
// Loop through each value in the array
foreach($value as $subkey => $subvalue){
// Convert the value to an integer
$value[$subkey] = (int)$subvalue;
}
// Filter unique user IDs
$value = array_unique($value);
// Sort the array
sort($value);
}
// Add exceptions for specific fields
if(in_array($key, ['permissions']) && is_array($value)){
// Loop through each value in the array
foreach($value as $subkey => $subvalue){
// Convert the value to an integer
$value[$subkey] = (int)$subvalue;
}
// Sort the array by key
ksort($value);
}
// Check if permissions is not an array
if(in_array($key, ['permissions']) && !is_array($value)){
// Create an empty array
$value = [];
}
// Add exceptions for specific fields
if(in_array($key, ['isDefault'])){
// Filter boolean
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
// Set the value back to the data array
$data[$key] = $value;
}
// Call the parent update method
return parent::update($id, $data);
}
}