Completed
Branch dev (69a0d4)
by
unknown
18:27
created

AdminPageFramework_Utility_Array   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 17
lcom 0
cbo 1
dl 0
loc 141
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isAssociative() 0 3 1
A isLastElement() 0 4 1
A isFirstElement() 0 4 1
A getReadableListOfArray() 0 9 2
B getReadableArrayContents() 0 35 6
A getReadableListOfArrayAsHTML() 0 11 2
B getReadableArrayContentsHTML() 0 27 4
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2015 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods dealing with PHP arrays which do not use WordPress functions.
12
 *
13
 * @since       2.0.0
14
 * @package     AdminPageFramework
15
 * @extends     AdminPageFramework_Utility_String
16
 * @subpackage  Utility
17
 * @internal
18
 */
19
abstract class AdminPageFramework_Utility_Array extends AdminPageFramework_Utility_String {
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
    
21
    /**
22
     * Checks if the given array is an associative array or not.
23
     * @since       DEVVER
24
     * @return      boolean
25
     */
26
    static public function isAssociative( array $aArray ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
27
        return array_keys ( $aArray ) !== range( 0, count( $aArray ) - 1 );
28
    }      
29
               
30
    /**
31
     * Determines whether the element is the last element of an array by the given key.
32
     * 
33
     * @since       3.0.0
34
     * @return      boolean
35
     */
36
    static public function isLastElement( array $aArray, $sKey ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
37
        end( $aArray );
38
        return $sKey === key( $aArray );
39
    }    
40
    /**
41
     * Determines whether element is the first element of an array by the given key.
42
     * 
43
     * @since       3.4.0
44
     * @return      boolean
45
     */
46
    static public function isFirstElement( array $aArray, $sKey ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
47
        reset( $aArray );
48
        return $sKey === key( $aArray );
49
    }    
50
            
51
    /**
52
     * Returns a readable list of the given array contents.
53
     * 
54
     * @remark      If the second dimension element is an array, it will be enclosed in parenthesis.
55
     * @since       3.3.0
56
     * @return      string      A readable list generated from the given array.
57
     */
58
    static public function getReadableListOfArray( array $aArray ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
59
        
60
        $_aOutput   = array();
61
        foreach( $aArray as $_sKey => $_vValue ) {        
62
            $_aOutput[] = self::getReadableArrayContents( $_sKey, $_vValue, 32 ) . PHP_EOL;
63
        }
64
        return implode( PHP_EOL, $_aOutput );
65
        
66
    }
67
    /**
68
     * Generates readable array contents.
69
     * 
70
     * @since       3.3.0
71
     * @return      string      The generated human readable array contents.
72
     */
73
    static public function getReadableArrayContents( $sKey, $vValue, $sLabelCharLengths=16, $iOffset=0 ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
74
        
75
        $_aOutput   = array();
76
        $_aOutput[] = ( $iOffset 
77
                ? str_pad( ' ', $iOffset  ) 
78
                : '' 
79
            ) 
80
            . ( $sKey 
81
                ? '[' . $sKey . ']' 
82
                : '' 
83
            );
84
        
85
        if ( ! in_array( gettype( $vValue ), array( 'array', 'object' ) ) ) {
86
            $_aOutput[] = $vValue;
87
            return implode( PHP_EOL, $_aOutput );                
88
        }
89
        
90
        foreach ( $vValue as $_sTitle => $_asDescription ) {
91
            if ( ! in_array( gettype( $_asDescription ), array( 'array', 'object' ) ) ) {
92
                $_aOutput[] = str_pad( ' ', $iOffset )
93
                    . $_sTitle 
94
                    . str_pad( ':', $sLabelCharLengths - self::getStringLength( $_sTitle ) )
95
                    . $_asDescription;
96
                continue;
97
            }
98
            $_aOutput[] = str_pad( ' ', $iOffset )
99
                . $_sTitle 
100
                . ": {" 
101
                . self::getReadableArrayContents( '', $_asDescription, 16, $iOffset + 4 )
102
                . PHP_EOL
103
                . str_pad( ' ', $iOffset ) . "}";
104
        }
105
        return implode( PHP_EOL, $_aOutput );    
106
        
107
    }        
108
    /**
109
     * Returns the readable list of the given array contents as HTML.
110
     * 
111
     * @since       3.3.0
112
     * @return      string      The HTML list generated from the given array.
113
     */
114
    static public function getReadableListOfArrayAsHTML( array $aArray ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
115
116
        $_aOutput   = array();
117
        foreach( $aArray as $_sKey => $_vValue ) {        
118
            $_aOutput[] = "<ul class='array-contents'>" 
119
                    .  self::getReadableArrayContentsHTML( $_sKey, $_vValue )
120
                . "</ul>" . PHP_EOL;
121
        }
122
        return implode( PHP_EOL, $_aOutput );    
123
        
124
    } 
125
        /**
126
         * Returns the readable array contents.
127
         * 
128
         * @since       3.3.0
129
         * @return      string      The HTML output generated from the given array.
130
         */    
131
        static public function getReadableArrayContentsHTML( $sKey, $vValue ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
132
            
133
            // Output container.
134
            $_aOutput   = array();
135
            
136
            // Title - array key
137
            $_aOutput[] = $sKey 
138
                ? "<h3 class='array-key'>" . $sKey . "</h3>"
139
                : "";
140
                
141
            // If it does not have a nested array or object, 
142
            if ( ! in_array( gettype( $vValue ), array( 'array', 'object' ) ) ) {
143
                $_aOutput[] = "<div class='array-value'>" 
144
                        . html_entity_decode( nl2br( str_replace( ' ', '&nbsp;', $vValue ) ), ENT_QUOTES )
145
                    . "</div>";
146
                return "<li>" . implode( PHP_EOL, $_aOutput ) . "</li>";    
147
            }
148
            
149
            // Now it is a nested item.
150
            foreach ( $vValue as $_sKey => $_vValue ) {   
151
                $_aOutput[] =  "<ul class='array-contents'>" 
152
                        . self::getReadableArrayContentsHTML( $_sKey, $_vValue ) 
153
                    . "</ul>";
154
            }
155
            return implode( PHP_EOL, $_aOutput ) ;
156
            
157
        }
158
159
}