-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
99 lines (81 loc) · 2.53 KB
/
Model.php
File metadata and controls
99 lines (81 loc) · 2.53 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
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseModel;
class ServicesModel extends BaseModel {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Model
$this->init('services');
}
/**
* Process a record
*
* @param array $record
* @return array
*/
protected function process(array $record): array
{
// Call the parent constructor
$record = parent::process($record);
// Check if the record has a task
if(array_key_exists('commissions', $record)){
// Decode the JSON fields
if(!is_array($record['commissions'])){
$record['commissions'] = json_decode($record['commissions'] ?? "[]", true);
}
// Loop through each commission in the array
foreach($record['commissions'] as $commissionKey => $commission){
// fetch the commission's user
$record['commissions'][$commissionKey]['user'] = $this->read('users', $commission['user']);
}
}
// Return the processed record
return $record;
}
/**
* Apply Joins to the Query
*
* @param Query $Query
* @return Query
*/
protected function joins(object $Query): object
{
// Apply Joins
$Query->join('product', 'products', 'id')
->join('agreement', 'files', 'id');
return $Query;
}
/**
* 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($key === 'commissions' && is_array($value)){
// Loop through each step in the array
foreach($value as $commissionKey => $commission){
// Sanitize the commission
// Float values
$value[$commissionKey]['rate'] = floatval($commission['rate'] ?? 0.0);
// Integer values
$value[$commissionKey]['user'] = intval($commission['user'] ?? 0);
}
}
// Set the value back to the data array
$data[$key] = $value;
}
// Call the parent update method
return parent::update($id, $data);
}
}