From 2b33bf44d2913c2d89f674086315d02797ecb6f9 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 12 Apr 2014 16:22:30 +0200 Subject: [PATCH] Text case conversion moved to gist --- .gitmodules | 3 ++ lib/TextCaseConverter | 1 + src/Enum.php | 7 ++++ src/Helpers/PrivilegesHelper.php | 15 ++++++-- src/Helpers/TextHelper.php | 57 +++---------------------------- src/Models/AbstractCrudModel.php | 12 +++++-- src/Views/post-edit.phtml | 2 +- src/Views/post-list-wrapper.phtml | 4 +-- src/Views/post-small.phtml | 7 ++-- src/Views/post-upload.phtml | 2 +- src/Views/post-view.phtml | 4 +-- src/Views/top-navigation.phtml | 6 ++-- src/Views/user-edit.phtml | 2 +- src/Views/user-settings.phtml | 2 +- src/Views/user-view.phtml | 2 +- src/core.php | 1 + 16 files changed, 55 insertions(+), 72 deletions(-) create mode 160000 lib/TextCaseConverter diff --git a/.gitmodules b/.gitmodules index 2a7690ae..bc01ee6e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "lib/chibi-sql"] path = lib/chibi-sql url = https://github.com/rr-/chibi-sql.git +[submodule "lib/TextCaseConverter"] + path = lib/TextCaseConverter + url = https://gist.github.com/rr-/10522533.git diff --git a/lib/TextCaseConverter b/lib/TextCaseConverter new file mode 160000 index 00000000..eabd7f5f --- /dev/null +++ b/lib/TextCaseConverter @@ -0,0 +1 @@ +Subproject commit eabd7f5ff2a1d1eb8a683e6bc658d613a56980b0 diff --git a/src/Enum.php b/src/Enum.php index e4bcb8ca..ad74989a 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -8,6 +8,13 @@ class Enum return array_search($constant, $constants); } + public static function toDisplayString($constant) + { + TextCaseConverter::convert(static::toString($constant), + TextCaseConverter::SNAKE_CASE, + TextCaseConverter::BLANK_CASE); + } + public static function getAll() { $cls = new ReflectionClass(get_called_class()); diff --git a/src/Helpers/PrivilegesHelper.php b/src/Helpers/PrivilegesHelper.php index 2e2a7654..5252b736 100644 --- a/src/Helpers/PrivilegesHelper.php +++ b/src/Helpers/PrivilegesHelper.php @@ -11,8 +11,14 @@ class PrivilegesHelper if (strpos($key, '.') === false) $key .= '.'; list ($privilegeName, $subPrivilegeName) = explode('.', $key); - $privilegeName = TextHelper::camelCaseToKebabCase($privilegeName); - $subPrivilegeName = TextHelper::camelCaseToKebabCase($subPrivilegeName); + + $privilegeName = TextCaseConverter::convert($privilegeName, + TextCaseConverter::CAMEL_CASE, + TextCaseConverter::SPINAL_CASE); + $subPrivilegeName = TextCaseConverter::convert($subPrivilegeName, + TextCaseConverter::CAMEL_CASE, + TextCaseConverter::SPINAL_CASE); + $key = rtrim($privilegeName . '.' . $subPrivilegeName, '.'); $minAccessRank = TextHelper::resolveConstant($minAccessRankName, 'AccessRank'); @@ -34,7 +40,10 @@ class PrivilegesHelper $user = \Chibi\Registry::getContext()->user; $minAccessRank = AccessRank::Admin; - $key = TextHelper::camelCaseToKebabCase(Privilege::toString($privilege)); + $key = TextCaseConverter::convert(Privilege::toString($privilege), + TextCaseConverter::CAMEL_CASE, + TextCaseConverter::SPINAL_CASE); + if (isset(self::$privileges[$key])) { $minAccessRank = self::$privileges[$key]; diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index 30b212e1..f798ea0e 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -17,61 +17,12 @@ class TextHelper return $text; } - //todo: convert to enum and make one method - public static function snakeCaseToCamelCase($string, $lower = false) - { - $string = explode('_', $string); - $string = array_map('trim', $string); - $string = array_map('ucfirst', $string); - $string = join('', $string); - if ($lower) - $string = lcfirst($string); - return $string; - } - - public static function kebabCaseToCamelCase($string) - { - $string = explode('-', $string); - $string = array_map('trim', $string); - $string = array_map('ucfirst', $string); - $string = join('', $string); - return $string; - } - - public static function camelCaseToKebabCase($string) - { - $string = preg_replace_callback('/[A-Z]/', function($x) - { - return '-' . strtolower($x[0]); - }, $string); - $string = trim($string, '-'); - return $string; - } - - public static function camelCaseToHumanCase($string, $ucfirst = false) - { - $string = preg_replace_callback('/[A-Z]/', function($x) - { - return ' ' . strtolower($x[0]); - }, $string); - $string = trim($string); - if ($ucfirst) - $string = ucfirst($string); - return $string; - } - - public static function humanCaseToKebabCase($string) - { - $string = trim($string); - $string = str_replace(' ', '-', $string); - $string = strtolower($string); - return $string; - } - public static function resolveConstant($constantName, $className = null) { - $constantName = self::kebabCaseToCamelCase($constantName); - //convert from kebab-case to CamelCase + $constantName = TextCaseConverter::convert($constantName, + TextCaseConverter::SPINAL_CASE, + TextCaseConverter::CAMEL_CASE); + if ($className !== null) { $constantName = $className . '::' . $constantName; diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index 1e8d779f..f6c6b503 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -75,7 +75,10 @@ abstract class AbstractCrudModel implements IModel $entity = self::spawn(); foreach ($row as $key => $val) { - $key = TextHelper::snakeCaseToCamelCase($key, true); + $key = TextCaseConverter::convert($key, + TextCaseConverter::SNAKE_CASE, + TextCaseConverter::LOWER_CAMEL_CASE); + $entity->$key = $val; } return $entity; @@ -93,7 +96,12 @@ abstract class AbstractCrudModel implements IModel if (isset($keyCache[$key])) $key = $keyCache[$key]; else - $key = $keyCache[$key] = TextHelper::snakeCaseToCamelCase($key, true); + { + $key = $keyCache[$key] = TextCaseConverter::convert($key, + TextCaseConverter::SNAKE_CASE, + TextCaseConverter::LOWER_CAMEL_CASE); + } + $entity->$key = $val; } $entities[$i] = $entity; diff --git a/src/Views/post-edit.phtml b/src/Views/post-edit.phtml index 4d2d2ab7..f24af211 100644 --- a/src/Views/post-edit.phtml +++ b/src/Views/post-edit.phtml @@ -7,7 +7,7 @@ diff --git a/src/Views/post-list-wrapper.phtml b/src/Views/post-list-wrapper.phtml index 05012c5d..45e5fdad 100644 --- a/src/Views/post-list-wrapper.phtml +++ b/src/Views/post-list-wrapper.phtml @@ -44,9 +44,9 @@ if (PrivilegesHelper::confirm(Privilege::MassTag)) $tab): ?> -
  • +
  • -
  • +
  • diff --git a/src/Views/post-small.phtml b/src/Views/post-small.phtml index d52faf15..93166b47 100644 --- a/src/Views/post-small.phtml +++ b/src/Views/post-small.phtml @@ -4,8 +4,11 @@ CustomAssetViewDecorator::addStylesheet('post-small.css'); $classNames = [ 'post', - 'post-type-' .(TextHelper::resolveMimeType($this->context->post->mimeType) ?: - TextHelper::camelCaseToHumanCase(PostType::toString($this->context->post->type))), + 'post-type-' . TextCaseConverter::convert( + TextHelper::resolveMimeType($this->context->post->mimeType) + ?: PostType::toString($this->context->post->type), + TextCaseConverter::CAMEL_CASE, + TextCaseConverter::SPINAL_CASE), ]; $masstag = (isset($this->context->source) diff --git a/src/Views/post-upload.phtml b/src/Views/post-upload.phtml index fb1744ae..8d0ce5c0 100644 --- a/src/Views/post-upload.phtml +++ b/src/Views/post-upload.phtml @@ -80,7 +80,7 @@ CustomAssetViewDecorator::addScript('../lib/tagit/jquery.tagit.js'); diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index c3ca2092..0c8c3bbd 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -98,7 +98,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
    Safety: - +
    @@ -313,7 +313,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; -
    +
    renderFile('post-file-render') ?>
    diff --git a/src/Views/top-navigation.phtml b/src/Views/top-navigation.phtml index fedb597b..760a1bbb 100644 --- a/src/Views/top-navigation.phtml +++ b/src/Views/top-navigation.phtml @@ -114,9 +114,9 @@
      -
    • - - +
    • + +
    • diff --git a/src/Views/user-edit.phtml b/src/Views/user-edit.phtml index 299c75c6..51c0b832 100644 --- a/src/Views/user-edit.phtml +++ b/src/Views/user-edit.phtml @@ -43,7 +43,7 @@
    diff --git a/src/Views/user-settings.phtml b/src/Views/user-settings.phtml index 774de923..9e375b53 100644 --- a/src/Views/user-settings.phtml +++ b/src/Views/user-settings.phtml @@ -15,7 +15,7 @@ echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); ?> - + diff --git a/src/Views/user-view.phtml b/src/Views/user-view.phtml index 63d345e7..76f8d4e1 100644 --- a/src/Views/user-view.phtml +++ b/src/Views/user-view.phtml @@ -30,7 +30,7 @@ CustomAssetViewDecorator::addStylesheet('user-view.css');
    Access rank: - +
    diff --git a/src/core.php b/src/core.php index 7d02ab68..c6d3c133 100644 --- a/src/core.php +++ b/src/core.php @@ -11,6 +11,7 @@ setlocale(LC_CTYPE, 'en_US.UTF-8'); ini_set('memory_limit', '128M'); //basic include calls, autoloader init +require_once $rootDir . 'lib' . DS . 'TextCaseConverter' . DS . 'TextCaseConverter.php'; require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'Markdown.php'; require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'MarkdownExtra.php'; require_once $rootDir . 'lib' . DS . 'chibi-core' . DS . 'Facade.php';