Skip to content

Commit

Permalink
Merge pull request #7 from atrapalo/UpdateComposer
Browse files Browse the repository at this point in the history
Remove static and tested it
  • Loading branch information
GuilleGF authored Mar 2, 2018
2 parents 2af576f + 766ee4d commit 5030bc9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/String/StringHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
*/
class StringHandler
{
/**
* @return StringHandler
*/
public static function getInstance()
{
return new self();
}

/**
* @param string $text
* @return string
*/
public static function removeAccentsByString(string $text): string
public function removeAccents(string $text): string
{
$charactersWithAccents = self::mbStringToArray(
$charactersWithAccents = $this->mbStringToArray(
"ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿºªç",
"UTF-8"
);

$charactersWithoutAccents = self::mbStringToArray(
$charactersWithoutAccents = $this->mbStringToArray(
"SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyyoac",
"UTF-8"
);
Expand All @@ -32,7 +40,7 @@ public static function removeAccentsByString(string $text): string
* @param string $encoding
* @return array
*/
private static function mbStringToArray(string $text, string $encoding): array
private function mbStringToArray(string $text, string $encoding): array
{
$result = [];
$length = mb_strlen($text);
Expand All @@ -50,7 +58,7 @@ private static function mbStringToArray(string $text, string $encoding): array
* @param string $url
* @return string
*/
public static function sanitizeUrl(string $url): string
public function sanitizeUrl(string $url): string
{
$encoding = mb_detect_encoding($url);
$url = mb_convert_case($url, MB_CASE_LOWER, $encoding);
Expand All @@ -60,7 +68,7 @@ public static function sanitizeUrl(string $url): string
'ñ', 'ç', '-', ' ', 'à', 'è', 'ì','ò', 'ù', 'á',
'é', 'í', 'ó', 'ú', '/', '´', '"', 'Á', 'É', 'Í',
'Ó', 'Ú', 'ä', 'ë', 'ï', 'ö','ü', 'â', 'ê', 'î',
'ô', 'û', '€', '$', '&', '!', '¡', '?', '¿',
'ô', 'û', '€', '', '$', '&', '!', '¡', '?', '¿',
'=', '(', ')', '%', '+',',', '.', ';', '@', ':',
'%', '*','º','ª','å','ø', '#', 'ß', 'æ', 'î', ',,'
];
Expand All @@ -69,13 +77,13 @@ public static function sanitizeUrl(string $url): string
'n', 'c', '-', '-', 'a', 'e', 'i', 'o', 'u','a',
'e', 'i', 'o', 'u', '-', '-', '', 'a', 'e', 'i',
'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e','i',
'o', 'u', 'euro', 'dollar', 'i', '', '', '', '',
'o', 'u', 'euro', 'euro', 'dollar', 'i', '', '', '', '',
'', '-', '-', '', '', '-', '-', '-','a', '-',
'-', '-','','','a','o', '-', 'ss', 'ae', 'i', '-'
];

$url = urlencode(str_ireplace($needle, $haystack, strtolower($url)));
$url = str_replace(['-+', '---', '--'], '-', $url);
$url = trim(preg_replace('/-+/', '-', $url), '-');

return $url;
}
Expand All @@ -84,27 +92,27 @@ public static function sanitizeUrl(string $url): string
* @param string $text
* @return string
*/
public static function sanitizeString(string $text): string
public function sanitizeString(string $text): string
{
return self::removeSpecialChars(self::removeAccentsByString($text));
return $this->removeSpecialChars($this->removeAccents($text));
}

/**
* @param string $text
* @return string
*/
public static function removeSpecialChars(string $text): string
public function removeSpecialChars(string $text): string
{
$text = preg_replace('/[^A-Za-z0-9]/', ' ', $text);

return self::removeExtraSpaces($text);
return $this->removeExtraSpaces($text);
}

/**
* @param string $text
* @return string
*/
public static function removeExtraSpaces(string $text): string
public function removeExtraSpaces(string $text): string
{
return trim(preg_replace('/\s+/', ' ', $text));
}
Expand Down
75 changes: 75 additions & 0 deletions tests/String/StringHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Atrapalo\PHPTools\Tests\String;

use Atrapalo\PHPTools\String\StringHandler;
use PHPUnit\Framework\TestCase;

/**
* Class StringHandlerTest
* @package Atrapalo\PHPTools\Tests\String
*/
class StringHandlerTest extends TestCase
{
/** @var StringHandler */
private $stringHandler;

protected function setUp()
{
$this->stringHandler = StringHandler::getInstance();
}

/**
* @test
*/
public function removeAccentsWithoutAccents()
{
$text = '*Text without accents and (special chars).';

$this->assertSame($text, $this->stringHandler->removeAccents($text));
}

/**
* @test
*/
public function removeAccentsWithAccents()
{
$text = '*Téxt without Àcceñts and ($pecial Çhars).';
$expectedText = '*Text without Accents and ($pecial Chars).';

$this->assertSame($expectedText, $this->stringHandler->removeAccents($text));
}

/**
* @test
*/
public function removeAccentsWithAllAccents()
{
$text = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿºªç';
$expectedText = 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyyoac';

$this->assertSame($expectedText, $this->stringHandler->removeAccents($text));
}

/**
* @test
*/
public function sanitizeString()
{
$text = '*Téxt without Àcceñts and ($pecial Çhars).';
$expectedText = 'Text without Accents and pecial Chars';

$this->assertSame($expectedText, $this->stringHandler->sanitizeString($text));
}

/**
* @test
*/
public function sanitizeUrl()
{
$urlText = '*Téxt € without__ Àcceñts and---+----($pecial Çhars).';
$expectedUrlText = 'text-euro-without__-accents-and-dollarpecial-chars';

$this->assertSame($expectedUrlText, $this->stringHandler->sanitizeUrl($urlText));
}
}

0 comments on commit 5030bc9

Please sign in to comment.