1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Minotaur |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Minotaur\View; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Stores block settings. |
25
|
|
|
*/ |
26
|
|
|
class BlockLayout |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array<string,array<string,int>> |
30
|
|
|
*/ |
31
|
|
|
private $blocks = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Adds a block definition to this layout. |
35
|
|
|
* |
36
|
|
|
* @param $region - The block region |
37
|
|
|
* @param $order - The display order, smallest shows up first |
38
|
|
|
* @param $name - The name of the block object in the container |
39
|
|
|
* @return self provides a fluent interface |
40
|
|
|
*/ |
41
|
3 |
|
public function add(string $region, int $order, string $name): self |
42
|
|
|
{ |
43
|
3 |
|
if (!array_key_exists($region, $this->blocks)) { |
44
|
3 |
|
$this->blocks[$region] = []; |
45
|
|
|
} |
46
|
3 |
|
$this->blocks[$region][$name] = $order; |
47
|
3 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the blocks defined in a region. |
52
|
|
|
* |
53
|
|
|
* @param string $region The block region |
54
|
|
|
* @return array<string> The block object names in display order |
55
|
|
|
*/ |
56
|
2 |
|
public function get(string $region): array |
57
|
|
|
{ |
58
|
2 |
|
$blocks = $this->blocks[$region] ?? []; |
59
|
2 |
|
asort($blocks); |
60
|
2 |
|
return array_keys($blocks); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets all block definitions. |
65
|
|
|
* |
66
|
|
|
* @return array<string,array<string>> The block definitions |
67
|
|
|
*/ |
68
|
1 |
|
public function getAll(): array |
69
|
|
|
{ |
70
|
1 |
|
$blocks = []; |
71
|
1 |
|
foreach ($this->blocks as $region => $names) { |
72
|
1 |
|
$a = $names; |
73
|
1 |
|
asort($a); |
74
|
1 |
|
$blocks[$region] = array_keys($a); |
75
|
|
|
} |
76
|
1 |
|
return $blocks; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adds all the block definitions from another block into this one. |
81
|
|
|
* |
82
|
|
|
* @param $other - The other object |
83
|
|
|
* @return self provides a fluent interface |
84
|
|
|
*/ |
85
|
1 |
|
public function merge(BlockLayout $other): self |
86
|
|
|
{ |
87
|
1 |
|
foreach ($other->blocks as $region => $blocks) { |
88
|
1 |
|
if (!array_key_exists($region, $this->blocks)) { |
89
|
1 |
|
$this->blocks[$region] = []; |
90
|
|
|
} |
91
|
1 |
|
$this->blocks[$region] = array_merge($this->blocks[$region], $blocks); |
92
|
|
|
} |
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|