Completed
Branch BUG-10381-asset-loading (2dbb93)
by
unknown
122:01 queued 110:40
created

AsciiOnly::convertAscii()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 1
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\formatters;
3
4
use EventEspresso\core\exceptions\InvalidDataTypeException;
5
6
defined('EVENT_ESPRESSO_VERSION') || exit;
7
8
9
10
/**
11
 * Class AsciiOnly
12
 * Removes all non-ascii characters from the string
13
 *
14
 * @package        Event Espresso
15
 * @author         Mike Nelson
16
 * @since          4.9.31.p
17
 */
18
class AsciiOnly extends FormatterBase
19
{
20
21
    /**
22
     * Removes all non Ascii characters from string
23
     *
24
     * @param string|int|float $input anything easily cast into a string
25
     * @return string
26
     */
27
    public function format($input)
28
    {
29
        //in case an int or float etc was passed in
30
        $input = (string)$input;
31
        $input = $this->convertAscii($input);
32
        return $input;
33
    }
34
35
36
37
    /**
38
     * Taken from https://gist.github.com/jaywilliams/119517
39
     * @param $string
40
     * @return string
41
     */
42
    protected function convertAscii($string)
43
    {
44
        // Replace Single Curly Quotes
45
        $search[]  = chr(226).chr(128).chr(152);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$search was never initialized. Although not strictly required by PHP, it is generally a good practice to add $search = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
        $replace[] = "'";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$replace was never initialized. Although not strictly required by PHP, it is generally a good practice to add $replace = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47
        $search[]  = chr(226).chr(128).chr(153);
48
        $replace[] = "'";
49
        // Replace Smart Double Curly Quotes
50
        $search[]  = chr(226).chr(128).chr(156);
51
        $replace[] = '"';
52
        $search[]  = chr(226).chr(128).chr(157);
53
        $replace[] = '"';
54
        // Replace En Dash
55
        $search[]  = chr(226).chr(128).chr(147);
56
        $replace[] = '--';
57
        // Replace Em Dash
58
        $search[]  = chr(226).chr(128).chr(148);
59
        $replace[] = '---';
60
        // Replace Bullet
61
        $search[]  = chr(226).chr(128).chr(162);
62
        $replace[] = '*';
63
        // Replace Middle Dot
64
        $search[]  = chr(194).chr(183);
65
        $replace[] = '*';
66
        // Replace Ellipsis with three consecutive dots
67
        $search[]  = chr(226).chr(128).chr(166);
68
        $replace[] = '...';
69
        // Apply Replacements
70
        $string = str_replace($search, $replace, $string);
71
        // Remove any non-ASCII Characters
72
        $string = preg_replace("/[^\x01-\x7F]/","", $string);
73
        return $string;
74
    }
75
}
76
// End of file EmojiRemoval.php
77
// Location: core\services\formatters/EmojiRemoval.php