Passed
Branch dev (8bd4f4)
by Michael
24:22
created

AdminPageFramework_Utility_Interpreter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 14
eloc 49
dl 0
loc 110
rs 10
c 4
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getReadableListOfArray() 0 7 2
B getReadableArrayContents() 0 33 6
A getReadableListOfArrayAsHTML() 0 8 2
A getReadableArrayContentsHTML() 0 25 4
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2021, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * Provides utility methods which do not use WordPress functions.
12
 *
13
 * @since       3.9.0
14
 * @package     AdminPageFramework/Utility
15
 * @internal
16
 */
17
abstract class AdminPageFramework_Utility_Interpreter extends AdminPageFramework_Utility_InterpreterHTMLTable {
18
19
    /**
20
     * Returns a readable list of the given array contents.
21
     *
22
     * @remark      If the second dimension element is an array, it will be enclosed in parentheses.
23
     * @since       3.3.0
24
     * @since       3.9.0       Moved from `AdminPageFramework_Utility_Array.php`.
25
     * @return      string      A readable list generated from the given array.
26
     */
27
    static public function getReadableListOfArray( array $aArray ) {
28
29
        $_aOutput   = array();
30
        foreach( $aArray as $_sKey => $_vValue ) {
31
            $_aOutput[] = self::getReadableArrayContents( $_sKey, $_vValue, 32 ) . PHP_EOL;
32
        }
33
        return implode( PHP_EOL, $_aOutput );
34
35
    }
36
    /**
37
     * Generates readable array contents.
38
     *
39
     * @since       3.3.0
40
     * @since       3.9.0       Moved from `AdminPageFramework_Utility_Array.php`.
41
     * @return      string      The generated human-readable array contents.
42
     */
43
    static public function getReadableArrayContents( $sKey, $vValue, $sLabelCharLengths=16, $iOffset=0 ) {
44
45
        $_aOutput   = array();
46
        $_aOutput[] = ( $iOffset
47
                ? str_pad( ' ', $iOffset  )
48
                : ''
49
            )
50
            . ( $sKey
51
                ? '[' . $sKey . ']'
52
                : ''
53
            );
54
55
        if ( ! in_array( gettype( $vValue ), array( 'array', 'object' ) ) ) {
56
            $_aOutput[] = $vValue;
57
            return implode( PHP_EOL, $_aOutput );
58
        }
59
60
        foreach ( $vValue as $_sTitle => $_asDescription ) {
61
            if ( ! in_array( gettype( $_asDescription ), array( 'array', 'object' ) ) ) {
62
                $_aOutput[] = str_pad( ' ', $iOffset )
63
                    . $_sTitle
64
                    . str_pad( ':', $sLabelCharLengths - self::getStringLength( $_sTitle ) )
65
                    . $_asDescription;
66
                continue;
67
            }
68
            $_aOutput[] = str_pad( ' ', $iOffset )
69
                . $_sTitle
70
                . ": {"
71
                . self::getReadableArrayContents( '', $_asDescription, 16, $iOffset + 4 )
72
                . PHP_EOL
73
                . str_pad( ' ', $iOffset ) . "}";
74
        }
75
        return implode( PHP_EOL, $_aOutput );
76
77
    }
78
    /**
79
     * Returns the readable list of the given array contents as HTML.
80
     *
81
     * @since       3.3.0
82
     * @since       3.9.0       Moved from `AdminPageFramework_Utility_Array.php`.
83
     * @return      string      The HTML list generated from the given array.
84
     */
85
    static public function getReadableListOfArrayAsHTML( array $aArray ) {
86
        $_aOutput   = array();
87
        foreach( $aArray as $_sKey => $_vValue ) {
88
            $_aOutput[] = "<ul class='array-contents'>"
89
                    .  self::getReadableArrayContentsHTML( $_sKey, $_vValue )
90
                . "</ul>" . PHP_EOL;
91
        }
92
        return implode( PHP_EOL, $_aOutput );
93
    }
94
95
    /**
96
     * Returns the readable array contents.
97
     *
98
     * @since       3.3.0
99
     * @since       3.9.0       Moved from `AdminPageFramework_Utility_Array.php`.
100
     * @return      string      The HTML output generated from the given array.
101
     */
102
    static public function getReadableArrayContentsHTML( $sKey, $vValue ) {
103
104
        // Output container.
105
        $_aOutput   = array();
106
107
        // Title - array key
108
        $_aOutput[] = $sKey
109
            ? "<h3 class='array-key'>" . $sKey . "</h3>"
110
            : "";
111
112
        // If it does not have a nested array or object,
113
        if ( ! in_array( gettype( $vValue ), array( 'array', 'object' ), true ) ) {
114
            $_aOutput[] = "<div class='array-value'>"
115
                    . html_entity_decode( nl2br( $vValue ), ENT_QUOTES )
116
                . "</div>";
117
            return "<li>" . implode( PHP_EOL, $_aOutput ) . "</li>";
118
        }
119
120
        // Now it is a nested item.
121
        foreach ( $vValue as $_sKey => $_vValue ) {
122
            $_aOutput[] =  "<ul class='array-contents'>"
123
                    . self::getReadableArrayContentsHTML( $_sKey, $_vValue )
124
                . "</ul>";
125
        }
126
        return implode( PHP_EOL, $_aOutput ) ;
127
128
    }
129
130
}