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\DoctrineMongoDBAdminBundle\Admin; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\BaseFieldDescription; |
17
|
|
|
|
18
|
|
|
class FieldDescription extends BaseFieldDescription |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->parentAssociationMappings = []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function setAssociationMapping($associationMapping) |
32
|
|
|
{ |
33
|
|
|
if (!\is_array($associationMapping)) { |
34
|
|
|
throw new \RuntimeException('The association mapping must be an array'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->associationMapping = $associationMapping; |
38
|
|
|
|
39
|
|
|
$this->type = $this->type ?: $associationMapping['type']; |
40
|
|
|
$this->mappingType = $this->mappingType ?: $associationMapping['type']; |
41
|
|
|
$this->fieldName = $associationMapping['fieldName']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* NEXT_MAJOR: Remove this method. |
46
|
|
|
* |
47
|
|
|
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0. Use FieldDescription::getTargetModel() instead. |
48
|
|
|
*/ |
49
|
|
|
public function getTargetEntity() |
50
|
|
|
{ |
51
|
|
|
@trigger_error(sprintf( |
|
|
|
|
52
|
|
|
'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.' |
53
|
|
|
.' Use %s::getTargetModel() instead.', |
54
|
|
|
__METHOD__, |
55
|
|
|
__CLASS__ |
56
|
|
|
), E_USER_DEPRECATED); |
57
|
|
|
|
58
|
|
|
return $this->getTargetModel(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @final since sonata-project/doctrine-mongodb-admin-bundle 3.x. |
63
|
|
|
*/ |
64
|
|
|
public function getTargetModel(): ?string |
65
|
|
|
{ |
66
|
|
|
if ($this->associationMapping) { |
|
|
|
|
67
|
|
|
return $this->associationMapping['targetDocument']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function setFieldMapping($fieldMapping) |
77
|
|
|
{ |
78
|
|
|
if (!\is_array($fieldMapping)) { |
79
|
|
|
throw new \RuntimeException('The field mapping must be an array'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->fieldMapping = $fieldMapping; |
83
|
|
|
|
84
|
|
|
$this->type = $this->type ?: $fieldMapping['type']; |
85
|
|
|
$this->mappingType = $this->mappingType ?: $fieldMapping['type']; |
86
|
|
|
$this->fieldName = $this->fieldName ?: $fieldMapping['fieldName']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function setParentAssociationMappings(array $parentAssociationMappings) |
93
|
|
|
{ |
94
|
|
|
foreach ($parentAssociationMappings as $parentAssociationMapping) { |
95
|
|
|
if (!\is_array($parentAssociationMapping)) { |
96
|
|
|
throw new \RuntimeException('An association mapping must be an array'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->parentAssociationMappings = $parentAssociationMappings; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function isIdentifier() |
107
|
|
|
{ |
108
|
|
|
return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function getValue($object) |
115
|
|
|
{ |
116
|
|
|
foreach ($this->parentAssociationMappings as $parentAssociationMapping) { |
117
|
|
|
$object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->getFieldValue($object, $this->fieldName); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: