1 : <?php
2 :
3 : /**
4 : * Class for string manipulations
5 : */
6 : class CorujaStringManipulation
7 : {
8 :
9 : /**
10 : * Special string casting for boolean
11 : *
12 : * @param string $strText String to be turned into boolean
13 : * @return boolean Input casting
14 : * @throws InvalidArgumentException If $strText is not string
15 : * @example strToBool("false") // returns false
16 : *
17 : * @assert ("") == false
18 : * @assert ("false") == false
19 : * @assert ("FaLsE") == false
20 : * @assert ("0") == false
21 : *
22 : * @assert ("a0a") == true
23 : * @assert ("true") == true
24 : * @assert ("1") == true
25 : *
26 : * @assert (null) throws InvalidArgumentException
27 : * @assert (123) throws InvalidArgumentException
28 : * @assert (array()) throws InvalidArgumentException
29 : * @assert (new stdClass()) throws InvalidArgumentException
30 : * @assert (false) throws InvalidArgumentException
31 : *
32 : */
33 : public static function strToBool( $strText )
34 : {
35 12 : if(!is_string($strText))
36 12 : {
37 5 : throw new InvalidArgumentException("Invalid argument [ ". var_export($strText) ." ]. It should be string");
38 : }
39 :
40 7 : $strText = strtolower( $strText );
41 7 : if ( $strText === "false" || $strText === "" || $strText === "0" )
42 7 : {
43 4 : return( false );
44 : }
45 3 : return( true );
46 : }
47 :
48 : /**
49 : * Get number chars inside a string
50 : *
51 : * @param string $strText Text with numbers
52 : * @return int Numbers in the string
53 : * @throws InvalidArgumentException If $strText is not string
54 : * @example forceInt("a1b2c3d4") // returns 1234
55 : *
56 : * @assert ("a1b2c3d4") == 1234
57 : * @assert ("") == 0
58 : * @assert ("a0a") == 0
59 : * @assert ("001") == 1
60 : * @assert ("abc") == 0
61 : * @assert ("a-10") == 10
62 : *
63 : * @assert ("-10") == -10
64 : * @assert ("--10") == -10
65 : * @assert ("-a-b-1-0-") == -10
66 : *
67 : * @assert (null) throws InvalidArgumentException
68 : * @assert (123) throws InvalidArgumentException
69 : * @assert (array()) throws InvalidArgumentException
70 : * @assert (new stdClass()) throws InvalidArgumentException
71 : * @assert (false) throws InvalidArgumentException
72 : *
73 : */
74 : public static function forceInt( $strText )
75 : {
76 14 : if(!is_string($strText))
77 14 : {
78 5 : throw new InvalidArgumentException("Invalid argument [ ". var_export($strText) ." ]. It should be string");
79 : }
80 :
81 9 : $arrNum = Array( "0","1","2","3","4","5","6","7","8","9");
82 9 : $strResult = "";
83 9 : for( $i = 0; $i < strlen( $strText ); ++$i )
84 : {
85 8 : $charLetra = $strText[$i];
86 8 : if( $i == 0 && $charLetra == "-" )
87 8 : {
88 3 : $strResult .= $charLetra;
89 3 : }
90 8 : if( in_array($charLetra,$arrNum))
91 8 : {
92 7 : $strResult .= $charLetra;
93 7 : }
94 8 : }
95 9 : return $strResult += 0;
96 : }
97 :
98 : /**
99 : * Change string case standard
100 : *
101 : * @param string $strFieldName Name in camel case
102 : * @return string Name in underline separated case
103 : * @throws InvalidArgumentException If $strText is not string
104 : * @example
105 : * CorujaStringManipulation::caseTabUnderlineTab("nameOfTheParameter")
106 : * // returns "NAME_OF_THE_PARAMETER"
107 : *
108 : * @assert ("test") == "TEST"
109 : * @assert ("somethingCool") == "SOMETHING_COOL"
110 : * @assert ("") == ""
111 : * @assert ("1something2Cool3") == "1SOMETHING2_COOL3"
112 : * @assert ("111something2222Cool333") == "111SOMETHING2222_COOL333"
113 : *
114 : * @assert (null) throws InvalidArgumentException
115 : * @assert (123) throws InvalidArgumentException
116 : * @assert (array()) throws InvalidArgumentException
117 : * @assert (new stdClass()) throws InvalidArgumentException
118 : * @assert (false) throws InvalidArgumentException
119 : */
120 : public static function camelCaseToUnderlineCase( $strText )
121 : {
122 10 : if(!is_string($strText))
123 10 : {
124 5 : throw new InvalidArgumentException("Invalid argument [ ". var_export($strText) ." ]. It should be string");
125 : }
126 :
127 5 : $arrFind = Array( "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" ,
128 5 : "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "X" , "Z" , "W" , "Y" );
129 :
130 5 : $arrReplace = Array( "_A" , "_B" , "_C" , "_D" , "_E" , "_F" , "_G" , "_H" , "_I" , "_J" , "_K" , "_L" , "_M" ,
131 5 : "_N" , "_O" , "_P" , "_Q" , "_R" , "_S" , "_T" , "_U" , "_V" , "_X" , "_Z" , "_W" , "_Y" );
132 :
133 5 : if( strlen( $strText ) > 0 )
134 5 : {
135 4 : $strText[0] = strtolower($strText[0]);
136 4 : }
137 :
138 5 : return strtoupper( str_replace( $arrFind , $arrReplace , $strText ) );
139 : }
140 :
141 : public static function retab( $strText , $intDeeper )
142 : {
143 0 : $arrText = explode( "\n" , $strText );
144 0 : foreach( $arrText as $intKey => $strTextElement)
145 : {
146 0 : $arrText [ $intKey ] = trim( $strTextElement );
147 0 : }
148 0 : $strTab = "\n" .str_repeat( "\t" , $intDeeper );
149 0 : $strText = $strTab . implode( $strTab , $arrText ) . $strTab;
150 0 : return $strText;
151 : }
152 :
153 : }
154 :
155 : ?>
|