Skip to content

Commit 7cad734

Browse files
authored
feat: add more morphers (#5)
- maskBrReal - ltrim - rtrim - toUpperFirst - replaceRegex - reverse - limit - normalize
1 parent 080141a commit 7cad734

File tree

8 files changed

+423
-9
lines changed

8 files changed

+423
-9
lines changed

docs/docs/methods/a-lot-of-manipulators.md

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@ $string = SM::make($string)
3434
echo $string; // Hello world
3535
```
3636

37+
## limit
38+
Limit the string length.
39+
40+
If $end is setted, it will be added at the end of string.
41+
42+
If $length is less than the length of $end, $end will be the new string.
43+
44+
```php
45+
$string = 'The quick brown fox jumps over the lazy dog';
46+
$string = SM::limit($string, 30);
47+
// or
48+
$string = SM::make($string)
49+
->limit(30);
50+
echo $string; // The quick brown fox jumps over
51+
52+
//if you use $end
53+
$string = 'The quick brown fox jumps over the lazy dog';
54+
$string = SM::limit($string, 30, '[...]');
55+
// or
56+
$string = SM::make($string)
57+
->limit(30, '[...]');
58+
echo $string; // The quick brown fox jumps[...]
59+
```
60+
61+
## normalize
62+
Remove all accents and special characters.
63+
64+
Only alphanumeric and spaces are preserved
65+
66+
```php
67+
$string = 'âäàåçêëèïîìÆôöòûùÿ';
68+
$string = SM::normalize($string);
69+
// or
70+
$string = SM::make($string)
71+
->normalize();
72+
echo $string; // aaaaceeeiiiooouuy
73+
```
74+
3775
## onlyAlpha
3876
Only keep letters
3977

@@ -75,21 +113,44 @@ Replace all occurrences of a string.
75113

76114
```php
77115
$string = 'The quick brown fox jumps';
78-
$string = SM::sub($string, 'fox', 'dog');
116+
$string = SM::replace($string, 'fox', 'dog');
79117
// or
80118
$string = SM::make($string)
81-
->sub('fox', 'dog');
119+
->replace('fox', 'dog');
82120
echo $string; // The quick brown dog jumps
83121

84122
// case insensitive:
85123
$string = 'The quick brown Fox jumps';
86-
$string = SM::sub($string, 'fox', 'Dog', false);
124+
$string = SM::replace($string, 'fox', 'Dog', false);
87125
// or
88126
$string = SM::make($string)
89-
->sub('fox', 'Dog', false);
127+
->replace('fox', 'Dog', false);
90128
echo $string; // The quick brown Dog jumps
91129
```
92130

131+
## replaceRegex
132+
Replace all occurrences of a regex.
133+
134+
```php
135+
$string = 'The quick brown fox jumps';
136+
$string = SM::replaceRegex($string, '/\s+/', ' ');
137+
// or
138+
$string = SM::make($string)
139+
->replaceRegex('/\s+/', ' ');
140+
echo $string; // The quick brown fox jumps
141+
```
142+
143+
## reverse
144+
Reverse the string
145+
146+
```php
147+
$string = 'spmuj xof nworb kciuq ehT';
148+
$string = SM::reverse($string);
149+
// or
150+
$string = SM::make($string)
151+
->reverse();
152+
echo $string; // The quick brown fox jumps
153+
```
93154

94155
## toBase64
95156
Encode to base64
@@ -127,6 +188,18 @@ $string = SM::make($string)
127188
echo $string; // HELLO WORLD
128189
```
129190

191+
## toUpperFirst
192+
Morph to upper case the first letter of string.
193+
194+
```php
195+
$string = 'hello world';
196+
$string = SM::toUpperFirst($string);
197+
// or
198+
$string = SM::make($string)
199+
->toUpperFirst();
200+
echo $string; // Hello world
201+
```
202+
130203
## trim
131204
Remove all spaces from the start and end.
132205

@@ -144,6 +217,40 @@ $string = SM::make($string)
144217
->trim(" \n\r\t\v\0");
145218
```
146219

220+
## ltrim
221+
Remove all spaces from the start and end.
222+
223+
```php
224+
$string = ' Hello world ';
225+
$string = SM::ltrim($string);
226+
// or
227+
$string = SM::make($string)
228+
->ltrim();
229+
echo $string; // Hello world
230+
231+
// you can set what caracters you can ltrim
232+
$string = SM::ltrim($string, " \n\r\t\v\0");
233+
$string = SM::make($string)
234+
->ltrim(" \n\r\t\v\0");
235+
```
236+
237+
## rtrim
238+
Remove all spaces from the start and end.
239+
240+
```php
241+
$string = ' Hello world ';
242+
$string = SM::rtrim($string);
243+
// or
244+
$string = SM::make($string)
245+
->rtrim();
246+
echo $string; // Hello world
247+
248+
// you can set what caracters you can rtrim
249+
$string = SM::rtrim($string, " \n\r\t\v\0");
250+
$string = SM::make($string)
251+
->rtrim(" \n\r\t\v\0");
252+
```
253+
147254
## withoutSpaces
148255
Remove all spaces
149256

docs/docs/methods/brazilian-maskers.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,58 @@ nav_order: 3
1414
Apply the Brazilian CEP mask
1515

1616
```php
17+
$string = '44555666';
1718
$string = SM::maskBrCep($string);
19+
// or
1820
$string = SM::withoutSpaces($string)
1921
->maskBrCep();
22+
echo $string // 44.555-666
2023
```
2124

2225
## maskBrCpf
2326
Apply the Brazilian CEP mask
2427

2528
```php
29+
$string = '11122233344';
2630
$string = SM::maskBrCpf($string);
31+
// or
2732
$string = SM::withoutSpaces($string)
2833
->maskBrCpf();
34+
echo $string // 111.222.333-44
2935
```
3036

3137
## maskBrCnpj
3238
Apply the Brazilian CNPJ mask
3339

3440
```php
41+
$string = '11222333444455';
3542
$string = SM::maskBrCnpj($string);
43+
// or
3644
$string = SM::withoutSpaces($string)
3745
->maskBrCnpj();
46+
echo $string // 11.222.333/4444-55
3847
```
3948

4049
## maskBrPhone
4150
Apply the Brazilian Phone mask
4251

4352
```php
53+
$string = '11988887777';
4454
$string = SM::maskBrPhone($string);
55+
// or
4556
$string = SM::withoutSpaces($string)
4657
->maskBrPhone();
58+
echo $string // (11) 98888-7777
59+
```
60+
61+
## maskBrReal
62+
Apply the Brazilian Phone mask
63+
64+
```php
65+
$string = '1234864.20'; // or float or int
66+
$string = SM::maskBrReal($string);
67+
// or
68+
$string = SM::withoutSpaces($string)
69+
->maskBrReal();
70+
echo $string // R$ 1.234.864,20
4771
```

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
55
colors="true"
6+
displayDetailsOnTestsThatTriggerDeprecations="true"
67
processIsolation="false"
78
stopOnError="false"
89
stopOnFailure="false"

src/Manipulators/Manipulator.php

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,57 @@ public function fromBase64()
2727
return $this;
2828
}
2929

30+
/**
31+
* Limit the string length.
32+
* If $end is setted, it will be added at the end.
33+
* If $length is less than the length of $end, $end will be the new string.
34+
* @param integer $length The maximum length.
35+
* @param string|null $end The string to be added at the end.
36+
* @return $this
37+
*/
38+
public function limit(int $length, string|null $end = null)
39+
{
40+
if (strlen($this->string) <= $length) {
41+
return $this;
42+
}
43+
$endLength = !is_null($end) ? strlen($end) : 0;
44+
$finalLength = ($length - $endLength < 0) ? 0 : $length - $endLength;
45+
$this->string = $endLength > 0 ?
46+
substr($this->string, 0, $finalLength) . $end :
47+
substr($this->string, 0, $finalLength);
48+
49+
return $this;
50+
}
51+
52+
/**
53+
* Remove all accents and special characters.
54+
* Only alphanumeric and spaces are preserved
55+
* @return $this
56+
*/
57+
public function normalize()
58+
{
59+
$transliteration = [
60+
'á' => 'a', 'à' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a',
61+
'ç' => 'c',
62+
'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
63+
'í' => 'i', 'ì' => 'i', 'î' => 'i', 'ï' => 'i',
64+
'ó' => 'o', 'ò' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ð' => 'o',
65+
'ú' => 'u', 'ù' => 'u', 'û' => 'u', 'ü' => 'u',
66+
'ñ' => 'n', 'š' => 's', 'ý' => 'y', 'ÿ' => 'y',
67+
'Á' => 'A', 'À' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A',
68+
'Ç' => 'C',
69+
'É' => 'E', 'È' => 'E', 'Ê' => 'E', 'Ë' => 'E',
70+
'Í' => 'I', 'Ì' => 'I', 'Î' => 'I', 'Ï' => 'I',
71+
'Ó' => 'O', 'Ò' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O',
72+
'Ú' => 'U', 'Ù' => 'U', 'Û' => 'U', 'Ü' => 'U',
73+
'Ñ' => 'N', 'Š' => 'S', 'Ý' => 'Y', 'Ÿ' => 'Y',
74+
];
75+
$input = strtr($this->string, $transliteration);
76+
$this->string = preg_replace('/[^a-zA-Z0-9\s]/', '', $input);
77+
78+
return $this;
79+
}
80+
3081
/**
3182
* Remove all characters except letters.
3283
* @return $this
@@ -67,6 +118,32 @@ public function replace(array|string $needle, array|string $replace, bool $caseS
67118
return $this;
68119
}
69120

121+
/**
122+
* Replace all occurrences of a regex.
123+
* @see preg_replace()
124+
* @param array|string $needleRegex The search string.
125+
* @param array|string $replace The replacement string.
126+
* @return $this
127+
*/
128+
public function replaceRegex(array|string $needleRegex, array|string $replace)
129+
{
130+
$this->string = preg_replace($needleRegex, $replace, $this->string);
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* Reverse the string.
137+
* @see strrev()
138+
* @return $this
139+
*/
140+
public function reverse()
141+
{
142+
$this->string = strrev($this->string);
143+
144+
return $this;
145+
}
146+
70147
/**
71148
* Slice the string.
72149
* @see substr()
@@ -117,6 +194,18 @@ public function toUpper()
117194
return $this;
118195
}
119196

197+
/**
198+
* Morph the first caracter to upper case.
199+
* @see ucfirst()
200+
* @return $this
201+
*/
202+
public function toUpperFirst()
203+
{
204+
$this->string = ucfirst($this->string);
205+
206+
return $this;
207+
}
208+
120209
/**
121210
* Remove all spaces from the start and end.
122211
* @see trim()
@@ -125,7 +214,39 @@ public function toUpper()
125214
*/
126215
public function trim(string $characters = " \n\r\t\v\0")
127216
{
128-
$this->string = trim($this->string, $characters);
217+
$this->string = $characters == null ?
218+
trim($this->string) :
219+
trim($this->string, $characters);
220+
221+
return $this;
222+
}
223+
224+
/**
225+
* Remove all spaces from the left.
226+
* @see ltrim()
227+
* @param string $characters The characters to be removed.
228+
* @return $this
229+
*/
230+
public function ltrim(string $characters = " \n\r\t\v\0")
231+
{
232+
$this->string = $characters == null ?
233+
ltrim($this->string) :
234+
ltrim($this->string, $characters);
235+
236+
return $this;
237+
}
238+
239+
/**
240+
* Remove all spaces from the right.
241+
* @see rtrim()
242+
* @param string $characters The characters to be removed.
243+
* @return $this
244+
*/
245+
public function rtrim(string $characters = " \n\r\t\v\0")
246+
{
247+
$this->string = $characters == null ?
248+
rtrim($this->string) :
249+
rtrim($this->string, $characters);
129250

130251
return $this;
131252
}

src/Maskers/BrazilianMasker.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,24 @@ public function maskBrPhone()
4848

4949
return $this;
5050
}
51+
52+
/**
53+
* Mask to a real currency.
54+
* @return $this
55+
*/
56+
public function maskBrReal()
57+
{
58+
if (empty($this->string)) {
59+
return $this;
60+
}
61+
$aux = $this->string;
62+
if (strpos($aux, ',') !== false && strpos($aux, '.') === false) {
63+
$aux = str_replace(',', '.', $aux);
64+
}
65+
$aux = floatval($aux);
66+
$aux = number_format($aux, 2, ',', '.');
67+
$this->string = 'R$ ' . $aux;
68+
69+
return $this;
70+
}
5171
}

0 commit comments

Comments
 (0)