Completed
Branch dev (bc2253)
by
unknown
06:09
created

getLengthSanitized()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 5
rs 9.4285
c 1
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 dealing with strings which do not use WordPress functions.
12
 *
13
 * @since       2.0.0
14
 * @package     AdminPageFramework
15
 * @subpackage  Utility
16
 * @internal
17
 */
18
abstract class AdminPageFramework_Utility_String extends AdminPageFramework_Utility_VariableType {
19
  
20
    /**
21
     * Returns the width for HTML attributes.
22
     * 
23
     * When a value may be a number with a unit like, '100%', it returns the value itself.
24
     * When a value misses a unit like '60', it returns with the unit such as '60%'.
25
     * 
26
     * @since       3.1.1
27
     * @since       3.8.0   Renamed from `sanitizeLength()`.
28
     * @return      string
29
     */
30
    static public function getLengthSanitized( $sLength, $sUnit='px' ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
31
        return is_numeric( $sLength ) 
32
            ? $sLength . $sUnit
33
            : $sLength;
34
    }    
35
        /**
36
         * @deprecated  3.8.0       Use `getLengthSanitized()` instead.
37
         */
38
        static public function sanitizeLength( $sLength, $sUnit='px' ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
39
            return self::getLengthSanitized( $sLength, $sUnit );
40
        }
41
  
42
    /**
43
     * Converts non-alphabetic characters to underscore.
44
     * 
45
     * @since       2.0.0
46
     * @return      string|null     The sanitized string.
47
     * @todo        Change the method name as it does not tell for what it will sanitized.
48
     * @todo        Examine why null needs to be returned.
49
     */ 
50
    public static function sanitizeSlug( $sSlug ) {
51
        return is_null( $sSlug )
52
            ? null
53
            : preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', trim( $sSlug ) );
54
    }    
55
    
56
    /**
57
     * Converts non-alphabetic characters to underscore except hyphen(dash).
58
     * 
59
     * @since       2.0.0
60
     * @return      string|null      The sanitized string.
61
     * @todo        Change the method name as it does not tell for what it will sanitized.
62
     * @todo        Examine why null needs to be returned.
63
     */ 
64
    public static function sanitizeString( $sString ) {
65
        return is_null( $sString )
66
            ? null
67
            : preg_replace( '/[^a-zA-Z0-9_\x7f-\xff\-]/', '_', $sString );
68
    }    
69
        
70
    
71
    /**
72
     * Checks if the passed value is a number and sets it to the default if not.
73
     * 
74
     * This is useful for form data validation. If it is a number and exceeds a set maximum number, 
75
     * it sets it to the maximum value. If it is a number and is below the minimum number, it sets to the minimum value.
76
     * Set a blank value for no limit.
77
     * 
78
     * @since       2.0.0
79
     * @return      string|integer      A numeric value will be returned. 
80
     */ 
81
    static public function fixNumber( $nToFix, $nDefault, $nMin='', $nMax='' ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
82
83
        if ( ! is_numeric( trim( $nToFix ) ) ) { 
84
            return $nDefault; 
85
        }
86
        if ( $nMin !== '' && $nToFix < $nMin ) { 
87
            return $nMin; 
88
        }
89
        if ( $nMax !== '' && $nToFix > $nMax ) { 
90
            return $nMax; 
91
        }
92
        return $nToFix;
93
        
94
    }     
95
    
96
    /**
97
     * Compresses CSS rules.
98
     * 
99
     * @since       3.0.0
100
     * @since       3.7.10      Changed the name from `minifyCSS()`.
101
     * @return      string
102
     */
103
    static public function getCSSMinified( $sCSSRules ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
104
        return str_replace( 
105
            array( "\r\n", "\r", "\n", "\t", '  ', '    ', '    '),  // remove line breaks, tab, and white sspaces.
106
            '', 
107
            preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $sCSSRules ) // remove comments
108
        );
109
    }    
110
        /**
111
         * @deprecated     3.7.10      Use `getCSSMinified()` instead.
112
         */
