|
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(); |
|
|
|
|
|
|
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->context)) { |
|
33
|
|
|
$property = '[' . $property . ']'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$cell->value = $this->accessor->getValue($cell->context, $property); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function configureOptions(OptionsResolver $options) |
|
40
|
|
|
{ |
|
41
|
|
|
$options->setDefault('header_template', 'Property'); |
|
42
|
|
|
$options->setDefault('property', null); |
|
43
|
|
|
$options->setAllowedTypes('property', ['string', 'null']); |
|
44
|
|
|
$options->setNormalizer('property', function (OptionsResolver $options, $value) { |
|
45
|
|
|
if (null !== $value) { |
|
46
|
|
|
return $value; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $options['column_name']; |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
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..