1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Rpc; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\ApiEntityManager; |
6
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
7
|
|
|
use Bankiru\Api\Doctrine\Mapping\EntityMetadata; |
8
|
|
|
use Bankiru\Api\Doctrine\Proxy\ApiCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
|
11
|
|
|
/** @internal */ |
12
|
|
|
final class SearchArgumentsTransformer |
13
|
|
|
{ |
14
|
|
|
/** @var ApiMetadata */ |
15
|
|
|
private $metadata; |
16
|
|
|
/** @var ApiEntityManager */ |
17
|
|
|
private $manager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SearchArgumentsTransformer constructor. |
21
|
|
|
* |
22
|
|
|
* @param ApiMetadata $metadata |
23
|
|
|
* @param ApiEntityManager $manager |
24
|
|
|
*/ |
25
|
17 |
|
public function __construct(ApiMetadata $metadata, ApiEntityManager $manager) |
26
|
|
|
{ |
27
|
17 |
|
$this->metadata = $metadata; |
28
|
17 |
|
$this->manager = $manager; |
29
|
17 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Converts doctrine entity criteria to API-ready criteria (converts types and field names) |
33
|
|
|
* |
34
|
|
|
* @param array $criteria |
35
|
|
|
* |
36
|
|
|
* @return array API-ready criteria |
37
|
|
|
*/ |
38
|
17 |
|
public function transformCriteria(array $criteria) |
39
|
|
|
{ |
40
|
17 |
|
$apiCriteria = []; |
41
|
17 |
|
foreach ($criteria as $field => $values) { |
42
|
17 |
|
if ($this->metadata->hasAssociation($field)) { |
43
|
7 |
|
$mapping = $this->metadata->getAssociationMapping($field); |
44
|
|
|
/** @var EntityMetadata $target */ |
45
|
7 |
|
$target = $this->manager->getClassMetadata($mapping['target']); |
46
|
|
|
|
47
|
|
|
$converter = function ($value) use ($target) { |
48
|
7 |
|
if (!is_object($value)) { |
49
|
4 |
|
return $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
$ids = $target->getIdentifierValues($value); |
53
|
5 |
|
if ($target->isIdentifierComposite) { |
54
|
|
|
return $ids; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
return array_shift($ids); |
58
|
7 |
|
}; |
59
|
|
|
|
60
|
7 |
|
if ($values instanceof Collection) { |
61
|
|
|
if ($values instanceof ApiCollection && !$values->isInitialized()) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
$values = $values->toArray(); |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
if (is_array($values)) { |
68
|
|
|
$values = array_map($converter, $values); |
69
|
|
|
} else { |
70
|
7 |
|
$values = $converter($values); |
71
|
|
|
} |
72
|
7 |
|
} else { |
73
|
17 |
|
$caster = function ($value) use ($field) { |
74
|
17 |
|
$type = $this->manager |
75
|
17 |
|
->getConfiguration() |
76
|
17 |
|
->getTypeRegistry()->get($this->metadata->getTypeOfField($field)); |
77
|
|
|
|
78
|
17 |
|
return $type->toApiValue($value); |
79
|
17 |
|
}; |
80
|
|
|
|
81
|
17 |
|
if (is_array($values)) { |
82
|
|
|
$values = array_map($caster, $values); |
83
|
|
|
} else { |
84
|
17 |
|
$values = $caster($values); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
17 |
|
$apiCriteria[$this->metadata->getApiFieldName($field)] = $values; |
89
|
17 |
|
} |
90
|
|
|
|
91
|
17 |
|
return $apiCriteria; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Converts doctrine entity order to API-ready order (converts field names) |
96
|
|
|
* |
97
|
|
|
* @param array $orderBy |
98
|
|
|
* |
99
|
|
|
* @return array API-ready order |
100
|
|
|
*/ |
101
|
4 |
|
public function transformOrder(array $orderBy = null) |
102
|
|
|
{ |
103
|
4 |
|
$apiOrder = []; |
104
|
4 |
|
foreach ((array)$orderBy as $field => $direction) { |
105
|
|
|
$apiOrder[$this->metadata->getApiFieldName($field)] = $direction; |
106
|
4 |
|
} |
107
|
|
|
|
108
|
4 |
|
return $apiOrder; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|