Completed
Branch dev (bd8e60)
by
unknown
05:08
created

getDataAttributeArray()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 1
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides utility methods which do not use WordPress functions.
12
 *
13
 * @since       3.7.0
14
 * @subpackage  Utility
15
 * @internal
16
 */
17
abstract class AdminPageFramework_Utility_HTMLAttribute extends AdminPageFramework_Utility_SystemInformation {
18
19
    /**
20
     * Generates inline CSS rules from the given array.
21
     * 
22
     * For example,
23
     * <code>
24
     * array(
25
     *      'width'  => '32px',
26
     *      'height' => '32px',
27
     * )
28
     * </code>
29
     * will be
30
     * <code>
31
     * 'width: 32px; height: 32px;'
32
     * </code>
33
     * 
34
     * @since       3.6.0
35
     * @return      string
36
     */
37
    static public function getInlineCSS( array $aCSSRules ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
38
        $_aOutput = array();
39
        foreach( $aCSSRules as $_sProperty => $_sValue ) {
40
            if ( is_null( $_sValue ) ) {
41
                continue;
42
            }
43
            $_aOutput[] = $_sProperty . ': ' . $_sValue;
44
        }
45
        return implode( '; ', $_aOutput );
46
    }    
47
        /**
48
         * @since           3.2.0
49
         * @deprecated      3.6.0       Use `getInlineCSS()` instead.
50
         */
51
        static public function generateInlineCSS( array $aCSSRules ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
52
            return self::getInlineCSS( $aCSSRules );
53
        }
54
    
55
    /**
56
     * Generates a string of inline styles for the style attribute value from multiple arguments.
57
     * 
58
     * Duplicated items will be merged.
59
     * 
60
     * For example,
61
     * `
62
     * getStyleAttribute( array( 'margin-top' => '10px', 'display: inline-block' ), 'float:right; display: none;' )
63
     * `
64
     * will generate
65
     * `
66
     * margin-top: 10px; display: inline-block; float:right;
67
     * `
68
     * @since       3.6.0
69
     * @return      string
70
     */
71
    static public function getStyleAttribute( $asInlineCSSes ) {
1 ignored issue
show
Unused Code introduced by
The parameter $asInlineCSSes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
72
        
73
        $_aCSSRules = array();
74
        foreach( array_reverse( func_get_args() ) as $_asCSSRules ) {
75
            
76
            // For array, store in the container.
77
            if ( is_array( $_asCSSRules ) ) {
78
                $_aCSSRules = array_merge( $_asCSSRules, $_aCSSRules );
79
                continue;
80
            }
81
            
82
            // At this point, it is a string. Break them down to array elements.
83
            $__aCSSRules = explode( ';', $_asCSSRules );
84
            foreach( $__aCSSRules as $_sPair ) {
85
                $_aCSSPair = explode( ':', $_sPair );
86
                if ( ! isset( $_aCSSPair[ 0 ], $_aCSSPair[ 1 ] ) ) {
87
                    continue;
88
                }
89
                $_aCSSRules[ $_aCSSPair[ 0 ] ] = $_aCSSPair[ 1 ];
90
            }
91
            
92
        }
93
        return self::getInlineCSS( array_unique( $_aCSSRules ) );
94
        
95
    }
96
        /**
97
         * @since           3.3.1
98
         * @deprecated      3.6.0       Use `getStyleAttribute()` instead.
99
         */
100
        static public function generateStyleAttribute( $asInlineCSSes ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
101
            self::getStyleAttribute( $asInlineCSSes );
102
        }
103
    
104
    /**
105
     * Generates a string of class selectors from multiple arguments.
106
     * 
107
     * For example, 
108
     * <code>
109
     * $sClasses = getClassAttribute( array( 'button, button-primary' ), 'remove_button button' );
110
     * </code>
111
     * Will generates
112
     * <code>
113
     *  button button-primary remove_button
114
     * </code>
115
     * 
116
     * @remark      Duplicated items will be merged.
117
     * @since       3.6.0
118
     * @todo        Fix an issue that when a multidimensional array is passed, it causes a warning:  Notice: Array to string conversion.
119
     * @return      string
120
     */
121
    static public function getClassAttribute( /* $asClassSelectors1, $asClassSelectors12, ... */ ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
122
        
123
        $_aClasses  = array();
124
        foreach( func_get_args() as $_asClasses ) {
125
            if ( ! in_array( gettype( $_asClasses ), array( 'array', 'string' ) ) ) {
126
                continue;
127
            }            
128
            $_aClasses = array_merge( 
129
                $_aClasses,
130
                is_array( $_asClasses )
131
                    ? $_asClasses
132
                    : explode( ' ', $_asClasses )
133
            );
134
        }
135
        $_aClasses  = array_unique( array_filter( $_aClasses ) );
136
        
137
        // @todo examine if it is okay to remove the trim() function below.
138
        return trim( implode( ' ', $_aClasses ) );
139
        
140
    }    
141
        /**
142
         * Generates a string of class selectors from multiple arguments.
143
         * 
144
         * @since       3.2.0
145
         * @return      string
146
         * @deprecated  3.6.0
147
         */
148
        static public function generateClassAttribute( /* $asClassSelectors1, $asClassSelectors12 ... */ ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
149
            $_aParams = func_get_args();
150
            return call_user_func_array(
151
                array( __CLASS__ , 'getClassAttribute' ), 
152
                $_aParams
153
            );        
154
        }
155
    
156
    /**
157
     * Returns an array for generating a data attribute from the given associative array.
158
     * 
159
     * @since       3.4.0
160
     * @return      array
161
     */
162
    static public function getDataAttributeArray( array $aArray ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
163
        
164
        $_aNewArray = array();
165
        foreach( $aArray as $sKey => $v ) {
166
            if ( in_array( gettype( $v ), array( 'array', 'object', 'NULL' ) ) ) {
167
                continue;
168
            }
169
            // 3.8.4+
170
            if ( $v === '' ) {
171
                $_aNewArray[ "data-{$sKey}" ] = '';
172
                continue;
173
            }
174
            $_aNewArray[ "data-{$sKey}" ] = $v 
175
                ? $v 
176
                : '0';
177
        }
178
        return $_aNewArray;
179
        
180
    }
181
    
182
}
183