1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\DoctrineQueryFilter\Metadata; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\Metadata\Exception\MetadataException; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Alex Patterson <[email protected]> |
13
|
|
|
* @package Arp\DoctrineQueryFilter\Metadata |
14
|
|
|
*/ |
15
|
|
|
final class Metadata implements MetadataInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ClassMetadata |
19
|
|
|
*/ |
20
|
|
|
private ClassMetadata $metadata; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ClassMetadata $metadata |
24
|
|
|
*/ |
25
|
11 |
|
public function __construct(ClassMetadata $metadata) |
26
|
|
|
{ |
27
|
11 |
|
$this->metadata = $metadata; |
28
|
11 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
4 |
|
public function getName(): string |
34
|
|
|
{ |
35
|
4 |
|
return $this->metadata->getName(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $fieldName |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
2 |
|
public function hasField(string $fieldName): bool |
44
|
|
|
{ |
45
|
2 |
|
return $this->metadata->hasField($fieldName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $fieldName |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
* |
53
|
|
|
* @throws MetadataException |
54
|
|
|
*/ |
55
|
4 |
|
public function getFieldMapping(string $fieldName): array |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
4 |
|
return $this->metadata->getFieldMapping($fieldName); |
59
|
1 |
|
} catch (MappingException $e) { |
60
|
1 |
|
throw new MetadataException( |
61
|
1 |
|
sprintf( |
62
|
1 |
|
'Unable to find field mapping for field \'%s::%s\': %s', |
63
|
1 |
|
$this->getName(), |
64
|
|
|
$fieldName, |
65
|
1 |
|
$e->getMessage() |
66
|
|
|
), |
67
|
1 |
|
$e->getCode(), |
68
|
|
|
$e |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $fieldName |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* |
78
|
|
|
* @throws MetadataException |
79
|
|
|
*/ |
80
|
2 |
|
public function getFieldType(string $fieldName): string |
81
|
|
|
{ |
82
|
2 |
|
$type = $this->getFieldMapping($fieldName)['type'] ?? ''; |
83
|
|
|
|
84
|
2 |
|
if (empty($type)) { |
85
|
1 |
|
throw new MetadataException( |
86
|
1 |
|
sprintf('Unable to resolve field data type for \'%s::%s\'', $this->getName(), $fieldName) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $type; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $fieldName |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
2 |
|
public function hasAssociation(string $fieldName): bool |
99
|
|
|
{ |
100
|
2 |
|
return $this->metadata->hasAssociation($fieldName); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $fieldName |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
* |
108
|
|
|
* @throws MetadataException |
109
|
|
|
*/ |
110
|
1 |
|
public function getAssociationMapping(string $fieldName): array |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
1 |
|
return $this->metadata->getAssociationMapping($fieldName); |
114
|
1 |
|
} catch (MappingException $e) { |
115
|
1 |
|
throw new MetadataException( |
116
|
1 |
|
sprintf( |
117
|
1 |
|
'Unable to find association mapping for field \'%s::%s\': %s', |
118
|
1 |
|
$this->getName(), |
119
|
|
|
$fieldName, |
120
|
1 |
|
$e->getMessage() |
121
|
|
|
), |
122
|
1 |
|
$e->getCode(), |
123
|
|
|
$e |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|