Completed
Push — master ( 46c3a0...a94e83 )
by Grégoire
13s queued 11s
created

src/Datagrid/DatagridMapper.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\AdminBundle\Datagrid;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
18
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
19
use Sonata\AdminBundle\Mapper\BaseMapper;
20
21
/**
22
 * This class is use to simulate the Form API.
23
 *
24
 * @author Thomas Rabaix <[email protected]>
25
 */
26
class DatagridMapper extends BaseMapper
27
{
28
    /**
29
     * @var DatagridInterface
30
     */
31
    protected $datagrid;
32
33
    public function __construct(
34
        DatagridBuilderInterface $datagridBuilder,
35
        DatagridInterface $datagrid,
36
        AdminInterface $admin
37
    ) {
38
        parent::__construct($datagridBuilder, $admin);
39
        $this->datagrid = $datagrid;
40
    }
41
42
    /**
43
     * @param string $name
44
     * @param string $type
45
     * @param string $fieldType
46
     * @param array  $fieldOptions
47
     *
48
     * @throws \RuntimeException
49
     *
50
     * @return DatagridMapper
51
     */
52
    public function add(
53
        $name,
54
        $type = null,
55
        array $filterOptions = [],
56
        $fieldType = null,
57
        $fieldOptions = null,
58
        array $fieldDescriptionOptions = []
59
    ) {
60
        if (\is_array($fieldOptions)) {
61
            $filterOptions['field_options'] = $fieldOptions;
62
        }
63
64
        if ($fieldType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldType of type string|null is loosely compared to true; 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...
65
            $filterOptions['field_type'] = $fieldType;
66
        }
67
68
        if ($name instanceof FieldDescriptionInterface) {
69
            $fieldDescription = $name;
70
            $fieldDescription->mergeOptions($filterOptions);
71
        } elseif (\is_string($name)) {
72
            if ($this->admin->hasFilterFieldDescription($name)) {
73
                throw new \RuntimeException(sprintf('Duplicate field name "%s" in datagrid mapper. Names should be unique.', $name));
74
            }
75
76
            if (!isset($filterOptions['field_name'])) {
77
                $filterOptions['field_name'] = substr(strrchr('.'.$name, '.'), 1);
78
            }
79
80
            $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
81
                $this->admin->getClass(),
82
                $name,
83
                array_merge($filterOptions, $fieldDescriptionOptions)
84
            );
85
        } else {
86
            throw new \RuntimeException(
87
                'Unknown field name in datagrid mapper.'
88
                .' Field name should be either of FieldDescriptionInterface interface or string.'
89
            );
90
        }
91
92
        if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) {
93
            // add the field with the DatagridBuilder
94
            $this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
95
        }
96
97
        return $this;
98
    }
99
100
    public function get($name)
101
    {
102
        return $this->datagrid->getFilter($name);
103
    }
104
105
    public function has($key)
106
    {
107
        return $this->datagrid->hasFilter($key);
108
    }
109
110
    final public function keys()
111
    {
112
        return array_keys($this->datagrid->getFilters());
113
    }
114
115
    public function remove($key)
116
    {
117
        $this->admin->removeFilterFieldDescription($key);
118
        $this->datagrid->removeFilter($key);
119
120
        return $this;
121
    }
122
123
    public function reorder(array $keys)
124
    {
125
        $this->datagrid->reorderFilters($keys);
126
127
        return $this;
128
    }
129
}
130