113
        static public function minifyCSS( $sCSSRules ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
114
            trigger_error( 
115
                AdminPageFramework_Registry::NAME . ': ' . sprintf(
116
                    'The method, %1$s, is deprecated. Use %2$s instead.',
117
                    'minifyCSS()',
118
                    'getCSSMinified()'
119
                    
120
                ),
121
                E_USER_NOTICE 
122
            );            
123
            return self::getCSSMinified( $sCSSRules );
124
        }
125
    
126
    /**
127
     * Returns the given string length.
128
     * @since       3.3.0
129
     * @return      integer|null        Null if an array is given.
130
     */
131
    static public function getStringLength( $sString ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
132
        return function_exists( 'mb_strlen' )
133
            ? mb_strlen( $sString )
134
            : strlen( $sString );
135
    }    
136
        
137
    /**
138
     * Returns a number from the given human readable size representation.
139
     * @since       3.4.6
140
     * @return      string|integer
141
     */     
142
    static public function getNumberOfReadableSize( $nSize ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
143
        
144
        $_nReturn     = substr( $nSize, 0, -1 );
145
        switch( strtoupper( substr( $nSize, -1 ) ) ) {
146
            case 'P':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
147
                $_nReturn *= 1024;
148
            case 'T':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
149
                $_nReturn *= 1024;
150
            case 'G':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
151
                $_nReturn *= 1024;
152
            case 'M':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
153
                $_nReturn *= 1024;
154
            case 'K':
155
                $_nReturn *= 1024;
156
        }
157
        return $_nReturn;
158
        
159
    }   
160
    
161
    /**
162
     * Returns a human readable size from the given byte number.
163
     * @since       3.4.6
164
     * @return      string
165
     */     
166
    static public function getReadableBytes( $nBytes ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
167
        $_aUnits    = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
168
        $_nLog      = log( $nBytes, 1024 );
169
        $_iPower    = ( int ) $_nLog;
170
        $_iSize     = pow( 1024, $_nLog - $_iPower );
171
        return $_iSize . $_aUnits[ $_iPower ];
172
    }
173
174
    /**
175
     * Trims a starting sub-string if exists.
176
     * @return      string
177
     * @since       3.7.2
178
     */    
179
    static public function getPrefixRemoved( $sString, $sPrefix ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
180
        return self::hasPrefix( $sPrefix, $sString )
181
            ? substr( $sString, strlen( $sPrefix ) )
182
            : $sStrung;        
0 ignored issues
show
Bug introduced by
The variable $sStrung does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
183
    }
184
    /**
185
     * Trims a traling sub-string if exists.
186
     * @return      string
187
     * @since       3.7.2
188
     */
189
    static public function getSuffixRemoved( $sString, $sSuffix ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
190
        return self::hasSuffix( $sSuffix, $sString )
191
            ? substr( $sString, 0, strlen( $sSuffix ) * - 1 )
192
            : $sString;
193
    }    
194
    
195
    /**
196
     * Checks if the given string has a certain prefix.
197
     * 
198
     * Used mainly in the __call() method to determine the called undefined method name has a certain prefix.
199
     * 
200
     * @since       3.5.3
201
     * @return      boolean     True if it has the given prefix; otherwise, false.
202
     */
203
    static public function hasPrefix( $sNeedle, $sHaystack ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
204
        return $sNeedle === substr( $sHaystack, 0, strlen( $sNeedle ) );
205
    }       
206
 
207
    /**
208
     * Checks if the given string has a certain suffix.
209
     * 
210
     * Used to check file base name etc.
211
     * 
212
     * @since   3.5.4
213
     * @reurn   boolean
214
     */
215
    static public function hasSuffix( $sNeedle, $sHaystack ) {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
216
        
217
        $_iLength = strlen( $sNeedle );
218
        if ( 0 === $_iLength ) {
219
            return true;
220
        }
221
        return substr( $sHaystack, - $_iLength ) === $sNeedle;
222
        
223
    }
224
 
225
}
226