|
|
@@ -576,69 +576,95 @@ class Inflector
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the given lower_case_and_underscored_word as a CamelCased word.
|
|
|
+ * Returns the input lower_case_delimited_string as a CamelCasedString.
|
|
|
*
|
|
|
- * @param string $lowerCaseAndUnderscoredWord Word to camelize
|
|
|
- * @return string Camelized word. LikeThis.
|
|
|
+ * @param string $string String to camelize
|
|
|
+ * @param string $delimiter the delimiter in the input string
|
|
|
+ * @return string CamelizedStringLikeThis.
|
|
|
* @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
|
|
|
*/
|
|
|
- public static function camelize($lowerCaseAndUnderscoredWord)
|
|
|
+ public static function camelize($string, $delimiter = '_')
|
|
|
{
|
|
|
- if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
|
|
|
- $result = str_replace(' ', '', static::humanize($lowerCaseAndUnderscoredWord));
|
|
|
- static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
|
|
|
+ $cacheKey = __FUNCTION__ . $delimiter;
|
|
|
+
|
|
|
+ $result = static::_cache($cacheKey, $string);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
+ $result = str_replace(' ', '', static::humanize($string, $delimiter));
|
|
|
+ static::_cache(__FUNCTION__, $string, $result);
|
|
|
}
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the given camelCasedWord as an underscored_word.
|
|
|
+ * Returns the input CamelCasedString as an underscored_string.
|
|
|
*
|
|
|
- * @param string $camelCasedWord Camel-cased word to be "underscorized"
|
|
|
- * @return string Underscore-syntaxed version of the $camelCasedWord
|
|
|
+ * Also replaces dashes with underscores
|
|
|
+ *
|
|
|
+ * @param string $string CamelCasedString to be "underscorized"
|
|
|
+ * @return string underscore_version of the input string
|
|
|
* @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
|
|
|
*/
|
|
|
- public static function underscore($camelCasedWord)
|
|
|
+ public static function underscore($string)
|
|
|
{
|
|
|
- if (!($result = static::_cache(__FUNCTION__, $camelCasedWord))) {
|
|
|
- $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
|
|
|
- static::_cache(__FUNCTION__, $camelCasedWord, $result);
|
|
|
- }
|
|
|
- return $result;
|
|
|
+ return static::delimit(str_replace('-', '_', $string), '_');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the given CamelCasedWordGroup as an dashed-word-group.
|
|
|
+ * Returns the input CamelCasedString as an dashed-string.
|
|
|
+ *
|
|
|
+ * Also replaces underscores with dashes
|
|
|
*
|
|
|
- * @param string $wordGroup The string to dasherize.
|
|
|
- * @return string Dashed version of the word group
|
|
|
+ * @param string $string The string to dasherize.
|
|
|
+ * @return string Dashed version of the input string
|
|
|
*/
|
|
|
- public static function dasherize($wordGroup)
|
|
|
+ public static function dasherize($string)
|
|
|
{
|
|
|
- $result = static::_cache(__FUNCTION__, $wordGroup);
|
|
|
- if ($result !== false) {
|
|
|
- return $result;
|
|
|
- }
|
|
|
-
|
|
|
- $result = str_replace('_', '-', static::underscore($wordGroup));
|
|
|
- static::_cache(__FUNCTION__, $wordGroup, $result);
|
|
|
- return $result;
|
|
|
+ return static::delimit(str_replace('_', '-', $string), '-');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the given underscored_word_group as a Human Readable Word Group.
|
|
|
+ * Returns the input lower_case_delimited_string as 'A Human Readable String'.
|
|
|
* (Underscores are replaced by spaces and capitalized following words.)
|
|
|
*
|
|
|
- * @param string $lowerCaseAndUnderscoredWord String to be made more readable
|
|
|
+ * @param string $string String to be humanized
|
|
|
+ * @param string $delimiter the character to replace with a space
|
|
|
* @return string Human-readable string
|
|
|
* @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms
|
|
|
*/
|
|
|
- public static function humanize($lowerCaseAndUnderscoredWord)
|
|
|
+ public static function humanize($string, $delimiter = '_')
|
|
|
{
|
|
|
- if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
|
|
|
- $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
|
|
|
- static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
|
|
|
+ $cacheKey = __FUNCTION__ . $delimiter;
|
|
|
+
|
|
|
+ $result = static::_cache($cacheKey, $string);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
+ $result = ucwords(str_replace($delimiter, ' ', $string));
|
|
|
+ static::_cache($cacheKey, $string, $result);
|
|
|
}
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Expects a CamelCasedInputString, and produces a lower_case_delimited_string
|
|
|
+ *
|
|
|
+ * @param string $string String to delimit
|
|
|
+ * @param string $delimiter the character to use as a delimiter
|
|
|
+ * @return string delimited string
|
|
|
+ */
|
|
|
+ public static function delimit($string, $delimiter = '_')
|
|
|
+ {
|
|
|
+ $cacheKey = __FUNCTION__ . $delimiter;
|
|
|
+
|
|
|
+ $result = static::_cache($cacheKey, $string);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
+ $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', $delimiter . '\\1', $string));
|
|
|
+ static::_cache($cacheKey, $string, $result);
|
|
|
+ }
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
@@ -651,10 +677,13 @@ class Inflector
|
|
|
*/
|
|
|
public static function tableize($className)
|
|
|
{
|
|
|
- if (!($result = static::_cache(__FUNCTION__, $className))) {
|
|
|
+ $result = static::_cache(__FUNCTION__, $className);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
$result = static::pluralize(static::underscore($className));
|
|
|
static::_cache(__FUNCTION__, $className, $result);
|
|
|
}
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
@@ -667,10 +696,13 @@ class Inflector
|
|
|
*/
|
|
|
public static function classify($tableName)
|
|
|
{
|
|
|
- if (!($result = static::_cache(__FUNCTION__, $tableName))) {
|
|
|
+ $result = static::_cache(__FUNCTION__, $tableName);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
$result = static::camelize(static::singularize($tableName));
|
|
|
static::_cache(__FUNCTION__, $tableName, $result);
|
|
|
}
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
@@ -683,12 +715,15 @@ class Inflector
|
|
|
*/
|
|
|
public static function variable($string)
|
|
|
{
|
|
|
- if (!($result = static::_cache(__FUNCTION__, $string))) {
|
|
|
+ $result = static::_cache(__FUNCTION__, $string);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
$camelized = static::camelize(static::underscore($string));
|
|
|
$replace = strtolower(substr($camelized, 0, 1));
|
|
|
$result = preg_replace('/\\w/', $replace, $camelized, 1);
|
|
|
static::_cache(__FUNCTION__, $string, $result);
|
|
|
}
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|