Completed
Pull Request — master (#28)
by Daniel
02:29
created

PropertyColumn   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A createCell() 0 7 1
A configureOptions() 0 14 2
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\RowData;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
12
use Psi\Component\Grid\CellInterface;
13
use Psi\Component\Grid\View\Cell as Cell;
14
15
class PropertyColumn implements ColumnInterface
16
{
17
    private $accessor;
18
19
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
20
    {
21
        $this->accessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
22
    }
23
24
    public function createCell(RowData $data, array $options): CellInterface
25
    {
26
        $property = $options['property'];
27
        $value = $this->accessor->getValue($data->getObject(), $property);
28
29
        return new Cell\ScalarCell($options['variant'], $value);
30
    }
31
32
    public function configureOptions(OptionsResolver $options)
33
    {
34
        $options->setDefault('variant', null);
35
        $options->setDefault('property', null);
36
        $options->setAllowedTypes('property', ['string', 'null']);
37
        $options->setAllowedTypes('variant', ['string', 'null']);
38
        $options->setNormalizer('property', function (OptionsResolver $options, $value) {
39
            if (null !== $value) {
40
                return $value;
41
            }
42
43
            return $options['column_name'];
44
        });
45
    }
46
}
47