1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Zicht\Bundle\PageBundle\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Helper class to configure the contentitem region / type matrix. |
10
|
|
|
* |
11
|
|
|
* Usage: |
12
|
|
|
* |
13
|
|
|
* function getContentItemMatrix() { |
14
|
|
|
* return ContentItemMatrix::create() |
15
|
|
|
* ->region('left') |
16
|
|
|
* ->type(Text::class) |
17
|
|
|
* ->type(Embed::class) |
18
|
|
|
* ->region('right') |
19
|
|
|
* ->type(Text::class) |
20
|
|
|
* ; |
21
|
|
|
* } |
22
|
|
|
* return |
23
|
|
|
*/ |
24
|
|
|
final class ContentItemMatrix |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Points to the current region being configured. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $currentRegion; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Contains the matrix |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $matrix; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Stubbed constructor. |
42
|
|
|
*/ |
43
|
2 |
|
public function __construct() |
44
|
|
|
{ |
45
|
2 |
|
$this->currentRegion = 'left'; |
46
|
2 |
|
$this->matrix = []; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Provides fluent interface for building the matrix. |
51
|
|
|
* |
52
|
|
|
* @return ContentItemMatrix |
53
|
|
|
*/ |
54
|
2 |
|
public static function create() |
55
|
|
|
{ |
56
|
2 |
|
return new self(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Select a region to configure. |
61
|
|
|
* |
62
|
|
|
* @param string $region |
63
|
|
|
* @param bool $reset |
64
|
|
|
* |
65
|
|
|
* @return ContentItemMatrix |
66
|
|
|
*/ |
67
|
2 |
|
public function region($region, $reset = false) |
68
|
|
|
{ |
69
|
2 |
|
$this->currentRegion = $region; |
70
|
2 |
|
if ($reset) { |
71
|
1 |
|
$this->matrix[$this->currentRegion] = []; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
2 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remove a region |
79
|
|
|
* |
80
|
|
|
* @param string $region |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function removeRegion($region) |
85
|
|
|
{ |
86
|
|
|
if (array_key_exists($region, $this->matrix)) { |
87
|
|
|
unset($this->matrix[$region]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Remove a type from a region |
95
|
|
|
* |
96
|
|
|
* @param string $type |
97
|
|
|
* @param string $region |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function removeTypeFromRegion($type, $region) |
102
|
|
|
{ |
103
|
|
|
if (array_key_exists($region, $this->matrix)) { |
104
|
|
|
array_walk( |
105
|
|
|
$this->matrix[ $region ], |
106
|
|
|
function ($value, $idx, $matrix) use ($type, $region) { |
107
|
|
|
$class = explode('\\', $value); |
108
|
|
|
$className = array_pop($class); |
109
|
|
|
if ($className === $type) { |
110
|
|
|
unset($matrix[ $region ][ $idx ]); |
111
|
|
|
} |
112
|
|
|
}, |
113
|
|
|
$this->matrix |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Adds the type to the currently selected region. |
123
|
|
|
* |
124
|
|
|
* @param string $className |
125
|
|
|
* |
126
|
|
|
* @return ContentItemMatrix |
127
|
|
|
*/ |
128
|
2 |
|
public function type($className) |
129
|
|
|
{ |
130
|
2 |
|
if (!class_exists($className)) { |
131
|
|
|
throw new \InvalidArgumentException( |
132
|
|
|
sprintf( |
133
|
|
|
'Class %s is non-existent or could not be loaded', |
134
|
|
|
$className |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
} |
138
|
2 |
|
$this->matrix[$this->currentRegion][] = $className; |
139
|
|
|
|
140
|
2 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the available types for a specified region. |
145
|
|
|
* If not specified, returns all types configured. |
146
|
|
|
* |
147
|
|
|
* @param string $region |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
1 |
|
public function getTypes($region = null) |
152
|
|
|
{ |
153
|
1 |
|
if (null === $region) { |
154
|
1 |
|
$ret = []; |
155
|
1 |
|
foreach ($this->matrix as $types) { |
156
|
1 |
|
$ret = array_merge($ret, $types); |
157
|
1 |
|
} |
158
|
|
|
|
159
|
1 |
|
return array_values(array_unique($ret)); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $this->matrix[$region]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns the available regions for a specified type. |
167
|
|
|
* If not specified, all regions are returned. |
168
|
|
|
* |
169
|
|
|
* @param null $type |
|
|
|
|
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
2 |
|
public function getRegions($type = null) |
174
|
|
|
{ |
175
|
2 |
|
if (null === $type) { |
176
|
2 |
|
return array_keys($this->matrix); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
$regions = []; |
180
|
1 |
|
foreach ($this->matrix as $region => $types) { |
181
|
1 |
|
if (in_array($type, $types)) { |
182
|
1 |
|
$regions[] = $region; |
183
|
1 |
|
} |
184
|
1 |
|
} |
185
|
|
|
|
186
|
1 |
|
return $regions; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function getMatrix() |
193
|
|
|
{ |
194
|
|
|
return $this->matrix; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|