Completed
Pull Request — master (#49)
by Daniel
02:09
created

PropertyColumn::buildCell()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Column;
6
7
use Psi\Component\Grid\CellInterface;
8
use Psi\Component\Grid\ColumnInterface;
9
use Psi\Component\Grid\GridContext;
10
use Psi\Component\Grid\View\Cell;
11
use Psi\Component\Grid\View\Header;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
use Symfony\Component\PropertyAccess\PropertyAccess;
14
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
15
16
class PropertyColumn implements ColumnInterface
17
{
18
    /**
19
     * @var PropertyAccessor
20
     */
21
    private $accessor;
22
23
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
24
    {
25
        $this->accessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
0 ignored issues
show
Documentation Bug introduced by
It seems like $propertyAccessor ?: \Sy...reatePropertyAccessor() of type object<Symfony\Component...pertyAccessorInterface> or object<Symfony\Component...ccess\PropertyAccessor> is incompatible with the declared type object<Psi\Component\Gri...olumn\PropertyAccessor> of property $accessor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
    }
27
28
    public function buildCell(Cell $cell, $data, array $options)
29
    {
30
        $property = $options['property'];
31
        $cell->template = 'property';
32
        $cell->value = $this->accessor->getValue($data, $property);
33
    }
34
35
    public function createHeader(GridContext $context, array $options)
36
    {
37
        return new Header($context, $options['column_name'], $options['property']);
38
    }
39
40
    public function configureOptions(OptionsResolver $options)
41
    {
42
        $options->setDefault('property', null);
43
        $options->setAllowedTypes('property', ['string', 'null']);
44
        $options->setNormalizer('property', function (OptionsResolver $options, $value) {
45
            if (null !== $value) {
46
                return $value;
47
            }
48
49
            return $options['column_name'];
50
        });
51
    }
52
}
53