Skip to content

Commit

Permalink
[9.x] Improvements to isAscii(), ascii() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
selcukcukur committed Apr 30, 2021
1 parent 6312193 commit 48a7897
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"symfony/routing": "^5.1.4",
"symfony/var-dumper": "^5.1.4",
"tijsverkoyen/css-to-inline-styles": "^2.2.2",
"vlucas/phpdotenv": "^5.2",
"voku/portable-ascii": "^1.4.8"
"vlucas/phpdotenv": "^5.2"
},
"replace": {
"illuminate/auth": "self.version",
Expand Down
56 changes: 51 additions & 5 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Ramsey\Uuid\Generator\CombGenerator;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactory;
use voku\helper\ASCII;

class Str
{
Expand Down Expand Up @@ -91,12 +90,35 @@ public static function afterLast($subject, $search)
* Transliterate a UTF-8 value to ASCII.
*
* @param string $value
* @param string $language
* @return string
*/
public static function ascii($value, $language = 'en')
public static function ascii($value)
{
return ASCII::to_ascii((string) $value, $language);
return static::encoding($value, 'UTF-8', 'ASCII');
}

/**
* Transliterate a ASCII value to UTF-8.
*
* @param string $value
* @return string
*/
public static function utf8($value)
{
return static::encoding($value, 'ASCII', 'UTF-8');
}

/**
* Transliterate a given value to target encoding.
*
* @param string $value
* @param string $to
* @param string|string[] $from
* @return string
*/
public static function encoding($value, $to, $from)
{
return mb_convert_encoding((string) $value, $to, $from);
}

/**
Expand Down Expand Up @@ -288,7 +310,31 @@ public static function is($pattern, $value)
*/
public static function isAscii($value)
{
return ASCII::is_ascii((string) $value);
return static::isEncoding($value, 'ASCII');
}

/**
* Determine if a given string is UTF-8.
*
* @param string $value
* @return bool
*/
public static function isUtf8($value)
{
return static::isEncoding($value, 'UTF-8');
}

/**
* Determine the encoding of a particular string.
*
* @param string $value
* @param string|string[]|null $encodings
* @param bool $strict
* @return bool
*/
public static function isEncoding($value, $encodings, $strict = false)
{
return mb_detect_encoding((string) $value, $encodings, $strict);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"illuminate/collections": "^8.0",
"illuminate/contracts": "^8.0",
"illuminate/macroable": "^8.0",
"nesbot/carbon": "^2.31",
"voku/portable-ascii": "^1.4.8"
"nesbot/carbon": "^2.31"
},
"conflict": {
"tightenco/collect": "<5.5.33"
Expand Down

0 comments on commit 48a7897

Please sign in to comment.