forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseUtils.php
More file actions
214 lines (187 loc) · 7.1 KB
/
CaseUtils.php
File metadata and controls
214 lines (187 loc) · 7.1 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace ProcessMaker\Repositories;
use Illuminate\Support\Collection;
use ProcessMaker\Constants\CaseStatusConstants;
class CaseUtils
{
const ALLOWED_ELEMENT_TYPES = ['task'];
const ALLOWED_REQUEST_TOKENS = ['task', 'scriptTask', 'callActivity'];
const CASE_NUMBER_PREFIX = 'cn_';
const PROCESS_FIELDS = [
'id' => 'id',
'name' => 'name',
];
const REQUEST_FIELDS = [
'id' => 'id',
'name' => 'name',
'parent_request_id' => 'parentRequest.id',
];
const TASK_FIELDS = [
'id' => 'id',
'element_id' => 'element_id',
'name' => 'element_name',
'process_id' => 'process_id',
'element_type' => 'element_type',
'status' => 'status',
];
const KEYWORD_FIELDS = [
'case_number' => 'case_number',
'case_title' => 'case_title',
];
/**
* Get the case number split into keywords.
* @param int $caseNumber
* @return string
*/
public static function getCaseNumberByKeywords(int $caseNumber): string
{
$caseNumber = (string) $caseNumber;
$keywords = array_map(
fn ($i) => self::CASE_NUMBER_PREFIX . substr($caseNumber, 0, $i),
range(1, strlen($caseNumber))
);
return implode(' ', $keywords);
}
/**
* Get the keywords for a case number and additional data.
* @param array $data
* @return string
*/
public static function getKeywords(array $dataKeywords): string
{
$keywords = array_map(function ($key, $keyword) {
return $key === 'case_number' ? self::getCaseNumberByKeywords($keyword) : $keyword;
}, array_keys($dataKeywords), $dataKeywords);
return implode(' ', $keywords);
}
/**
* Store processes.
*
* @param Collection $processes
* @param array|null $processData
* An optional array of additional process data. Each element in the array should have the following structure:
* [
* 'id' => int, // The unique identifier of the process
* 'name' => string // The name of the process
* ]
* @param string|null $processName
* @return Collection
*/
public static function storeProcesses(Collection $processes, ?array $processData = []): Collection
{
// check if the process data has the required keys
if (!empty($processData) && array_key_exists('id', $processData) && array_key_exists('name', $processData)) {
$processes->push($processData);
}
return $processes->unique('id')->values();
}
/**
* Store requests.
*
* @param Collection $requests
* @param array|null $requestData
* An optional array of additional request data. Each element in the array should have the following structure:
* [
* 'id' => int, // The unique identifier of the request
* 'name' => string, // The name of the request
* 'parent_request_id' => int|null // The ID of the parent request, or null if there is no parent
* ]
* @return Collection
*/
public static function storeRequests(Collection $requests, ?array $requestData = []): Collection
{
// check if the request data has the required keys
if (!empty($requestData) && array_key_exists('id', $requestData) && array_key_exists('name', $requestData)) {
$requests->push($requestData);
}
return $requests->unique('id')->values();
}
/**
* Store request tokens.
*
* @param Collection $requestTokens
* @param int|null $tokenId
* @return Collection
*/
public static function storeRequestTokens(Collection $requestTokens, ?int $tokenId = null): Collection
{
if (!is_null($tokenId)) {
$requestTokens->push($tokenId);
}
return $requestTokens->unique()->values();
}
/**
* Store tasks.
*
* @param Collection $tasks
* @param array|null $taskData
* An optional array of additional task data. Each element in the array should have the following structure:
* [
* 'id' => int, // The unique identifier of the task
* 'element_id' => int, // The unique identifier of the element
* 'name' => string, // The name of the element
* 'process_id' => int // The unique identifier of the process
* 'element_type' => string // The type of the element
* ]
* @return Collection
*/
public static function storeTasks(Collection $tasks, ?array $taskData = []): Collection
{
if (
!empty($taskData) && !array_diff(array_keys(self::TASK_FIELDS), array_keys($taskData))
&& in_array($taskData['element_type'], self::ALLOWED_ELEMENT_TYPES)
) {
unset($taskData['element_type']);
// This field is converted to string because: The Json_Search in MySQL only works with strings
$taskData['id'] = (string) $taskData['id'];
$tasks->prepend($taskData);
}
return $tasks->unique('id')->values();
}
/**
* Store participants.
*
* @param Collection $participants
* @param int|null $participantId
* @return Collection
*/
public static function storeParticipants(Collection $participants, ?int $participantId = null): Collection
{
if (!is_null($participantId)) {
$participants->push($participantId);
}
return $participants->unique()->values();
}
/**
* Extract data from an object based on a mapping array.
*
* @param object $object The object to extract data from.
* @param string $mapping An associative array where keys are the desired keys in the output array and values are the corresponding properties in the object.
* @return array The extracted data as an associative array.
*/
public static function extractData(object $object, string $fieldType): array
{
$fields = $fieldType . '_FIELDS';
$mapping = constant('self::' . $fields);
$data = [];
foreach ($mapping as $key => $property) {
$data[$key] = data_get($object, $property);
}
return $data;
}
/**
* The getStatus function returns the status of a case as "IN_PROGRESS" if it is "ACTIVE", otherwise it returns the
* current status.
*
* @param string instanceStatus The `instanceStatus` parameter is a string that represents the status of a case
* instance. The `getStatus` function compares this status with the `ACTIVE` status defined in the
* `CaseStatusConstants` class. If the `instanceStatus` is `ACTIVE`, the function returns `IN_PROGRESS
*
* @return If the is equal to CaseStatusConstants::ACTIVE, then CaseStatusConstants::IN_PROGRESS will
* be returned. Otherwise, will be returned as is.
*/
public static function getStatus(string $instanceStatus)
{
return $instanceStatus === CaseStatusConstants::ACTIVE ? CaseStatusConstants::IN_PROGRESS : $instanceStatus;
}
}