Completed
Pull Request — master (#596)
by
unknown
06:21
created

FieldDescription::isIdentifier()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
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\DoctrineORMAdminBundle\Admin;
13
14
use Doctrine\ORM\Mapping\ClassMetadata;
15
use Sonata\AdminBundle\Admin\BaseFieldDescription;
16
17
class FieldDescription extends BaseFieldDescription
18
{
19
    /**
20
     * Constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->parentAssociationMappings = array();
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function setAssociationMapping($associationMapping)
31
    {
32
        if (!is_array($associationMapping)) {
33
            throw new \RuntimeException('The association mapping must be an array');
34
        }
35
36
        $this->associationMapping = $associationMapping;
37
38
        $this->type = $this->type ?: $associationMapping['type'];
39
        $this->mappingType = $this->mappingType ?: $associationMapping['type'];
40
        $this->fieldName = $associationMapping['fieldName'];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getTargetEntity()
47
    {
48
        if ($this->associationMapping) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->associationMapping of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
49
            return $this->associationMapping['targetEntity'];
50
        }
51
52
        return;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function setFieldMapping($fieldMapping)
59
    {
60
        if (!is_array($fieldMapping)) {
61
            throw new \RuntimeException('The field mapping must be an array');
62
        }
63
64
        $this->fieldMapping = $fieldMapping;
65
66
        $this->type = $this->type ?: $fieldMapping['type'];
67
        $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
68
        $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function setParentAssociationMappings(array $parentAssociationMappings)
75
    {
76
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
77
            if (!is_array($parentAssociationMapping)) {
78
                throw new \RuntimeException('An association mapping must be an array');
79
            }
80
        }
81
82
        $this->parentAssociationMappings = $parentAssociationMappings;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function isIdentifier()
89
    {
90
        return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getValue($object)
97
    {
98
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
99
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
100
        }
101
102
        $fieldMapping = $this->getFieldMapping();
103
        // Support embedded object for mapping
104
        // Ref: http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/tutorials/embeddables.html
105
        if (isset($fieldMapping['declaredField'])) {
106
            $object = $this->getFieldValue($object, $fieldMapping['declaredField']);
107
        }
108
109
        return $this->getFieldValue($object, $this->fieldName);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    final public function describesSingleValuedAssociation()
116
    {
117
        return $this->mappingType === ($this->mappingType & ClassMetadata::TO_ONE);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    final public function describesCollectionValuedAssociation()
124
    {
125
        return $this->mappingType === ($this->mappingType & ClassMetadata::TO_MANY);
126
    }
127
}
128