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