Completed
Pull Request — 3.x (#1049)
by Javier
01:32
created

FieldDescription::getTargetModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\DoctrineORMAdminBundle\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)
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
    public function getTargetEntity()
42
    {
43
        @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
            'Method %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.'
45
            .' Use %s::getTargetModel() instead.',
46
            __METHOD__,
47
            __CLASS__
48
        ), E_USER_DEPRECATED);
49
50
        return $this->getTargetModel();
51
    }
52
53
    public function getTargetModel(): ?string
54
    {
55
        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...
56
            return $this->associationMapping['targetEntity'];
57
        }
58
59
        return null;
60
    }
61
62
    public function setFieldMapping($fieldMapping)
63
    {
64
        if (!\is_array($fieldMapping)) {
65
            throw new \RuntimeException('The field mapping must be an array');
66
        }
67
68
        $this->fieldMapping = $fieldMapping;
69
70
        $this->type = $this->type ?: $fieldMapping['type'];
71
        $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
72
        $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
73
    }
74
75
    public function setParentAssociationMappings(array $parentAssociationMappings)
76
    {
77
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
78
            if (!\is_array($parentAssociationMapping)) {
79
                throw new \RuntimeException('An association mapping must be an array');
80
            }
81
        }
82
83
        $this->parentAssociationMappings = $parentAssociationMappings;
84
    }
85
86
    public function isIdentifier()
87
    {
88
        return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
89
    }
90
91
    public function getValue($object)
92
    {
93
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
94
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
95
        }
96
97
        $fieldMapping = $this->getFieldMapping();
98
        // Support embedded object for mapping
99
        // Ref: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/embeddables.html
100
        if (isset($fieldMapping['declaredField'])) {
101
            $parentFields = explode('.', $fieldMapping['declaredField']);
102
            foreach ($parentFields as $parentField) {
103
                $object = $this->getFieldValue($object, $parentField);
104
            }
105
        }
106
107
        return $this->getFieldValue($object, $this->fieldName);
108
    }
109
}
110