Completed
Push — master ( bae860...fc1986 )
by Grégoire
11s
created

src/Admin/FieldDescription.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrineMongoDBAdminBundle\Admin;
15
16
use Sonata\AdminBundle\Admin\BaseFieldDescription;
17
18
class FieldDescription extends BaseFieldDescription
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function __construct()
24
    {
25
        $this->parentAssociationMappings = [];
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function setAssociationMapping($associationMapping): void
32
    {
33
        if (!\is_array($associationMapping)) {
34
            throw new \RuntimeException('The association mapping must be an array');
35
        }
36
37
        $this->associationMapping = $associationMapping;
38
39
        $this->type = $this->type ?: $associationMapping['type'];
40
        $this->mappingType = $this->mappingType ?: $associationMapping['type'];
41
        $this->fieldName = $associationMapping['fieldName'];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getTargetEntity()
48
    {
49
        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...
50
            return $this->associationMapping['targetDocument'];
51
        }
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function setFieldMapping($fieldMapping): void
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): void
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