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
<?php
function mgToWeight(int $value, string $unit) {
if ( $unit === 'g' ) {
return number_format(($value / 1000), 0);
} elseif ( $unit === 'kg' ) {
return number_format(($value / 1000000), 2);
}
}
function renderUnits (string $unit) {
return [
[
'unit' => 'g',
'selected' => ($unit == 'g')
],
[
'unit' => 'kg',
'selected' => ($unit == 'kg')
]
];
}
function response (int $responseCode, null|string $message, null|array $extraData = NULL) {
$response = [];
if ( $message !== NULL ) $response['message'] = $message;
if ( $extraData !== NULL ) $response = array_merge($response, $extraData);
http_response_code($responseCode);
exit(json_encode($response));
}