-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResponse.php
48 lines (47 loc) · 1.49 KB
/
Response.php
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
<?php
namespace Justuno\M2;
use Justuno\Core\Framework\W\Result\Json;
# 2019-10-30
final class Response {
/**
* 2019-11-20
* @used-by \Justuno\M2\Controller\Cart\Add::execute()
* @used-by \Justuno\M2\Controller\Response\Catalog::execute()
* @used-by \Justuno\M2\Controller\Response\Inventory::execute()
* @used-by \Justuno\M2\Controller\Response\Orders::execute()
*/
static function p(\Closure $f):Json {/** @var array(string => mixed) $r */
# 2023-07-15 "Ignore requests of Heritrix web crawler": https://github.com/JustunoCom/m2/issues/51
if (ju_request_ua('heritrix')) {
$r = 'Heritrix is forbidden';
ju_403();
}
else {
try {
$r = $f();
ju_sentry(__CLASS__, sprintf('%s: %s', ju_request_o()->getHttpHost(), ju_class_l(ju_caller_c())));
}
catch (\Exception $e) {
$r = ['message' => $e->getMessage()];
ju_sentry(__CLASS__, $e);
}
}
return Json::i(is_null($r) ? 'OK' : (!is_array($r) ? $r : self::filter($r)));
}
/**
* 2019-10-30 «if a property is null or an empty string do not send it back»: https://github.com/justuno-com/m1/issues/9
* @used-by self::filter()
* @used-by self::p()
* @param array(string => mixed) $a
* @return array(string => mixed)
*/
private static function filter(array $a):array {
$r = []; /** @var array(string => mixed) $r */
foreach ($a as $k => $v) { /** @var string $k */ /** @var mixed $v */
if (!in_array($v, ['', null], true)) {
$r[$k] = !is_array($v) ? $v : self::filter($v);
}
}
return $r;
}
}