Completed
Pull Request — master (#395)
by
unknown
02:26
created

FieldDescription::__construct()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
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\DoctrinePHPCRAdminBundle\Admin;
13
14
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
15
use Sonata\AdminBundle\Admin\BaseFieldDescription;
16
17
/**
18
 * {@inheritdoc}
19
 */
20
class FieldDescription extends BaseFieldDescription
21
{
22
    public function __construct()
23
    {
24
        $this->parentAssociationMappings = array();
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws \InvalidArgumentException if the mapping is no array or of an
31
     *                                   unknown type.
32
     */
33
    public function setAssociationMapping($associationMapping)
34
    {
35
        if (!is_array($associationMapping)) {
36
            throw new \InvalidArgumentException('The association mapping must be an array');
37
        }
38
39
        $this->associationMapping = $associationMapping;
40
41
        if (isset($associationMapping['type'])) {
42
            $this->type = $this->type ?: $associationMapping['type'];
43
            $this->mappingType = $this->mappingType ?: $associationMapping['type'];
44
        } else {
45
            throw new \InvalidArgumentException('Unknown association mapping type');
46
        }
47
        $this->fieldName = $associationMapping['fieldName'];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getTargetEntity()
54
    {
55
        if (isset($this->associationMapping['targetDocument'])) {
56
            return $this->associationMapping['targetDocument'];
57
        }
58
        if (isset($this->associationMapping['referringDocument'])) {
59
            return $this->associationMapping['referringDocument'];
60
        }
61
62
        return;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @throws \InvalidArgumentException if the mapping information is not an array.
69
     */
70
    public function setFieldMapping($fieldMapping)
71
    {
72
        if (!is_array($fieldMapping)) {
73
            throw new \InvalidArgumentException('The field mapping must be an array');
74
        }
75
76
        $this->fieldMapping = $fieldMapping;
77
78
        $this->type = $this->type ?: $fieldMapping['type'];
79
        $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
80
        $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function isIdentifier()
87
    {
88
        return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getValue($object)
95
    {
96
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
97
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
98
        }
99
100
        return $this->getFieldValue($object, $this->fieldName);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     *
106
     * @throws \InvalidArgumentException if the list of mappings does contain
107
     *                                   something else than arrays.
108
     */
109
    public function setParentAssociationMappings(array $parentAssociationMappings)
110
    {
111
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
112
            if (!is_array($parentAssociationMapping)) {
113
                throw new \RuntimeException('An association mapping must be an array');
114
            }
115
        }
116
117
        $this->parentAssociationMappings = $parentAssociationMappings;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    final public function describesSingleValuedAssociation()
124
    {
125
        return in_array($this->mappingType, array(
126
            ClassMetadata::MANY_TO_ONE,
127
            'child',
128
            'parent',
129
        ), true);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    final public function describesCollectionValuedAssociation()
136
    {
137
        return in_array($this->mappingType, array(
138
            ClassMetadata::MANY_TO_MANY,
139
            'children',
140
            'referrers',
141
            'mixedreferrers',
142
        ), true);
143
    }
144
}
145