1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt; |
4
|
|
|
|
5
|
|
|
class Render |
6
|
|
|
{ |
7
|
|
|
public static function fromConstructionPlan($constructionPlan) |
8
|
|
|
{ |
9
|
|
|
do_action('Flynt/beforeRenderConstructionPlan', $constructionPlan); |
10
|
|
|
|
11
|
|
|
$html = self::recursivelyRenderFromConstructionPlan($constructionPlan); |
12
|
|
|
|
13
|
|
|
do_action('Flynt/afterRenderConstructionPlan', $constructionPlan); |
14
|
|
|
|
15
|
|
|
return $html; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected static function recursivelyRenderFromConstructionPlan($constructionPlan) |
19
|
|
|
{ |
20
|
|
|
if (false === self::validateConstructionPlan($constructionPlan)) { |
21
|
|
|
return ''; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$areaHtml = self::extractAreaHtml($constructionPlan); |
25
|
|
|
|
26
|
|
|
return self::applyRenderFilters($constructionPlan, $areaHtml); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected static function validateConstructionPlan($constructionPlan) |
30
|
|
|
{ |
31
|
|
|
return self::validateConstructionPlanIsNonEmptyArray($constructionPlan) |
32
|
|
|
&& self::validateConstructionPlanName($constructionPlan) |
33
|
|
|
&& self::validateConstructionPlanData($constructionPlan); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected static function validateConstructionPlanIsNonEmptyArray($constructionPlan) |
37
|
|
|
{ |
38
|
|
|
if (!is_array($constructionPlan)) { |
39
|
|
|
trigger_error( |
40
|
|
|
'Construction Plan is not an array! ' . ucfirst(gettype($constructionPlan)) . ' given.', |
41
|
|
|
E_USER_WARNING |
42
|
|
|
); |
43
|
|
|
return false; |
44
|
|
|
} elseif (empty($constructionPlan)) { |
45
|
|
|
trigger_error('Empty Construction Plan array!', E_USER_WARNING); |
46
|
|
|
return false; |
47
|
|
|
} else { |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected static function validateConstructionPlanName($constructionPlan) |
53
|
|
|
{ |
54
|
|
|
if (!isset($constructionPlan['name'])) { |
55
|
|
|
trigger_error('Construction Plan is missing key: "name"', E_USER_WARNING); |
56
|
|
|
return false; |
57
|
|
|
} elseif (!is_string($constructionPlan['name'])) { |
58
|
|
|
trigger_error('Construction Plan key "name" is not a string!', E_USER_WARNING); |
59
|
|
|
return false; |
60
|
|
|
} else { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected static function validateConstructionPlanData($constructionPlan) |
66
|
|
|
{ |
67
|
|
|
if (!isset($constructionPlan['data'])) { |
68
|
|
|
trigger_error('Construction Plan is missing key: "data"', E_USER_WARNING); |
69
|
|
|
return false; |
70
|
|
|
} elseif (!is_array($constructionPlan['data'])) { |
71
|
|
|
trigger_error('Construction Plan key "data" is not an array!', E_USER_WARNING); |
72
|
|
|
return false; |
73
|
|
|
} else { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected static function extractAreaHtml($constructionPlan) |
79
|
|
|
{ |
80
|
|
|
$areaHtml = []; |
81
|
|
|
if (array_key_exists('areas', $constructionPlan)) { |
82
|
|
|
if (!is_array($constructionPlan['areas'])) { |
83
|
|
|
trigger_error('Construction Plan key "areas" is not an array!', E_USER_WARNING); |
84
|
|
|
} else { |
85
|
|
|
$areas = $constructionPlan['areas']; |
86
|
|
|
$areaHtml = array_reduce( |
87
|
|
|
array_keys($areas), |
88
|
|
|
function ($carry, $areaName) use ($areas) { |
89
|
|
|
$carry[$areaName] = self::joinAreaComponents($areaName, $areas); |
90
|
|
|
return $carry; |
91
|
|
|
}, |
92
|
|
|
[] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return $areaHtml; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected static function joinAreaComponents($areaName, $areas) |
100
|
|
|
{ |
101
|
|
|
$components = $areas[$areaName]; |
102
|
|
|
|
103
|
|
|
// "areas" need to be an associative array |
104
|
|
|
if (is_int($areaName)) { |
105
|
|
|
trigger_error('Area name is not defined!', E_USER_WARNING); |
106
|
|
|
return ''; |
107
|
|
|
} |
108
|
|
|
if (!is_array($components)) { |
109
|
|
|
trigger_error("Area \"{$areaName}\" is not an array!", E_USER_WARNING); |
110
|
|
|
return ''; |
111
|
|
|
} |
112
|
|
|
return implode('', array_map('self::recursivelyRenderFromConstructionPlan', $components)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected static function applyRenderFilters($constructionPlan, $areaHtml) |
116
|
|
|
{ |
117
|
|
|
$componentData = $constructionPlan['data']; |
118
|
|
|
$componentName = $constructionPlan['name']; |
119
|
|
|
|
120
|
|
|
$output = apply_filters('Flynt/renderComponent', null, $componentName, $componentData, $areaHtml); |
121
|
|
|
$output = apply_filters( |
122
|
|
|
"Flynt/renderComponent?name={$componentName}", |
123
|
|
|
$output, |
124
|
|
|
$componentName, |
125
|
|
|
$componentData, |
126
|
|
|
$areaHtml |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return is_null($output) ? '' : $output; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|