Completed
Push — master ( 9be902...75f49e )
by Daniel
02:41
created

CompareTable::displayTableFromMultiLevelArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 3
eloc 10
nc 2
nop 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 CompareTable
30
{
31
32
    use CompareResults;
33
34
    private $superGlobals;
35
36
    protected function displayTableFromMultiLevelArray($inArray)
37
    {
38
        if ((!is_array($inArray['source'])) || (!is_array($inArray['destination']))) {
39
            return '';
40
        }
41
        $this->superGlobals = $inArray['SuperGlobals'];
42
        return '<table style="width:100%">'
43
                . $this->tableHeader(['Servers' => $inArray['Servers']])
44
                . $this->tableBody([
45
                    'source'      => $inArray['source'],
46
                    'destination' => $inArray['destination'],
47
                ])
48
                . '</table>';
49
    }
50
51
    private function decideDisplayAllOrOnlyDifferent()
52
    {
53
        $displayOnlyDifferent = false;
54
        if ($this->superGlobals->get('displayOnlyDifferent') == '1') {
55
            $displayOnlyDifferent = true;
56
        }
57
        return $displayOnlyDifferent;
58
    }
59
60
    private function displayTableRow($inArray)
61
    {
62
        $sString = '';
63
        if ($inArray['displayOnlyDifferent']) {
64
            if ($inArray['first'] != $inArray['second']) {
65
                $sString = $inArray['rowContent'];
66
            }
67
        } else {
68
            $sString = $inArray['rowContent'];
69
        }
70
        return $sString;
71
    }
72
73
    private function prepareArrayForTableBody($inArray)
74
    {
75
        $source    = $inArray['source'];
76
        $dest      = $inArray['destination'];
77
        $firstRow  = $this->mergeArraysIntoFirstSecond($source, $dest, ['first', 'second']);
78
        $secondRow = $this->mergeArraysIntoFirstSecond($dest, $source, ['second', 'first']);
79
        $row       = array_merge($firstRow, $secondRow);
80
        ksort($row);
81
        return $row;
82
    }
83
84
    private function tableBody($inArray)
85
    {
86
        $row                  = $this->prepareArrayForTableBody($inArray);
87
        $displayOnlyDifferent = $this->decideDisplayAllOrOnlyDifferent();
88
        $aString              = [];
89
        foreach ($row as $key => $value) {
90
            $rowString = '<tr><td style="width:20%;">' . $key . '</td><td style="width:40%;">'
91
                    . str_replace(',', ', ', $value['first']) . '</td><td style="width:40%;">'
92
                    . str_replace(',', ', ', $value['second']) . '</td></tr>';
93
            $aString[] = $this->displayTableRow([
94
                'rowContent'           => $rowString,
95
                'displayOnlyDifferent' => $displayOnlyDifferent,
96
                'first'                => $value['first'],
97
                'second'               => $value['second'],
98
            ]);
99
        }
100
        return '<tbody>' . implode('', $aString) . '</tbody>';
101
    }
102
103
    private function tableHeader($inArray)
104
    {
105
        $urlArguments = '?Label=' . $this->superGlobals->get('Label');
106
        return '<thead><tr>'
107
                . '<th>Identifier</th>'
108
                . '<th><a href="' . $inArray['Servers'][$this->superGlobals->get('localConfig')]['url']
109
                . $urlArguments . '" target="_blank">'
110
                . $inArray['Servers'][$this->superGlobals->get('localConfig')]['name'] . '</a></th>'
111
                . '<th><a href="' . $inArray['Servers'][$this->superGlobals->get('serverConfig')]['url']
112
                . $urlArguments . '" target="_blank">'
113
                . $inArray['Servers'][$this->superGlobals->get('serverConfig')]['name'] . '</a></th>'
114
                . '</tr></thead>';
115
    }
116
}
117