|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Povs\ListerBundle\DependencyInjection\Locator\FieldTypeLocator; |
|
7
|
|
|
use Povs\ListerBundle\Exception\ListException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @property ListField[]|ArrayCollection $fields |
|
11
|
|
|
* @method ListField get(string $id) |
|
12
|
|
|
* |
|
13
|
|
|
* @author Povilas Margaiatis <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class ListMapper extends AbstractMapper |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var FieldTypeLocator |
|
19
|
|
|
*/ |
|
20
|
|
|
private $fieldTypeLocator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string list type |
|
24
|
|
|
*/ |
|
25
|
|
|
private $type; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ListMapper|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $listMapper; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ListMapper constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param FieldTypeLocator $fieldTypeLocator |
|
36
|
|
|
* @param string $listType |
|
37
|
|
|
* @param ListMapper|null $listMapper |
|
38
|
|
|
*/ |
|
39
|
12 |
|
public function __construct(FieldTypeLocator $fieldTypeLocator, string $listType, ?ListMapper $listMapper = null) |
|
40
|
|
|
{ |
|
41
|
12 |
|
parent::__construct(); |
|
42
|
12 |
|
$this->fieldTypeLocator = $fieldTypeLocator; |
|
43
|
12 |
|
$this->type = $listType; |
|
44
|
12 |
|
$this->listMapper = $listMapper; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $id field id |
|
49
|
|
|
* @param string|null $fieldType field type |
|
50
|
|
|
* @param array $options |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
11 |
|
public function add(string $id, ?string $fieldType = null, ?array $options = []): self |
|
55
|
|
|
{ |
|
56
|
11 |
|
if (null !== $fieldType) { |
|
57
|
1 |
|
$fieldType = $this->fieldTypeLocator->get($fieldType); |
|
58
|
1 |
|
$options = array_merge($fieldType->getDefaultOptions($this->type), $options); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
11 |
|
$listField = new ListField($id, $options, $fieldType); |
|
62
|
11 |
|
$this->addField($listField); |
|
63
|
|
|
|
|
64
|
11 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return ListMapper |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function build(): self |
|
71
|
|
|
{ |
|
72
|
3 |
|
if (!$this->listMapper) { |
|
73
|
1 |
|
throw ListException::listNotConfigured(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$this->fields = $this->listMapper->getFields(); |
|
77
|
|
|
|
|
78
|
2 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param bool|null $lazy |
|
83
|
|
|
* |
|
84
|
|
|
* @return ArrayCollection|FilterField[] |
|
85
|
|
|
*/ |
|
86
|
5 |
|
public function getFields(?bool $lazy = null): ArrayCollection |
|
87
|
|
|
{ |
|
88
|
5 |
|
if (null === $lazy) { |
|
89
|
5 |
|
return $this->fields; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return $this->fields->filter(static function (ListField $listField) use ($lazy) { |
|
93
|
1 |
|
return $listField->getOption(ListField::OPTION_LAZY) === $lazy; |
|
94
|
1 |
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|