Completed
Pull Request — 2.x (#521)
by Maximilian
02:22
created

FieldDescription::setAssociationMapping()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 6
nop 1
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 Sonata\AdminBundle\Admin\BaseFieldDescription;
15
16
/**
17
 * {@inheritdoc}
18
 */
19
class FieldDescription extends BaseFieldDescription
20
{
21
    public function __construct()
22
    {
23
        $this->parentAssociationMappings = [];
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws \InvalidArgumentException if the mapping is no array or of an
30
     *                                   unknown type
31
     */
32
    public function setAssociationMapping($associationMapping)
33
    {
34
        if (!is_array($associationMapping)) {
35
            throw new \InvalidArgumentException('The association mapping must be an array');
36
        }
37
38
        $this->associationMapping = $associationMapping;
39
40
        if (isset($associationMapping['type'])) {
41
            $this->type = $this->type ?: $associationMapping['type'];
42
            $this->mappingType = $this->mappingType ?: $associationMapping['type'];
43
        } else {
44
            throw new \InvalidArgumentException('Unknown association mapping type');
45
        }
46
        $this->fieldName = $associationMapping['fieldName'];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getTargetEntity()
53
    {
54
        if (isset($this->associationMapping['targetDocument'])) {
55
            return $this->associationMapping['targetDocument'];
56
        }
57
        if (isset($this->associationMapping['referringDocument'])) {
58
            return $this->associationMapping['referringDocument'];
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @throws \InvalidArgumentException if the mapping information is not an array
66
     */
67
    public function setFieldMapping($fieldMapping)
68
    {
69
        if (!is_array($fieldMapping)) {
70
            throw new \InvalidArgumentException('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
    /**
81
     * {@inheritdoc}
82
     */
83
    public function isIdentifier()
84
    {
85
        return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getValue($object)
92
    {
93
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
94
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
95
        }
96
97
        return $this->getFieldValue($object, $this->fieldName);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     *
103
     * @throws \InvalidArgumentException if the list of mappings does contain
104
     *                                   something else than arrays
105
     */
106
    public function setParentAssociationMappings(array $parentAssociationMappings)
107
    {
108
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
109
            if (!is_array($parentAssociationMapping)) {
110
                throw new \RuntimeException('An association mapping must be an array');
111
            }
112
        }
113
114
        $this->parentAssociationMappings = $parentAssociationMappings;
115
    }
116
}
117