Completed
Pull Request — master (#18)
by Daniel
06:17 queued 02:37
created

PropertyCell::createView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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 Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
12
13
class PropertyCell implements CellInterface
14
{
15
    private $accessor;
16
17
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
18
    {
19
        $this->accessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
20
    }
21
22
    public function createView($data, array $options): CellViewInterface
23
    {
24
        $property = $options['property'];
25
        $value = $this->accessor->getValue($data, $property);
26
27
        return new View\ScalarView($value);
28
    }
29
30
    public function configureOptions(OptionsResolver $options)
31
    {
32
        $options->setDefault('property', null);
33
        $options->setAllowedTypes('property', ['string', 'null']);
34
        $options->setNormalizer('property', function (OptionsResolver $options, $value) {
35
            if (null !== $value) {
36
                return $value;
37
            }
38
39
            return $options['column_name'];
40
        });
41
    }
42
}
43