Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
80 / 80 |
| Str | |
100.00% |
1 / 1 |
|
100.00% |
13 / 13 |
42 | |
100.00% |
80 / 80 |
| strim | |
100.00% |
1 / 1 |
3 | |
100.00% |
2 / 2 |
|||
| jsStrIt | |
100.00% |
1 / 1 |
10 | |
100.00% |
15 / 15 |
|||
| replacePos | |
100.00% |
1 / 1 |
4 | |
100.00% |
7 / 7 |
|||
| camel2Words | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| camel2snake | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| snake2camel | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
|||
| toLabel | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| standardize | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| strit | |
100.00% |
1 / 1 |
4 | |
100.00% |
7 / 7 |
|||
| stritc | |
100.00% |
1 / 1 |
4 | |
100.00% |
7 / 7 |
|||
| strlike | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| fieldit | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| sqlNormalized | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
|||
| <?php | |
| namespace ia\Util; | |
| use ia\Lib\iaPalabra; | |
| class Str { | |
| /** | |
| * superTrim trim (including \s utf spaces), and change multiple spaces to one space | |
| * | |
| * @param string $str | |
| * @return string | |
| */ | |
| public static function strim($str) { | |
| $s1 = preg_replace('/[\pZ\pC]/muS',' ',$str); | |
| // @codeCoverageIgnoreStart | |
| if(preg_last_error()) { | |
| $s1 = preg_replace('/[\pZ\pC]/muS',' ', iconv("UTF-8","UTF-8//IGNORE",$str)); | |
| if(preg_last_error()) | |
| return trim(preg_replace('/ {2,}/mS',' ',$str)); | |
| } | |
| // @codeCoverageIgnoreEnd | |
| return trim(preg_replace('/ {2,}/muS',' ',$s1)); | |
| } | |
| public static function jsStrIt($s) { | |
| if($s === true) { | |
| return "true"; | |
| } | |
| if($s === false) { | |
| return "false"; | |
| } | |
| if($s === null) { | |
| return "null"; | |
| } | |
| if($s === 0 || $s === "0") { | |
| return "0"; | |
| } | |
| if(is_numeric($s) && $s !== '') { | |
| return $s; | |
| } | |
| if(strpos($s, "'") === false) { | |
| return "'$s'"; | |
| } elseif(strpos($s, '"') === false) { | |
| return '"'.$s.'"'; | |
| } | |
| return "'".str_replace("'", "\\'", $s)."'"; | |
| } | |
| /** | |
| * Reemplaza el caracter en posicion $n reemplazado por $replaceChar | |
| * | |
| * @param string $palabra | |
| * @param string $replaceChar | |
| * @param int $n | |
| * @return string con el caracter en posicion $n reemplazado por $replaceChar | |
| */ | |
| public static function replacePos($palabra, $replaceChar, $n) { | |
| if($n < 0) { | |
| if($n == -1) { | |
| return mb_substr($palabra, 0, -1).$replaceChar; | |
| } | |
| return mb_substr($palabra, 0, $n).$replaceChar.mb_substr($palabra, $n + 1); | |
| } | |
| if($n == 0) { | |
| return $replaceChar.$palabra; | |
| } | |
| return mb_substr($palabra, 0, $n - 1).$replaceChar.mb_substr($palabra, $n); | |
| } | |
| public static function camel2Words($s) { | |
| if(empty($s)) { | |
| return $s; | |
| } | |
| $word = self::strim( preg_replace("/([A-Zรรรรรร])/uS", " $1", | |
| preg_replace("/([\\d]+\.*[\d]*)/u", " $1 ", | |
| preg_replace("/[A-Zรรรรรร]([\.\,\:\;]+)/uiS", "$0 $2",$s) | |
| ) | |
| )); | |
| $word[0] = mb_convert_case($word[0], MB_CASE_UPPER); | |
| return $word; | |
| } | |
| public static function camel2snake($s) { | |
| if(empty($s)) { | |
| return $s; | |
| } | |
| $s[0] = mb_convert_case($s[0], MB_CASE_LOWER); | |
| return mb_convert_case(preg_replace("/([A-Zรรรรรร])/u", "_$1", $s), MB_CASE_LOWER); | |
| } | |
| public static function snake2camel($s) { | |
| if(empty($s)) { | |
| return $s; | |
| } | |
| $words = []; | |
| $string = str_replace(" ", "_", self::strim($s)); | |
| foreach(explode('_',$string) as $w) { | |
| $words[] = mb_convert_case($w, MB_CASE_TITLE); | |
| } | |
| $word = implode('', $words); | |
| $word[0] = mb_convert_case($word[0], MB_CASE_LOWER); | |
| return $word; | |
| } | |
| public static function toLabel($s) { | |
| $s = self::camel2snake($s); | |
| $label = preg_replace('/[\s\_]+/uiS', ' ', $s); | |
| if(empty($label)) { | |
| $label = str_replace('_',' ',$s); | |
| } | |
| return self::strim(iaPalabra::ucwords(iaPalabra::acentua($label))); | |
| } | |
| /** | |
| * Facilita comparar palabras: quitando espacios, acentos | |
| * | |
| * @param string $palabra | |
| * @return string $palabra in lowercase, unaccented, trimmed, punctuation chars to spaces, only single spaces. | |
| */ | |
| public static function standardize($palabra) { | |
| $standarized = strtolower( \URLify::downcode(self::strim( | |
| preg_replace("/['\\p{M}\\p{P}]/umS", '', preg_replace("/[\\-_\\,\\!\\?]/umS", ' ', $palabra) ) | |
| ))); | |
| // @codeCoverageIgnoreStart | |
| if($standarized == null) { | |
| return strtolower( \URLify::downcode(self::strim( | |
| str_replace(["'", '"', '.', ','], '', str_replace(['-','_'], ' ', $palabra)) | |
| ))); | |
| } | |
| // @codeCoverageIgnoreEnd | |
| return $standarized; | |
| } | |
| /** | |
| * Quote and protect Sql value | |
| * | |
| * @param string $str | |
| * @param integer $maxLength | |
| * @param bool $maxLengthInCharacters | |
| * @return string | |
| * | |
| * @test "'El Gato' \'ap con slash \\'ap con doble slash, un slash \ x".chr(8).chr(0).chr(26).chr(27)."y es 'felix'" | |
| */ | |
| public static function strit($str, $maxLength=0, $maxLengthInCharacters=true) { | |
| if($str === null) { | |
| return 'null'; | |
| } | |
| if($maxLength) { | |
| if($maxLengthInCharacters) { | |
| $str = mb_substr($str , 0, $maxLength); // operates on characters | |
| } else { | |
| $str = mb_strcut($str , 0, $maxLength); // operates on bytes | |
| } | |
| } | |
| return "'".str_replace( array("\\","'",chr(8),chr(0),chr(26),chr(27)), array("\\\\","''",'','','',''),$str)."'"; | |
| } | |
| /** | |
| * Quote and protect Sql value followed by a coma | |
| * | |
| * @param string $str | |
| * @param integer $maxLength | |
| * @param bool $maxLengthInCharacters | |
| * @return string | |
| * | |
| * @test "'El Gato' \'ap con slash \\'ap con doble slash, un slash \ x".chr(8).chr(0).chr(26).chr(27)."y es 'felix'" | |
| */ | |
| public static function stritc($str, $maxLength=0, $maxLengthInCharacters=true) { | |
| if($str === null) { | |
| return 'null,'; | |
| } | |
| if($maxLength) { | |
| if($maxLengthInCharacters) { | |
| $str = mb_substr($str , 0, $maxLength); // operates on characters | |
| } else { | |
| $str = mb_strcut($str , 0, $maxLength); // operates on bytes | |
| } | |
| } | |
| return "'".str_replace( array("\\","'",chr(8),chr(0),chr(26),chr(27)), array("\\\\","''",'','','',''),$str)."',"; | |
| } | |
| /** | |
| * Protect a string to use in Sql like, so % and _ won't have a special value | |
| * | |
| * @param string $str | |
| * @return string | |
| */ | |
| public static function strlike($str) { | |
| return str_replace(array('%', '_'), array("\\%", "\\_"), $str); | |
| } | |
| /** | |
| * Protect with ` quotes a: column name to `column name` respecting . table.column to `table`.`column` | |
| * | |
| * @param string $fieldName | |
| * @return string | |
| */ | |
| public static function fieldit($fieldName) { | |
| $protected = []; | |
| $n = explode('.',$fieldName); | |
| foreach($n as $field) { | |
| $protected[]= '`'.str_replace('`', '', self::strim($field) ).'`'; | |
| } | |
| return implode('.', $protected); | |
| } | |
| /** | |
| * Generalize an Sql statement changing "parameters" to ? | |
| * | |
| * @param string $sql | |
| * @return string | |
| */ | |
| public static function sqlNormalized($sql) { | |
| static $sqlKeyWords =["select "," from "," join "," left "," right "," outer "," inner "," straight "," exists "," with "," on "," where "," and "," or "," not "," null "," in "," is "," having "," limit "," order by "," group by ","update ","insert ","delete "," as "]; | |
| static $sqlKeyWordsUpperCase = []; | |
| if(empty($sqlKeyWordsUpperCase)) { | |
| foreach($sqlKeyWords as $keyword) { | |
| $sqlKeyWordsUpperCase[] = strtoupper($keyword); | |
| } | |
| } | |
| $sql = str_replace(['=','>','>=','<','<=','<>',"\r","\n","\t"],[' = ',' > ',' >= ',' < ',' <= ',' <> ',' ',' ',' '], $sql); | |
| $sql = str_ireplace($sqlKeyWords,$sqlKeyWordsUpperCase,$sql); | |
| return preg_replace("/('([^']|'')*'|\b[-+]?[0-9]*\.?[0-9]+\b)/uS", '?', str_replace(["\\'". 'ยด', '"'], '', self::strim($sql)) ); | |
| } | |
| } |