|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\DoctrineMongoDBAdminBundle\Admin; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Admin\BaseFieldDescription; |
|
15
|
|
|
|
|
16
|
|
|
class FieldDescription extends BaseFieldDescription |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->parentAssociationMappings = array(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setAssociationMapping($associationMapping) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!is_array($associationMapping)) { |
|
32
|
|
|
throw new \RuntimeException('The association mapping must be an array'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->associationMapping = $associationMapping; |
|
36
|
|
|
|
|
37
|
|
|
$this->type = $this->type ?: $associationMapping['type']; |
|
38
|
|
|
$this->mappingType = $this->mappingType ?: $associationMapping['type']; |
|
39
|
|
|
$this->fieldName = $associationMapping['fieldName']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getTargetEntity() |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->associationMapping) { |
|
|
|
|
|
|
48
|
|
|
return $this->associationMapping['targetDocument']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setFieldMapping($fieldMapping) |
|
58
|
|
|
{ |
|
59
|
|
|
if (!is_array($fieldMapping)) { |
|
60
|
|
|
throw new \RuntimeException('The field mapping must be an array'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->fieldMapping = $fieldMapping; |
|
64
|
|
|
|
|
65
|
|
|
$this->type = $this->type ?: $fieldMapping['type']; |
|
66
|
|
|
$this->mappingType = $this->mappingType ?: $fieldMapping['type']; |
|
67
|
|
|
$this->fieldName = $this->fieldName ?: $fieldMapping['fieldName']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setParentAssociationMappings(array $parentAssociationMappings) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($parentAssociationMappings as $parentAssociationMapping) { |
|
76
|
|
|
if (!is_array($parentAssociationMapping)) { |
|
77
|
|
|
throw new \RuntimeException('An association mapping must be an array'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->parentAssociationMappings = $parentAssociationMappings; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isIdentifier() |
|
88
|
|
|
{ |
|
89
|
|
|
return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getValue($object) |
|
96
|
|
|
{ |
|
97
|
|
|
foreach ($this->parentAssociationMappings as $parentAssociationMapping) { |
|
98
|
|
|
$object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->getFieldValue($object, $this->fieldName); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.