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
<?php
namespace ServiceCore\Infrastructure\Domain\Repository;
use ApiQL\ApiQL;
use Doctrine\DBAL\FetchMode;
/**
* Trait QueryableTrait
*
* @package ServiceCore\Infrastructure\Domain\Repository
*/
trait QueryableTrait
{
/**
* @param array $payload
*
* @return array
*/
public function get(array $payload = []): array
{
$query = $this->queryBuilder
->from($this->tableName)
;
if (property_exists($this, 'ordered') && $this->ordered == true) {
$query->orderBy('row_order');
}
$apiQL = new ApiQL($payload, $query);
$apiQL->execute();
$result = $query->execute()->fetchAll(FetchMode::ASSOCIATIVE);
$result = $this->decodeParameters($result);
return $result;
}
/**
* @param int $id
*
* @return array|null
*/
public function getById(int $id): ?array
{
$query = $this->queryBuilder
->select('*')
->from($this->tableName)
->where('id = :id')
->setParameter('id', $id)
;
$result = $query->execute()->fetch(FetchMode::ASSOCIATIVE);
$result = $this->decodeParameters($result);
return $result ? $result : null;
}
/**
* @param string $guid
*
* @return array|null
*/
public function getByGuid(string $guid): ?array
{
$query = $this->queryBuilder
->select('*')
->from($this->tableName)
->where('guid = :guid')
->setParameter('guid', $guid)
;
$result = $query->execute()->fetch(FetchMode::ASSOCIATIVE);
$result = $this->decodeParameters($result);
return $result ? $result : null;
}
/**
* @param mixed $result
*
* @return mixed
*/
public function decodeParameters($result)
{
if (is_array($result) && !empty($result)) {
if (isset($result[0]) && isset($result[0]['properties'])) {
foreach ($result as $index => $value) {
$result[$index]['properties'] = json_decode($value['properties'], true);
}
} elseif (isset($result['properties'])) {
$result['properties'] = json_decode($result['properties'], true);
}
}
return $result;
}
}