forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryResultTryToJsonListener.php
More file actions
49 lines (43 loc) · 1.4 KB
/
QueryResultTryToJsonListener.php
File metadata and controls
49 lines (43 loc) · 1.4 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
<?php
declare(strict_types=1);
namespace App\Listener;
use App\Event\StatementComplete;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
/**
* @Listener
*/
class QueryResultTryToJsonListener implements ListenerInterface
{
public function listen(): array
{
return [
StatementComplete::class,
];
}
public function process(object $event)
{
if (!$event instanceof StatementComplete) return;
if (empty($event->result)) return;
$statement = $event->statement;
$columnCount = count(array_keys(current($event->result)));
$columnMeta = [];
for ($i = 0; $i <= $columnCount; $i++) {
$meta = $statement->getColumnMeta($i);
if ($meta) {
$columnMeta[$meta['name']] = $meta;
}
}
foreach(array_filter($columnMeta, function ($item) {
return !isset($item['native_type']) && in_array('blob', $item['flags']);
}, ARRAY_FILTER_USE_BOTH) as $item) {
for ($i = 0; $i < count($event->result); $i++) {
if (!is_string($event->result[$i][$item['name']])) continue;
$jsonData = json_decode($event->result[$i][$item['name']], true);
if (is_array($jsonData)) {
$event->result[$i][$item['name']] = $jsonData;
}
}
}
}
}