Completed
Push — master ( 1d9387...a47b74 )
by Garrett
08:57
created

ASCII::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace StringObject\Normalize;
4
5
use StringObject\AnyStrObj;
6
7
class ASCII
8
{
9
    // STATIC PROPERTIES
10
    protected static $asciimap = [
11
        'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ',
12
                'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ',
13
                'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ',
14
                'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ'],
15
        'b' => ['б', 'β', 'Ъ', 'Ь', 'ب'],
16
        'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ'],
17
        'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض'],
18
        'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē',
19
                'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ',
20
                'έ', 'е', 'ё', 'э', 'є', 'ə'],
21
        'f' => ['ф', 'φ', 'ف'],
22
        'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ج'],
23
        'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه'],
24
        'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί',
25
                'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ',
26
                'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и'],
27
        'j' => ['ĵ', 'ј', 'Ј'],
28
        'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك'],
29
        'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل'],
30
        'm' => ['м', 'μ', 'م'],
31
        'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن'],
32
        'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ',
33
                'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ',
34
                'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ'],
35
        'p' => ['п', 'π'],
36
        'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر'],
37
        's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص'],
38
        't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط'],
39
        'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū',
40
                'ů', 'ű', 'ŭ', 'ų', 'µ', 'у'],
41
        'v' => ['в'],
42
        'w' => ['ŵ', 'ω', 'ώ'],
43
        'x' => ['χ'],
44
        'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي'],
45
        'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز'],
46
        'aa' => ['ع'],
47
        'ae' => ['ä', 'æ'],
48
        'ch' => ['ч'],
49
        'dj' => ['ђ', 'đ'],
50
        'dz' => ['џ'],
51
        'gh' => ['غ'],
52
        'kh' => ['х', 'خ'],
53
        'lj' => ['љ'],
54
        'nj' => ['њ'],
55
        'oe' => ['ö', 'œ'],
56
        'ps' => ['ψ'],
57
        'sh' => ['ш'],
58
        'shch' => ['щ'],
59
        'ss' => ['ß'],
60
        'th' => ['þ', 'ث', 'ذ', 'ظ'],
61
        'ts' => ['ц'],
62
        'ue' => ['ü'],
63
        'ya' => ['я'],
64
        'yu' => ['ю'],
65
        'zh' => ['ж'],
66
        '(c)' => ['©'],
67
        'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ',
68
                'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ',
69
                'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ',
70
                'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'],
71
        'B' => ['Б', 'Β'],
72
        'C' => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ'],
73
        'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'],
74
        'E' => ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'Ë', 'Ē',
75
                'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ', 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ',
76
                'Ὲ', 'Е', 'Ё', 'Э', 'Є', 'Ə'],
77
        'F' => ['Ф', 'Φ'],
78
        'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ'],
79
        'H' => ['Η', 'Ή'],
80
        'I' => ['Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί',
81
                'Ϊ', 'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И',
82
                'І', 'Ї'],
83
        'K' => ['К', 'Κ'],
84
        'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ'],
85
        'M' => ['М', 'Μ'],
86
        'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν'],
87
        'O' => ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ',
88
                'Ờ', 'Ở', 'Ỡ', 'Ợ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ',
89
                'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О', 'Θ', 'Ө'],
90
        'P' => ['П', 'Π'],
91
        'R' => ['Ř', 'Ŕ', 'Р', 'Ρ'],
92
        'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ'],
93
        'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ'],
94
        'U' => ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Û', 'Ū',
95
                'Ů', 'Ű', 'Ŭ', 'Ų', 'У'],
96
        'V' => ['В'],
97
        'W' => ['Ω', 'Ώ'],
98
        'X' => ['Χ'],
99
        'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ'],
100
        'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ'],
101
        'AE' => ['Ä', 'Æ'],
102
        'CH' => ['Ч'],
103
        'DJ' => ['Ђ'],
104
        'DZ' => ['Џ'],
105
        'KH' => ['Х'],
106
        'LJ' => ['Љ'],
107
        'NJ' => ['Њ'],
108
        'OE' => ['Ö'],
109
        'PS' => ['Ψ'],
110
        'SH' => ['Ш'],
111
        'SHCH' => ['Щ'],
112
        'SS' => ['ẞ'],
113
        'TH' => ['Þ'],
114
        'TS' => ['Ц'],
115
        'UE' => ['Ü'],
116
        'YA' => ['Я'],
117
        'YU' => ['Ю'],
118
        'ZH' => ['Ж'],
119
        ' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82",
120
                "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86",
121
                "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A",
122
                "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80"],
123
    ];
124
125
    // PROPERTIES
126
    protected $strobj;
127
128
    protected function __construct(AnyStrObj $strobj)
129
    {
130
        $this->strobj = $strobj;
131
132
    }
133
134
    public function __call($name, $args)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
135
    {
136
        return $this->strobj->$name($args);
137
    }
138
139
    public function __toString()
140
    {
141
        return $this->strobj->__toString();
142
    }
143
144
    public static function convert(AnyStrObj $strobj)
145
    {
146
        $str = $strobj->raw;
0 ignored issues
show
Documentation introduced by
The property $raw is declared protected in StringObject\AnyStrObj. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
147
        foreach (self::$asciimap as $key => $value) {
148
            $str = \str_replace($value, $key, $str);
149
        }
150
        return self::duplicate($strobj, $str);
151
    }
152
153
    public static function convertPure(AnyStrObj $strobj)
154
    {
155
        $asciiobj = self::convert($strobj);
156
        $str = \preg_replace('/[^\x20-\x7E]/u', '', $asciiobj->raw);
0 ignored issues
show
Bug introduced by
The property raw does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
157
        return self::duplicate($strobj, $str);
158
    }
159
160
    protected static function duplicate(AnyStrObj $strobj, $str)
161
    {
162
        $classname = \get_class($strobj);
163
        return new self(new $classname($str));
164
    }
165
}
166