Skip to content

Commit

Permalink
Add urlsafeB64Decode and urlsafeB64Encode to StringUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbrown committed Mar 6, 2019
1 parent 04647ee commit 93fd2e7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-1.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
This changelog references the relevant changes done in 1.x versions.


## v1.1.1
* Add `urlsafeB64Decode` and `urlsafeB64Encode` to `StringUtils`.


## v1.1.0
* Marked these classes as deprecated as they will be removed in 2.x.
* BigNumber
Expand Down
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@
"psr-4": {
"Gdbots\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
}
}
}
49 changes: 43 additions & 6 deletions src/Common/Util/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ final class StringUtils
/**
* Private constructor. This class is not meant to be instantiated.
*/
private function __construct() {}
private function __construct()
{
}

/**
* Returns true if the provided string starts with a letter.
*
* @param string $str
*
* @return string
*/
public static function startsWithLetter($str)
Expand All @@ -26,6 +29,7 @@ public static function startsWithLetter($str)
* Converts a camelCase string to slug-i-fied style.
*
* @param string $camel
*
* @return string
*/
public static function toSlugFromCamel($camel)
Expand All @@ -37,6 +41,7 @@ public static function toSlugFromCamel($camel)
* Converts a slug-i-fied string to camelCase style.
*
* @param string $slug
*
* @return string
*/
public static function toCamelFromSlug($slug)
Expand All @@ -48,6 +53,7 @@ public static function toCamelFromSlug($slug)
* Converts a camelCase string to snake_case style.
*
* @param string $camel
*
* @return string
*/
public static function toSnakeFromCamel($camel)
Expand All @@ -59,6 +65,7 @@ public static function toSnakeFromCamel($camel)
* Converts a snake_case string to camelCase style.
*
* @param string $snake
*
* @return string
*/
public static function toCamelFromSnake($snake)
Expand All @@ -70,6 +77,7 @@ public static function toCamelFromSnake($snake)
* Converts a slug-case string to snake_case style.
*
* @param string $slug
*
* @return string
*/
public static function toSnakeFromSlug($slug)
Expand All @@ -81,6 +89,7 @@ public static function toSnakeFromSlug($slug)
* Converts a snake_case string to slug-case style.
*
* @param string $snake
*
* @return string
*/
public static function toSlugFromSnake($snake)
Expand All @@ -93,6 +102,7 @@ public static function toSlugFromSnake($snake)
* xml attributes and nodes without the use of CDATA.
*
* @param string $str
*
* @return string
*/
public static function xmlEscape($str)
Expand Down Expand Up @@ -131,7 +141,7 @@ public static function xmlEscape($str)
$pos += 4;
} else if (($asciiPos >= 224) && ($asciiPos <= 239)) {
// 3 chars representing one unicode character
$thisLetter = substr ($str, $pos, 3);
$thisLetter = substr($str, $pos, 3);
$pos += 3;
} else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
// 2 chars representing one unicode character
Expand All @@ -144,7 +154,7 @@ public static function xmlEscape($str)
}

// process the string representing the letter to a unicode entity
$thisLen = strlen ($thisLetter);
$thisLen = strlen($thisLetter);
$thisPos = 0;
$decimalCode = 0;
while ($thisPos < $thisLen) {
Expand All @@ -161,9 +171,9 @@ public static function xmlEscape($str)
}

if ($thisLen == 1) {
$encodedLetter = '&#'. str_pad($decimalCode, 3, '0', STR_PAD_LEFT) . ';';
$encodedLetter = '&#' . str_pad($decimalCode, 3, '0', STR_PAD_LEFT) . ';';
} else {
$encodedLetter = '&#'. str_pad($decimalCode, 5, '0', STR_PAD_LEFT) . ';';
$encodedLetter = '&#' . str_pad($decimalCode, 5, '0', STR_PAD_LEFT) . ';';
}

$c = $decimalCode;
Expand Down Expand Up @@ -200,6 +210,7 @@ public static function xmlEscape($str)

/**
* @param mixed $var
*
* @return string
*/
public static function varToString($var)
Expand All @@ -209,7 +220,7 @@ public static function varToString($var)
}

if (is_array($var)) {
$a = array();
$a = [];
foreach ($var as $k => $v) {
$a[] = sprintf('%s => %s', $k, self::varToString($v));
}
Expand All @@ -235,4 +246,30 @@ public static function varToString($var)

return (string)$var;
}

/**
* @param string $input
*
* @return string
*/
public static function urlsafeB64Decode($input)
{
$remainder = strlen($input) % 4;
if ($remainder) {
$padlen = 4 - $remainder;
$input .= str_repeat('=', $padlen);
}

return base64_decode(strtr($input, '-_', '+/'));
}

/**
* @param string $input
*
* @return string
*/
public static function urlsafeB64Encode($input)
{
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
}
}

0 comments on commit 93fd2e7

Please sign in to comment.