Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
13.16% covered (danger)
13.16%
5 / 38
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
IacStringer
13.16% covered (danger)
13.16%
5 / 38
0.00% covered (danger)
0.00%
0 / 6
369.45
0.00% covered (danger)
0.00%
0 / 1
 strim
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 strit
42.86% covered (danger)
42.86%
3 / 7
0.00% covered (danger)
0.00%
0 / 1
6.99
 stritc
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 exportTrimed
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 sqlNormalized
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 constant2name
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * IacStringer
4 * Helper string functions
5 *
6 * @package sql
7 * @author Informatica Asociada
8 * @copyright 2015
9 * @version 1.0.0
10 */
11namespace Iac\inc\sql;
12
13
14/**
15 * IacStringer
16 * String functions
17 *
18 * @package sql
19 * @author Informatica Asociada
20 * @copyright 2015
21 * @version 1.0.0
22 */
23trait IacStringer {
24
25    /**
26     * iacStringer::strim()
27     * Trims and removes double space, tabs leaving single spaces
28     *
29     *
30     * @param string $s
31     * @return string
32     */
33    public function strim($s) {
34        if($s === null || $s === false)
35            return '';
36        return preg_replace('/\s+/', ' ',  trim($s) );
37    }
38
39    /**
40     * iacStringer::strit()
41     *
42     *
43     * @param string $str
44     * @param integer $maxLength
45     * @param bool $maxLengthInCharacters
46     * @return string
47     */
48    public function strit($str, $maxLength=0, $maxLengthInCharacters=true) {
49        if($str === null) {
50            return 'null';
51        }
52        if($maxLength) {
53            if($maxLengthInCharacters) {
54                $str = mb_substr($str , 0, $maxLength);  // operates on characters
55            } else {
56                $str = mb_strcut($str , 0, $maxLength); // operates on bytes
57            }
58        }
59        return "'".str_replace( array("\\",chr(8),chr(0),chr(26),chr(27)), array("\\\\",'','','',''),str_replace("'","''", $str))."'";
60    }
61
62    /**
63     * iacStringer::stritc()
64     *
65     *
66     * @param string $str
67     * @param integer $maxLength
68     * @param bool $maxLengthInCharacters
69     * @return string
70     */
71    public function stritc($str, $maxLength=0, $maxLengthInCharacters=true) {
72        if($str === null) {
73            return 'null,';
74        }
75        if($maxLength)
76            if($maxLengthInCharacters)
77                $str = mb_substr($str , 0, $maxLength); // operates on characters
78            else
79                $str = mb_strcut($str , 0, $maxLength); // operates on bytes
80        return "'".str_replace( array("\\",chr(8),chr(0),chr(26),chr(27)), array("\\\\",'','','',''),str_replace("'","''", $str))."',";
81    }
82
83    /**
84     * exportTrimed()
85     * Returns var_export($obj) as a one liner, no CR/LF
86     *
87     * @param mixed $obj
88     * @return string returns var_export($obj) as a one liner, no CR/LF
89     */
90    public function exportTrimed($obj) {
91        if($obj === null)
92            return "null";
93        if($obj === '')
94            return '""';
95        if(is_numeric($obj))
96            return $obj;
97        return $this->strim(str_replace(array("\r","\n"),"", var_export($obj,true) ));
98    }
99
100    /**
101     * sqlNormalized()
102     *
103     * @param string $sql
104     * @return string
105     */
106    public function sqlNormalized($sql) {
107        $sqlLimit = preg_replace(
108            "/LIMIT\\s*(\\d+?\\s*\\,\\s*\\d+?|\\d+?)/mUu",'LIMIT ?', str_replace(
109                    array("''", " =", "= ", "> ", " >","< ", " <"),
110                    array("",    "=", "=",  ">",   ">","<",   "<"),
111                    $this->strim($sql)
112                )
113        );
114        return preg_replace("/([=\\>\\<,\\(])\\s*(\\'.*\\'|\\-?\\d+?[\\.\\d]*?)/mUu",'${1}?',$sqlLimit);
115    }
116
117    /**
118     * constant2name()
119     *
120     * @param string $group
121     * @param mixed $value string or number
122     * @param string $tag
123     * @return string
124     */
125    public function constant2name($group,$value,$tag='ERR') {
126        static $constats;
127        if(!isset($constants)) {
128            $constants = get_defined_constants(true);
129        }
130        if(!isset($constants[$group])) {
131            return "Unknown ($value)";
132        }
133        foreach($constants[$group] as $c=>$n) {
134            if( (empty($tag) || stripos($c,$tag)!==FALSE) &&  $n===$value) {
135                return "$c ($value)";
136            }
137        }
138        return "Unknown ($value)";
139    }
140
141}