Utils   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 79
ccs 27
cts 27
cp 1
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fastUnicode2ord() 0 19 5
A array_is_list() 0 7 2
A unicode2chr() 0 2 1
A htmlentitiesFromUnicode() 0 2 1
A contains() 0 2 1
1
<?php
2
3
namespace Matecat\SubFiltering\Utils;
4
5
class Utils {
6
7
    /**
8
     * @param $array
9
     *
10
     * @return bool
11
     */
12 83
    public static function array_is_list( $array ): bool {
13
14 83
        if ( $array === [] ) {
15 1
            return true;
16
        }
17
18 82
        return array_keys( $array ) === range( 0, count( $array ) - 1 );
19
20
    }
21
22
    /**
23
     * @param string $needle
24
     * @param string $haystack
25
     *
26
     * @return bool
27
     */
28 6
    public static function contains( string $needle, string $haystack ): bool {
29 6
        return strpos( $haystack, $needle ) !== false;
30
    }
31
32
    /**
33
     * Get the char code from a multibyte char
34
     *
35
     * 2/3 times faster than the old implementation
36
     *
37
     * @param $mb_char string Unicode Multibyte Char String
38
     *
39
     * @return int
40
     *
41
     */
42 7
    public static function fastUnicode2ord( string $mb_char ) {
43 7
        switch ( strlen( $mb_char ) ) {
44 7
            case 1:
45 2
                return ord( $mb_char );
46 5
            case 2:
47 1
                return ( ord( $mb_char[ 0 ] ) - 0xC0 ) * 0x40 +
48 1
                        ord( $mb_char[ 1 ] ) - 0x80;
49 4
            case 3:
50 2
                return ( ord( $mb_char[ 0 ] ) - 0xE0 ) * 0x1000 +
51 2
                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x40 +
52 2
                        ord( $mb_char[ 2 ] ) - 0x80;
53 2
            case 4:
54 1
                return ( ord( $mb_char[ 0 ] ) - 0xF0 ) * 0x40000 +
55 1
                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x1000 +
56 1
                        ( ord( $mb_char[ 2 ] ) - 0x80 ) * 0x40 +
57 1
                        ord( $mb_char[ 3 ] ) - 0x80;
58
        }
59
60 1
        return 20; //as default, return a space (should never happen)
61
    }
62
63
    /**
64
     * @param array $str
65
     *
66
     * @return string
67
     */
68 2
    public static function htmlentitiesFromUnicode( array $str ): string {
69 2
        return "&#" . self::fastUnicode2ord( $str[ 1 ] ) . ";";
70
    }
71
72
    /**
73
     * multibyte string manipulation functions
74
     * source : http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php
75
     * original source: PHPExcel libary (http://phpexcel.codeplex.com/)
76
     * get the char from Unicode code
77
     *
78
     * @param int $o
79
     *
80
     * @return string
81
     */
82 41
    public static function unicode2chr( int $o ): string {
83 41
        return (string)mb_convert_encoding( '&#' . $o . ';', 'UTF-8', 'HTML-ENTITIES' );
84
    }
85
86
}