Completed
Push — master ( 823b87...20dd6d )
by Daniel
02:15
created

OutputFormBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 10
c 9
b 0
f 0
lcom 1
cbo 0
dl 0
loc 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A listOfKnownLabels() 0 20 2
A providers() 0 21 2
A setOutputForm() 0 14 1
B setFormOptions() 0 28 1
A turnRequestedValueIntoCheckboxStatus() 0 12 3
A typeOfResults() 0 21 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\info_compare;
28
29
trait OutputFormBuilder
30
{
31
32
    private function listOfKnownLabels($inArray)
33
    {
34
        $informatorKnownLabels = array_diff($inArray['KnownLabels'], ['--- List of known labels']);
35
        $tmpOptions            = [];
36
        foreach ($informatorKnownLabels as $value) {
37
            $tmpOptions[] = '<input type="radio" name="Label" id="Label_' . $value . '" value="' . $value . '" '
38
                    . $this->turnRequestedValueIntoCheckboxStatus([
39
                        'CheckedValue'  => $value,
40
                        'Defaults'      => $inArray['Defaults'],
41
                        'RequestedName' => 'Label',
42
                        'SuperGlobals'  => $inArray['SuperGlobals'],
43
                    ])
44
                    . '/>'
45
                    . '<label for="Label_' . $value . '">' . $value . '</label>';
46
        }
47
        return '<fieldset style="float:left;">'
48
                . '<legend>Informator Label to use</legend>'
49
                . implode('<br/>', $tmpOptions)
50
                . '</fieldset>';
51
    }
52
53
    private function providers($inArray)
54
    {
55
        $tmpOptions = [];
56
        foreach ($inArray['Servers'] as $key => $value) {
57
            $tmpOptions[] = '<a href="' . $value['url'] . '" target="_blank">run-me</a>&nbsp;'
58
                    . '<input type="radio" name="' . $inArray['ConfigName'] . '" id="'
59
                    . $inArray['ConfigName'] . '_' . $key . '" value="' . $key . '" '
60
                    . $this->turnRequestedValueIntoCheckboxStatus([
61
                        'CheckedValue'  => (string) $key,
62
                        'Defaults'      => $inArray['Defaults'],
63
                        'RequestedName' => $inArray['ConfigName'],
64
                        'SuperGlobals'  => $inArray['SuperGlobals'],
65
                    ])
66
                    . '/>'
67
                    . '<label for="' . $inArray['ConfigName'] . '_' . $key . '">' . $value['name'] . '</label>';
68
        }
69
        return '<fieldset style="float:left;">'
70
                . '<legend>' . $inArray['TitleStart'] . ' config providers</legend>'
71
                . implode('<br/>', $tmpOptions)
72
                . '</fieldset>';
73
    }
74
75
    protected function setOutputForm($inArray)
76
    {
77
        $sReturn = $this->setFormOptions($inArray);
78
        return '<div class="tabbertab" id="tabOptions" title="Options">'
79
                . '<style type="text/css" media="all" scoped>label { width: auto; }</style>'
80
                . '<form method="get" action="'
81
                . $inArray['SuperGlobals']->server->get('PHP_SELF')
82
                . '">'
83
                . '<input type="submit" value="Apply" />'
84
                . '<br/>' . implode('', $sReturn)
85
                . '</form>'
86
                . '<div style="float:none;clear:both;height:1px;">&nbsp;</div>'
87
                . '</div><!--from tabOptions-->';
88
    }
89
90
    private function setFormOptions($inArray)
91
    {
92
        $sReturn   = [];
93
        $sReturn[] = $this->typeOfResults([
94
            'Defaults'     => $inArray['Defaults'],
95
            'SuperGlobals' => $inArray['SuperGlobals'],
96
        ]);
97
        $sReturn[] = $this->providers([
98
            'ConfigName'   => 'localConfig',
99
            'Defaults'     => $inArray['Defaults'],
100
            'Servers'      => $inArray['Servers'],
101
            'SuperGlobals' => $inArray['SuperGlobals'],
102
            'TitleStart'   => 'Source',
103
        ]);
104
        $sReturn[] = $this->providers([
105
            'ConfigName'   => 'serverConfig',
106
            'Defaults'     => $inArray['Defaults'],
107
            'Servers'      => $inArray['Servers'],
108
            'SuperGlobals' => $inArray['SuperGlobals'],
109
            'TitleStart'   => 'Target',
110
        ]);
111
        $sReturn[] = $this->listOfKnownLabels([
112
            'Defaults'     => $inArray['Defaults'],
113
            'KnownLabels'  => $inArray['KnownLabels'],
114
            'SuperGlobals' => $inArray['SuperGlobals'],
115
        ]);
116
        return $sReturn;
117
    }
118
119
    private function turnRequestedValueIntoCheckboxStatus($inArray)
120
    {
121
        if (is_null($inArray['SuperGlobals']->get($inArray['RequestedName']))) {
122
            $valToSet = (string) $inArray['Defaults'][$inArray['RequestedName']];
123
            $inArray['SuperGlobals']->request->set($inArray['RequestedName'], $valToSet);
124
        }
125
        $checkboxStatus = '';
126
        if ($inArray['SuperGlobals']->get($inArray['RequestedName']) === $inArray['CheckedValue']) {
127
            $checkboxStatus = 'checked ';
128
        }
129
        return $checkboxStatus;
130
    }
131
132
    private function typeOfResults($inArray)
133
    {
134
        return '<fieldset style="float:left;"><legend>Type of results displayed</legend>'
135
                . '<input type="radio" name="displayOnlyDifferent" id="displayOnlyDifferent" value="1" '
136
                . $this->turnRequestedValueIntoCheckboxStatus([
137
                    'CheckedValue'  => '1',
138
                    'Defaults'      => $inArray['Defaults'],
139
                    'RequestedName' => 'displayOnlyDifferent',
140
                    'SuperGlobals'  => $inArray['SuperGlobals'],
141
                ])
142
                . '/><label for="displayOnlyDifferent">Only the Different values</label>'
143
                . '<br/><input type="radio" name="displayOnlyDifferent" id="displayAll" value="0" '
144
                . $this->turnRequestedValueIntoCheckboxStatus([
145
                    'CheckedValue'  => '0',
146
                    'Defaults'      => $inArray['Defaults'],
147
                    'RequestedName' => 'displayOnlyDifferent',
148
                    'SuperGlobals'  => $inArray['SuperGlobals'],
149
                ])
150
                . '/><label for="displayAll">All</label>'
151
                . '</fieldset>';
152
    }
153
}
154