Test Failed
Push — master ( 9ff342...fa2d9a )
by Marcio
07:29 queued 03:30
created

Ucfirst::removeAccentsIstring()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @link      https://github.com/knut7/framework/ for the canonical source repository
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
14
 * @author    Marcio Zebedeu - [email protected]
15
 * @version   1.0.2
16
 */
17
18
namespace Ballybran\Helpers\Utility;
19
20
class Ucfirst
21
{
22
23
24
    /**
25
     * @param string $post
26
     * @param array $char
27
     * @return string
28
     */
29
    public static function _ucfirst($post, $char = array("'", "-", " ")) : string
30
    {
31
32
        $string = strtolower($post);
33
        foreach ($char as $temp) {
34
            $pos = strpos($string, $temp);
35
36
            if ($pos) {
37
38
                $mend = '';
39
                $a_split = explode($temp, $string);
40
                foreach ($a_split as $temp2) {
41
                    $mend .= ucfirst($temp2) . $temp;
42
                }
43
                $string = substr($mend, 0, -1);
44
            }
45
        }
46
        return ucfirst($string);
47
    }
48
    /**
49
     * @param string $strString
50
     * @param int $intLength
51
     * @return string
52
     */
53
    public static function abbreviate($strString, $intLength = NULL) : string
54
    {
55
        $defaultAbbrevLength = 8; //Default abbreviation length if none is specified
56
57
        //Set up the string for processing
58
        $strString = preg_replace("/[^A-Za-z0-9]/", '', $strString); //Remove non-alphanumeric characters
59
        $strString = ucfirst($strString); //Capitalize the first character (helps with abbreviation calcs)
60
        $stringIndex = 0;
61
        //Figure out everything we need to know about the resulting abbreviation string
62
        $uppercaseCount = preg_match_all('/[A-Z]/', $strString, $uppercaseLetters, PREG_OFFSET_CAPTURE); //Record occurences of uppercase letters and their indecies in the $uppercaseLetters array, take note of how many there are
63
        $targetLength = isset($intLength) ? intval($intLength) : $defaultAbbrevLength; //Maximum length of the abbreviation
64
        $uppercaseCount = $uppercaseCount > $targetLength ? $targetLength : $uppercaseCount; //If there are more uppercase letters than the target length, adjust uppercaseCount to ignore overflow
65
        $targetWordLength = round($targetLength / intval($uppercaseCount)); //How many characters need to be taken from each uppercase-designated "word" in order to best meet the target length?
66
        $abbrevLength = 0; //How long the abbreviation currently is
67
        $abbreviation = ''; //The actual abbreviation
68
        //Create respective arrays for the occurence indecies and the actual characters of uppercase characters within the string
69
        for ($i = 0; $i < $uppercaseCount; $i++) {
70
            //$ucIndicies[] = $uppercaseLetters[1];  //Not actually used. Could be used to calculate abbreviations more efficiently than the routine below by strictly considering indecies
71
            $ucLetters[] = $uppercaseLetters[0][$i][0];
72
        }
73
        $characterDeficit = 0; //Gets incremented when an uppercase letter is encountered before $targetCharsPerWord characters have been collected since the last UC char.
74
        $wordIndex = $targetWordLength; //HACK: keeps track of how many characters have been carried into the abbreviation since the last UC char
75
        while ($stringIndex < strlen($strString)) {  //Process the whole input string...
76
            if ($abbrevLength >= $targetLength) {
77
                //...unless the abbreviation has hit the target length cap
78
                break;
79
            }
80
            $currentChar = $strString[$stringIndex++]; //Grab a character from the string, advance the string cursor
81
            if (in_array($currentChar, $ucLetters)) {           //If handling a UC char, consider it a new word
82
                $characterDeficit += $targetWordLength - $wordIndex; //If UC chars are closer together than targetWordLength, keeps track of how many extra characters are required to fit the target length of the abbreviation
83
                $wordIndex = 0; //Set the wordIndex to reflect a new word
84
            } else if ($wordIndex >= $targetWordLength) {
85
                if ($characterDeficit == 0) {
86
                    //If the word is full and we're not short any characters, ignore the character
87
                    continue;
88
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL on line 88 at column 0
Loading history...
89
                else
90
                    $characterDeficit--; //If we are short some characters, decrement the defecit and carry on with adding the character to the abbreviation
91
=======
92
                } else {
93
                                    $characterDefecit--;
94
                }
95
                //If we are short some characters, decrement the defecit and carry on with adding the character to the abbreviation
96
>>>>>>> db6a84ee1ccfc7512761c60858471990924eed14
97
            }
98
            $abbreviation .= $currentChar; //Add the character to the abbreviation
99
            $abbrevLength++; //Increment abbreviation length
100
            $wordIndex++; //Increment the number of characters for this word
101
        }
102
        return $abbreviation;
103
    }
104
105
    /**
106
     * example removeAccentsIstring( "Õ") output "O"
107
     * @param string $string
108
     * @return string
109
     */
110
    public static function removeAccentsIstring($string) : string
111
    {
112
        return preg_replace(array("/(á|à|ã|â|ä)/", "/(Á|À|Ã|Â|Ä)/", "/(é|è|ê|ë)/", "/(É|È|Ê|Ë)/", "/(í|ì|î|ï)/", "/(Í|Ì|Î|Ï)/", "/(ó|ò|õ|ô|ö)/", "/(Ó|Ò|Õ|Ô|Ö)/", "/(ú|ù|û|ü)/", "/(Ú|Ù|Û|Ü)/", "/(ñ)/", "/(Ñ)/", "/(ç)/", "/(Ç)/"), explode(" ", "a A e E i I o O u U n N c C"), $string);
113
114
    }
115
    /**
116
     * @param string $string
117
     * @param bool 
118
     * @return string
119
     */
120
    public static function removeAccents($string, $slug = false) : string
121
    {
122
        $string = strtolower($string);
123
        // ASCII code of vowels
124
        $ascii['a'] = range(224, 230);
125
        $ascii['e'] = range(232, 235);
126
        $ascii['i'] = range(236, 239);
127
        $ascii['o'] = array_merge(range(242, 246), array(240, 248));
128
        $ascii['u'] = range(249, 252);
129
        // ASCII code for other characters
130
        $ascii['b'] = array(223);
131
        $ascii['c'] = array(231);
132
        $ascii['d'] = array(208);
133
        $ascii['n'] = array(241);
134
        $ascii['y'] = array(253, 255);
135
        foreach ($ascii as $key => $item) {
136
            $acentos = '';
137
            foreach ($item AS $codigo) {
138
                $acentos .= chr($codigo);
139
            }
140
            $troca[$key] = '/[' . $acentos . ']/i';
141
        }
142
        $string = preg_replace(array_values($troca), array_keys($troca), $string);
143
        // Slug?
144
        if ($slug) {
145
            // Swap anything that is not letter or number for a character ($slug)           
146
                $string = preg_replace('/[^a-z0-9]/i', $slug, $string);
147
            // Removes repeated characters ($slug)             
148
            $string = preg_replace('/' . $slug . '{2,}/i', $slug, $string);
149
            $string = trim($string, $slug);
150
        }
151
        return $string;
152
    }
153
154
155
}
156