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

PropertyColumn   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

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