Completed
Push — 3.x ( 7b8913...94af5b )
by
unknown
01:20
created

src/Builder/ShowBuilder.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\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
23
class ShowBuilder implements ShowBuilderInterface
24
{
25
    protected $guesser;
26
27
    protected $templates;
28
29
    public function __construct(TypeGuesserInterface $guesser, array $templates)
30
    {
31
        $this->guesser = $guesser;
32
        $this->templates = $templates;
33
    }
34
35
    /**
36
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
37
     */
38
    public function getBaseList(array $options = [])
39
    {
40
        return new FieldDescriptionCollection();
41
    }
42
43
    /**
44
     * @param string|null $type
45
     *
46
     * @return mixed
47
     */
48
    public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
49
    {
50
        if (null === $type) {
51
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
52
            $fieldDescription->setType($guessType->getType());
53
        } else {
54
            $fieldDescription->setType($type);
55
        }
56
57
        $this->fixFieldDescription($admin, $fieldDescription);
58
        $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
59
60
        $list->add($fieldDescription);
61
    }
62
63
    /**
64
     * The method defines the correct default settings for the provided FieldDescription.
65
     */
66
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
67
    {
68
        $fieldDescription->setAdmin($admin);
69
70
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
71
            list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
72
            $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
73
74
            // set the default field mapping
75
            if (isset($metadata->fieldMappings[$lastPropertyName])) {
76
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
77
            }
78
79
            // set the default association mapping
80
            if (isset($metadata->associationMappings[$lastPropertyName])) {
81
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
82
            }
83
        }
84
85
        if (!$fieldDescription->getType()) {
86
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin)));
87
        }
88
89
        $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
90
        $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
91
92
        if (!$fieldDescription->getTemplate()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDescription->getTemplate() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
93
            if ('id' === $fieldDescription->getType()) {
94
                $fieldDescription->setType('string');
95
            }
96
97
            if ('int' === $fieldDescription->getType()) {
98
                $fieldDescription->setType('integer');
99
            }
100
101
            $template = $this->getTemplate($fieldDescription->getType());
102
103
            if (null === $template) {
104
                if (ClassMetadata::ONE === $fieldDescription->getMappingType()) {
105
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig';
106
                } elseif (ClassMetadata::MANY === $fieldDescription->getMappingType()) {
107
                    $template = '@SonataAdmin/CRUD/Association/show_many_to_many.html.twig';
108
                }
109
            }
110
111
            $fieldDescription->setTemplate($template);
112
        }
113
114
        if (\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) {
115
            $admin->attachAdminClass($fieldDescription);
116
        }
117
    }
118
119
    /**
120
     * @param string $type
121
     *
122
     * @return string
123
     */
124
    private function getTemplate($type)
125
    {
126
        if (!isset($this->templates[$type])) {
127
            return;
128
        }
129
130
        return $this->templates[$type];
131
    }
132
}
133