Completed
Branch dev (71673f)
by
unknown
05:17
created

AdminPageFramework_Utility_String::hasSuffix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 9.6666
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 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
     * ```
27
     * 0        -> '0px'
28
     * '0'      -> '0px'
29
     * null     -> '0px'
30
     * ''       -> '0px'
31
     * false    -> '0px'
32
     * ```
33
     * 
34
     * @since       3.1.1
35
     * @since       3.8.0   Renamed from `sanitizeLength()`.
36
     * @since       3.8.4   When non-true value is passed, `0px` will be returned.
37
     * @return      string
38
     */
39
    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...
40
        $sLength = $sLength ? $sLength : 0;
41
        return is_numeric( $sLength ) 
42
            ? $sLength . $sUnit
43
            : $sLength;
44
    }    
45
  
46
    /**
47
     * Converts non-alphabetic characters to underscore.
48
     * 
49
     * @since       2.0.0
50
     * @return      string|null     The sanitized string.
51
     * @todo        Change the method name as it does not tell for what it will sanitized.
52
     * @todo        Examine why null needs to be returned.
53
     */ 
54
    public static function sanitizeSlug( $sSlug ) {
55
        return is_null( $sSlug )
56
            ? null
57
            : preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', trim( $sSlug ) );
58
    }    
59
    
60
    /**
61
     * Converts non-alphabetic characters to underscore except hyphen(dash).
62
     * 
63
     * @since       2.0.0
64
     * @return      string|null      The sanitized string.
65
     * @todo        Change the method name as it does not tell for what it will sanitized.
66
     * @todo        Examine why null needs to be returned.
67
     */ 
68
    public static function sanitizeString( $sString ) {
69
        return is_null( $sString )
70
            ? null
71
            : preg_replace( '/[^a-zA-Z0-9_\x7f-\xff\-]/', '_', $sString );
72
    }    
73
        
74
    
75
    /**
76
     * Checks if the passed value is a number and sets it to the default if not.
77
     * 
78
     * This is useful for form data validation. If it is a number and exceeds a set maximum number, 
79
     * it sets it to the maximum value. If it is a number and is below the minimum number, it sets to the minimum value.
80
     * Set a blank value for no limit.
81
     * 
82
     * @since       2.0.0
83
     * @return      string|integer      A numeric value will be returned. 
84
     */ 
85
    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...
86
87
        if ( ! is_numeric( trim( $nToFix ) ) ) { 
88
            return $nDefault; 
89
        }
90
        if ( $nMin !== '' && $nToFix < $nMin ) { 
91
            return $nMin; 
92
        }
93
        if ( $nMax !== '' && $nToFix > $nMax ) { 
94
            return $nMax; 
95
        }
96
        return $nToFix;
97
        
98
    }     
99
    
100
    /**
101
     * Compresses CSS rules.
102
     * 
103
     * @since       3.0.0
104
     * @since       3.7.10      Changed the name from `minifyCSS()`.
105
     * @return      string
106
     */
107
    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...
108
        return str_replace( 
109
            array( "\r\n", "\r", "\n", "\t", '  ', '    ', '    '),  // remove line breaks, tab, and white sspaces.
110
            '', 
111
            preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $sCSSRules ) // remove comments
112
        );
113
    }    
114
              
115
    /**
116
     * Returns the given string length.
117
     * @since       3.3.0
118
     * @return      integer|null        Null if an array is given.
119
     */
120
    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...
121
        return function_exists( 'mb_strlen' )
122
            ? mb_strlen( $sString )
123
            : strlen( $sString );
124
    }    
125
        
126
    /**
127
     * Returns a number from the given human readable size representation.
128
     * 
129
     * For example,
130
     * ```
131
     * 20M   -> 20971520
132
     * 324k  -> 331776
133
     * ```
134
     * 
135
     * Only a single character for the unit is allowed. So ~MB will not be recognized and only the first letter will be left.
136
     * ```
137
     * 20MB  -> '20M'
138
     * 42KB  -> '42K'
139
     * ```
140
     * 
141
     * @since       3.4.6
142
     * @return      string|integer
143
     */     
144
    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...
145
        
146
        $_nReturn     = substr( $nSize, 0, -1 );
147
        switch( strtoupper( substr( $nSize, -1 ) ) ) {
148
            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...
149
                $_nReturn *= 1024;
150
            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...
151
                $_nReturn *= 1024;
152
            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...
153
                $_nReturn *= 1024;
154
            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...
155
                $_nReturn *= 1024;
156
            case 'K':
157
                $_nReturn *= 1024;
158
        }
159
        return $_nReturn;
160
        
161
    }   
162
    
163
    /**
164
     * Returns a human readable size from the given byte number.
165
     * @since       3.4.6
166
     * @return      string
167
     */     
168
    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...
169
        $_aUnits    = array( 0 => 'B', 1 => 'KB', 2 => 'MB', 3 => 'GB' );
170
        $_nLog      = log( $nBytes, 1024 );
171
        $_iPower    = ( int ) $_nLog;
172
        $_iSize     = pow( 1024, $_nLog - $_iPower );
173
        return $_iSize . $_aUnits[ $_iPower ];
174
    }
175
176
    /**
177
     * Trims a starting sub-string if exists.
178
     * @return      string
179
     * @since       3.7.2
180
     */    
181
    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...
182
        return self::hasPrefix( $sPrefix, $sString )
183
            ? substr( $sString, strlen( $sPrefix ) )
184
            : $sString;        
185
    }
186
    /**
187
     * Trims a trailing sub-string if exists.
188
     * @return      string
189
     * @since       3.7.2
190
     */
191
    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...
192
        return self::hasSuffix( $sSuffix, $sString )
193
            ? substr( $sString, 0, strlen( $sSuffix ) * - 1 )
194
            : $sString;
195
    }    
196
    
197
    /**
198
     * Checks if the given string has a certain prefix.
199
     * 
200
     * Used mainly in the __call() method to determine the called undefined method name has a certain prefix.
201
     * 
202
     * @since       3.5.3
203
     * @return      boolean     True if it has the given prefix; otherwise, false.
204
     */
205
    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...
206
        return ( string ) $sNeedle === substr( $sHaystack, 0, strlen( ( string ) $sNeedle ) );
207
    }       
208
 
209
    /**
210
     * Checks if the given string has a certain suffix.
211
     * 
212
     * Used to check file base name etc.
213
     * 
214
     * @since   3.5.4
215
     * @return  boolean
216
     */
217
    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...
218
        
219
        $_iLength = strlen( ( string ) $sNeedle );
220
        if ( 0 === $_iLength ) {
221
            return true;
222
        }
223
        return substr( $sHaystack, - $_iLength ) === $sNeedle;
224
        
225
    }
226
 
227
}
228