|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\View; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
6
|
|
|
use Povs\ListerBundle\Mapper\ListField; |
|
7
|
|
|
use Povs\ListerBundle\Service\ListQueryBuilder; |
|
8
|
|
|
use Povs\ListerBundle\Service\ValueAccessor; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Povilas Margaiatis <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class RowView implements ViewInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public const TYPE_BODY = 'body'; |
|
16
|
|
|
public const TYPE_HEADER = 'header'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ValueAccessor |
|
20
|
|
|
*/ |
|
21
|
|
|
private $valueAccessor; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Query builder to fetch lazy loadable data |
|
25
|
|
|
* |
|
26
|
|
|
* @var QueryBuilder|null |
|
27
|
|
|
*/ |
|
28
|
|
|
private $queryBuilder; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ListView |
|
32
|
|
|
*/ |
|
33
|
|
|
private $listView; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array row value |
|
37
|
|
|
*/ |
|
38
|
|
|
private $value = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var FieldView[] |
|
42
|
|
|
*/ |
|
43
|
|
|
private $fields = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int|null |
|
47
|
|
|
*/ |
|
48
|
|
|
private $id; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* RowView constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param ValueAccessor $valueAccessor |
|
54
|
|
|
* @param QueryBuilder|null $queryBuilder |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function __construct(ValueAccessor $valueAccessor, ?QueryBuilder $queryBuilder) |
|
57
|
|
|
{ |
|
58
|
4 |
|
$this->valueAccessor = $valueAccessor; |
|
59
|
4 |
|
$this->queryBuilder = $queryBuilder; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return FieldView[] |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getFields(): array |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->fields; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function getValue(): array |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $this->value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return int|null |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function getId(): ?int |
|
82
|
|
|
{ |
|
83
|
2 |
|
return $this->id; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array ['label' => value][] |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function getLabeledValue(): array |
|
90
|
|
|
{ |
|
91
|
2 |
|
$value = []; |
|
92
|
|
|
|
|
93
|
2 |
|
foreach ($this->fields as $field) { |
|
94
|
2 |
|
$value[$field->getLabel()] = $field->getValue(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return ListView |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function getList(): ListView |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->listView; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @internal |
|
110
|
|
|
* |
|
111
|
|
|
* @param ListView $listView |
|
112
|
|
|
* @param string $type |
|
113
|
|
|
* @param array|null $data |
|
114
|
|
|
*/ |
|
115
|
4 |
|
public function init(ListView $listView, string $type, ?array $data): void |
|
116
|
|
|
{ |
|
117
|
4 |
|
$this->listView = $listView; |
|
118
|
4 |
|
$this->fields = []; |
|
119
|
4 |
|
$this->value = []; |
|
120
|
|
|
|
|
121
|
4 |
|
if (self::TYPE_BODY === $type) { |
|
122
|
2 |
|
$this->id = (int) $data[ListQueryBuilder::IDENTIFIER_ALIAS]; |
|
123
|
2 |
|
$data = $this->valueAccessor->normalizeData($data, $this->queryBuilder); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
foreach ($this->listView->getFieldViews() as $fieldView) { |
|
127
|
4 |
|
if ($type === self::TYPE_BODY) { |
|
128
|
2 |
|
$value = $this->valueAccessor->getFieldValue($fieldView, $data); |
|
129
|
|
|
} else { |
|
130
|
2 |
|
$value = $this->valueAccessor->getHeaderValue($fieldView); |
|
131
|
2 |
|
$fieldView->getListField()->setOption(ListField::OPTION_LABEL, $value); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
4 |
|
$fieldView->init($this, $value); |
|
135
|
4 |
|
$this->fields[] = $fieldView; |
|
136
|
4 |
|
$this->value[$fieldView->getListField()->getId()] = $value; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|