Completed
Pull Request — master (#21)
by Daniel
03:27 queued 01:17
created

PropertyCell::configureOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Cell;
6
7
use Psi\Component\Grid\CellInterface;
8
use Psi\Component\Grid\CellViewInterface;
9
use Psi\Component\Grid\RowData;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use Symfony\Component\PropertyAccess\PropertyAccess;
13
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
14
15
class PropertyCell implements CellInterface
16
{
17
    private $accessor;
18
19
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
20
    {
21
        $this->accessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
22
    }
23
24
    public function buildForm(FormBuilderInterface $builder, array $options)
25
    {
26
    }
27
28
    public function createView(RowData $data, array $options): CellViewInterface
29
    {
30
        $property = $options['property'];
31
        $value = $this->accessor->getValue($data->getObject(), $property);
32
33
        return new View\ScalarView($value);
34
    }
35
36
    public function configureOptions(OptionsResolver $options)
37
    {
38
        $options->setDefault('property', null);
39
        $options->setAllowedTypes('property', ['string', 'null']);
40
        $options->setNormalizer('property', function (OptionsResolver $options, $value) {
41
            if (null !== $value) {
42
                return $value;
43
            }
44
45
            return $options['column_name'];
46
        });
47
    }
48
}
49