Completed
Pull Request — master (#79)
by Daniel
02:24
created

PropertyColumn::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Column;
6
7
use Psi\Component\Grid\ColumnInterface;
8
use Psi\Component\Grid\View\Cell;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
12
13
class PropertyColumn extends AbstractColumn
14
{
15
    /**
16
     * @var PropertyAccessor
17
     */
18
    private $accessor;
19
20
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
21
    {
22
        $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...
23
    }
24
25
    public function buildCell(Cell $cell, array $options)
26
    {
27
        $property = $options['property'];
28
29
        // if the column name is the same as the property name, then assume that
30
        // the user has not overridden the property and, if the context is an array,
31
        // access it as such.
32
        if ($options['column_name'] === $options['property'] && false === is_object($cell->getContext())) {
33
            $property = '[' . $property . ']';
34
        }
35
36
        $cell->value = $this->accessor->getValue($cell->getContext(), $property);
37
    }
38
39
    public function configureOptions(OptionsResolver $options)
40
    {
41
        $options->setDefault('property', null);
42
        $options->setAllowedTypes('property', ['string', 'null']);
43
        $options->setNormalizer('property', function (OptionsResolver $options, $value) {
44
            if (null !== $value) {
45
                return $value;
46
            }
47
48
            return $options['column_name'];
49
        });
50
    }
51
}
52