1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\Admin; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\BaseFieldDescription; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
class FieldDescription extends BaseFieldDescription |
22
|
|
|
{ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->parentAssociationMappings = []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
* |
31
|
|
|
* @throws \InvalidArgumentException if the mapping is no array or of an |
32
|
|
|
* unknown type |
33
|
|
|
*/ |
34
|
|
|
public function setAssociationMapping($associationMapping): void |
35
|
|
|
{ |
36
|
|
|
if (!\is_array($associationMapping)) { |
37
|
|
|
throw new \InvalidArgumentException('The association mapping must be an array'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->associationMapping = $associationMapping; |
41
|
|
|
|
42
|
|
|
if (isset($associationMapping['type'])) { |
43
|
|
|
$this->type = $this->type ?: $associationMapping['type']; |
44
|
|
|
$this->mappingType = $this->mappingType ?: $associationMapping['type']; |
45
|
|
|
} else { |
46
|
|
|
throw new \InvalidArgumentException('Unknown association mapping type'); |
47
|
|
|
} |
48
|
|
|
$this->fieldName = $associationMapping['fieldName']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getTargetEntity() |
55
|
|
|
{ |
56
|
|
|
if (isset($this->associationMapping['targetDocument'])) { |
57
|
|
|
return $this->associationMapping['targetDocument']; |
58
|
|
|
} |
59
|
|
|
if (isset($this->associationMapping['referringDocument'])) { |
60
|
|
|
return $this->associationMapping['referringDocument']; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
* |
67
|
|
|
* @throws \InvalidArgumentException if the mapping information is not an array |
68
|
|
|
*/ |
69
|
|
|
public function setFieldMapping($fieldMapping): void |
70
|
|
|
{ |
71
|
|
|
if (!\is_array($fieldMapping)) { |
72
|
|
|
throw new \InvalidArgumentException('The field mapping must be an array'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->fieldMapping = $fieldMapping; |
76
|
|
|
|
77
|
|
|
$this->type = $this->type ?: $fieldMapping['type']; |
78
|
|
|
$this->mappingType = $this->mappingType ?: $fieldMapping['type']; |
79
|
|
|
$this->fieldName = $this->fieldName ?: $fieldMapping['fieldName']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function isIdentifier() |
86
|
|
|
{ |
87
|
|
|
return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getValue($object) |
94
|
|
|
{ |
95
|
|
|
foreach ($this->parentAssociationMappings as $parentAssociationMapping) { |
96
|
|
|
$object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->getFieldValue($object, $this->fieldName); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
* |
105
|
|
|
* @throws \InvalidArgumentException if the list of mappings does contain |
106
|
|
|
* something else than arrays |
107
|
|
|
*/ |
108
|
|
|
public function setParentAssociationMappings(array $parentAssociationMappings): void |
109
|
|
|
{ |
110
|
|
|
foreach ($parentAssociationMappings as $parentAssociationMapping) { |
111
|
|
|
if (!\is_array($parentAssociationMapping)) { |
112
|
|
|
throw new \RuntimeException('An association mapping must be an array'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->parentAssociationMappings = $parentAssociationMappings; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|