Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions src/Illuminate/Support/Casing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

namespace Illuminate\Support;

use Illuminate\Support\Traits\Macroable;

class Casing
{
use Macroable;

/**
* The cache of snake-cased words.
*
* @var array
*/
protected static $snakeCache = [];

/**
* The cache of camel-cased words.
*
* @var array
*/
protected static $camelCache = [];

/**
* The cache of studly-cased words.
*
* @var array
*/
protected static $studlyCache = [];

/**
* Convert the case of a string.
*
* @param string $string
* @param int $mode
* @param string|null $encoding
* @return string
*/
public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8')
{
return mb_convert_case($string, $mode, $encoding);
}

/**
* Convert the given string to lower-case.
*
* @param string $value
* @return string
*/
public static function lower($value)
{
return mb_strtolower($value, 'UTF-8');
}


/**
* Convert a value to camel case.
*
* @param string $value
* @return string
*/
public static function camel($value)
{
if (isset(static::$camelCache[$value])) {
return static::$camelCache[$value];
}

return static::$camelCache[$value] = lcfirst(static::studly($value));
}

/**
* Convert a string to kebab case.
*
* @param string $value
* @return string
*/
public static function kebab($value)
{
return static::snake($value, '-');
}

/**
* Convert the given string to upper-case.
*
* @param string $value
* @return string
*/
public static function upper($value)
{
return mb_strtoupper($value, 'UTF-8');
}

/**
* Convert the given string to proper case.
*
* @param string $value
* @return string
*/
public static function title($value)
{
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
}

/**
* Convert the given string to proper case for each word.
*
* @param string $value
* @return string
*/
public static function headline($value)
{
$parts = explode(' ', $value);

$parts = count($parts) > 1
? array_map([static::class, 'title'], $parts)
: array_map([static::class, 'title'], static::ucsplit(implode('_', $parts)));

$collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts));

return implode(' ', array_filter(explode('_', $collapsed)));
}

/**
* Make a string's first character lowercase.
*
* @param string $string
* @return string
*/
public static function lcfirst($string)
{
return static::lower(static::substr($string, 0, 1)).static::substr($string, 1);
}

/**
* Make a string's first character uppercase.
*
* @param string $string
* @return string
*/
public static function ucfirst($string)
{
return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
}

/**
* Convert a string to snake case.
*
* @param string $value
* @param string $delimiter
* @return string
*/
public static function snake($value, $delimiter = '_')
{
$key = $value;

if (isset(static::$snakeCache[$key][$delimiter])) {
return static::$snakeCache[$key][$delimiter];
}

if (! ctype_lower($value)) {
$value = preg_replace('/\s+/u', '', ucwords($value));

$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
}

return static::$snakeCache[$key][$delimiter] = $value;
}

/**
* Convert a value to studly caps case.
*
* @param string $value
* @return string
*/
public static function studly($value)
{
$key = $value;

if (isset(static::$studlyCache[$key])) {
return static::$studlyCache[$key];
}

$words = explode(' ', static::replace(['-', '_'], ' ', $value));

$studlyWords = array_map(fn ($word) => static::ucfirst($word), $words);

return static::$studlyCache[$key] = implode($studlyWords);
}

/**
* Remove all strings from the casing caches.
*
* @return void
*/
public static function flushCache()
{
static::$snakeCache = [];
static::$camelCache = [];
static::$studlyCache = [];
}
}
Loading