1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\Mapper; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Povs\ListerBundle\Exception\ListFieldException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Povilas Margaiatis <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractMapper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var AbstractField[]|ArrayCollection |
15
|
|
|
*/ |
16
|
|
|
protected $fields = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* AbstractMapper constructor. |
20
|
|
|
*/ |
21
|
31 |
|
public function __construct() |
22
|
|
|
{ |
23
|
31 |
|
$this->fields = new ArrayCollection(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $id |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
18 |
|
public function has(string $id): bool |
32
|
|
|
{ |
33
|
18 |
|
return $this->fields->containsKey($id); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $id |
38
|
|
|
* |
39
|
|
|
* @return AbstractField |
40
|
|
|
*/ |
41
|
12 |
|
public function get(string $id): AbstractField |
42
|
|
|
{ |
43
|
12 |
|
if (!$this->has($id)) { |
44
|
3 |
|
throw ListFieldException::fieldNotExists($id); |
45
|
|
|
} |
46
|
|
|
|
47
|
9 |
|
return $this->fields->offsetGet($id); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $id |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
3 |
|
public function remove(string $id): self |
56
|
|
|
{ |
57
|
3 |
|
if ($this->has($id)) { |
58
|
3 |
|
$this->fields->offsetUnset($id); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return ArrayCollection|AbstractField[] |
66
|
|
|
*/ |
67
|
2 |
|
public function getFields(): ArrayCollection |
68
|
|
|
{ |
69
|
2 |
|
return $this->fields; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param AbstractField $field |
74
|
|
|
*/ |
75
|
30 |
|
protected function addField(AbstractField $field): void |
76
|
|
|
{ |
77
|
30 |
|
if (($position = $field->getOption(ListField::OPTION_POSITION)) && $this->insertBefore($field, $position)) { |
78
|
1 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
30 |
|
$this->fields->offsetSet($field->getId(), $field); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param AbstractField $field Field to insert |
86
|
|
|
* @param string $position Field id. New field will be inserted before this field. |
87
|
|
|
* |
88
|
|
|
* @return bool whether field was inserted. |
89
|
|
|
*/ |
90
|
1 |
|
private function insertBefore(AbstractField $field, string $position): bool |
91
|
|
|
{ |
92
|
1 |
|
$fields = $this->fields->toArray(); |
93
|
1 |
|
$offset = array_search($position, array_keys($fields), true); |
94
|
|
|
|
95
|
1 |
|
if (false === $offset) { |
96
|
1 |
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$newFields = array_merge( |
100
|
1 |
|
array_slice($fields, 0, (int) $offset, true), |
101
|
1 |
|
[$field->getId() => $field], |
102
|
1 |
|
array_slice($fields, (int) $offset, null, true) |
103
|
1 |
|
); |
104
|
1 |
|
$this->fields = new ArrayCollection($newFields); |
105
|
|
|
|
106
|
1 |
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|