Completed
Push — master ( 829fea...03fe8f )
by Dominik
9s
created

Render::validateConstructionPlanIsNonEmptyArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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