Text case conversion moved to gist

This commit is contained in:
Marcin Kurczewski 2014-04-12 16:22:30 +02:00
parent d3e135ea15
commit 2b33bf44d2
16 changed files with 55 additions and 72 deletions

3
.gitmodules vendored
View file

@ -7,3 +7,6 @@
[submodule "lib/chibi-sql"] [submodule "lib/chibi-sql"]
path = lib/chibi-sql path = lib/chibi-sql
url = https://github.com/rr-/chibi-sql.git url = https://github.com/rr-/chibi-sql.git
[submodule "lib/TextCaseConverter"]
path = lib/TextCaseConverter
url = https://gist.github.com/rr-/10522533.git

1
lib/TextCaseConverter Submodule

@ -0,0 +1 @@
Subproject commit eabd7f5ff2a1d1eb8a683e6bc658d613a56980b0

View file

@ -8,6 +8,13 @@ class Enum
return array_search($constant, $constants); 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() public static function getAll()
{ {
$cls = new ReflectionClass(get_called_class()); $cls = new ReflectionClass(get_called_class());

View file

@ -11,8 +11,14 @@ class PrivilegesHelper
if (strpos($key, '.') === false) if (strpos($key, '.') === false)
$key .= '.'; $key .= '.';
list ($privilegeName, $subPrivilegeName) = explode('.', $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, '.'); $key = rtrim($privilegeName . '.' . $subPrivilegeName, '.');
$minAccessRank = TextHelper::resolveConstant($minAccessRankName, 'AccessRank'); $minAccessRank = TextHelper::resolveConstant($minAccessRankName, 'AccessRank');
@ -34,7 +40,10 @@ class PrivilegesHelper
$user = \Chibi\Registry::getContext()->user; $user = \Chibi\Registry::getContext()->user;
$minAccessRank = AccessRank::Admin; $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])) if (isset(self::$privileges[$key]))
{ {
$minAccessRank = self::$privileges[$key]; $minAccessRank = self::$privileges[$key];

View file

@ -17,61 +17,12 @@ class TextHelper
return $text; 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) public static function resolveConstant($constantName, $className = null)
{ {
$constantName = self::kebabCaseToCamelCase($constantName); $constantName = TextCaseConverter::convert($constantName,
//convert from kebab-case to CamelCase TextCaseConverter::SPINAL_CASE,
TextCaseConverter::CAMEL_CASE);
if ($className !== null) if ($className !== null)
{ {
$constantName = $className . '::' . $constantName; $constantName = $className . '::' . $constantName;

View file

@ -75,7 +75,10 @@ abstract class AbstractCrudModel implements IModel
$entity = self::spawn(); $entity = self::spawn();
foreach ($row as $key => $val) foreach ($row as $key => $val)
{ {
$key = TextHelper::snakeCaseToCamelCase($key, true); $key = TextCaseConverter::convert($key,
TextCaseConverter::SNAKE_CASE,
TextCaseConverter::LOWER_CAMEL_CASE);
$entity->$key = $val; $entity->$key = $val;
} }
return $entity; return $entity;
@ -93,7 +96,12 @@ abstract class AbstractCrudModel implements IModel
if (isset($keyCache[$key])) if (isset($keyCache[$key]))
$key = $keyCache[$key]; $key = $keyCache[$key];
else else
$key = $keyCache[$key] = TextHelper::snakeCaseToCamelCase($key, true); {
$key = $keyCache[$key] = TextCaseConverter::convert($key,
TextCaseConverter::SNAKE_CASE,
TextCaseConverter::LOWER_CAMEL_CASE);
}
$entity->$key = $val; $entity->$key = $val;
} }
$entities[$i] = $entity; $entities[$i] = $entity;

View file

@ -7,7 +7,7 @@
<?php foreach (PostSafety::getAll() as $safety): ?> <?php foreach (PostSafety::getAll() as $safety): ?>
<label> <label>
<input type="radio" name="safety" value="<?php echo $safety ?>" <?php if ($this->context->transport->post->safety == $safety) echo 'checked="checked"' ?>/> <input type="radio" name="safety" value="<?php echo $safety ?>" <?php if ($this->context->transport->post->safety == $safety) echo 'checked="checked"' ?>/>
&nbsp;<?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety), true) ?> &nbsp;<?php echo ucfirst(PostSafety::toDisplayString($safety)) ?>
</label> </label>
<?php endforeach ?> <?php endforeach ?>
</div> </div>

View file

@ -44,9 +44,9 @@ if (PrivilegesHelper::confirm(Privilege::MassTag))
<?php foreach ($tabs as $i => $tab): ?> <?php foreach ($tabs as $i => $tab): ?>
<?php list($name, $url) = $tab ?> <?php list($name, $url) = $tab ?>
<?php if ($i == $activeTab): ?> <?php if ($i == $activeTab): ?>
<li class="selected <?php echo TextHelper::humanCaseToKebabCase($name) ?>"> <li class="selected <?php echo TextCaseConverter::convert($name, TextCaseConverter::BLANK_CASE, TextCaseConverter::SPINAL_CASE) ?>">
<?php else: ?> <?php else: ?>
<li class="<?php echo TextHelper::humanCaseToKebabCase($name) ?>"> <li class="<?php echo TextCaseConverter::convert($name, TextCaseConverter::BLANK_CASE, TextCaseConverter::SPINAL_CASE) ?>">
<?php endif ?> <?php endif ?>
<a href="<?php echo $url ?>"> <a href="<?php echo $url ?>">
<?php echo $name ?> <?php echo $name ?>

View file

@ -4,8 +4,11 @@ CustomAssetViewDecorator::addStylesheet('post-small.css');
$classNames = $classNames =
[ [
'post', 'post',
'post-type-' .(TextHelper::resolveMimeType($this->context->post->mimeType) ?: 'post-type-' . TextCaseConverter::convert(
TextHelper::camelCaseToHumanCase(PostType::toString($this->context->post->type))), TextHelper::resolveMimeType($this->context->post->mimeType)
?: PostType::toString($this->context->post->type),
TextCaseConverter::CAMEL_CASE,
TextCaseConverter::SPINAL_CASE),
]; ];
$masstag = (isset($this->context->source) $masstag = (isset($this->context->source)

View file

@ -80,7 +80,7 @@ CustomAssetViewDecorator::addScript('../lib/tagit/jquery.tagit.js');
<?php foreach (PostSafety::getAll() as $safety): ?> <?php foreach (PostSafety::getAll() as $safety): ?>
<label> <label>
<input type="radio" name="safety" value="<?php echo $safety ?>"<?php if (!$checked) echo ' checked="checked"' ?>/> <input type="radio" name="safety" value="<?php echo $safety ?>"<?php if (!$checked) echo ' checked="checked"' ?>/>
<?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety), true) ?> <?php echo ucfirst(PostSafety::toDisplayString($safety)) ?>
<?php $checked = true ?> <?php $checked = true ?>
</label> </label>
<?php endforeach ?> <?php endforeach ?>

View file

@ -98,7 +98,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="key-value safety"> <div class="key-value safety">
<span class="key">Safety:</span> <span class="key">Safety:</span>
<span class="value safety-<?php echo $val = TextHelper::camelCaseToHumanCase(PostSafety::toString($this->context->transport->post->safety)) ?>" title="<?php echo $val ?>"> <span class="value safety-<?php echo $val = PostSafety::toDisplayString($this->context->transport->post->safety) ?>" title="<?php echo $val ?>">
<?php echo $val ?> <?php echo $val ?>
</span> </span>
</div> </div>
@ -313,7 +313,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
</div> </div>
<?php endif ?> <?php endif ?>
<div class="post-wrapper post-type-<?php echo strtolower(PostType::toString($this->context->transport->post->type)) ?>"> <div class="post-wrapper post-type-<?php echo PostType::toString($this->context->transport->post->type) ?>">
<?php echo $this->renderFile('post-file-render') ?> <?php echo $this->renderFile('post-file-render') ?>
</div> </div>

View file

@ -114,9 +114,9 @@
<ul> <ul>
<?php foreach (PostSafety::getAll() as $safety): ?> <?php foreach (PostSafety::getAll() as $safety): ?>
<?php if (PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety))): ?> <?php if (PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety))): ?>
<li class="safety-<?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety)) ?>"> <li class="safety-<?php echo TextCaseConverter::convert(PostSafety::toString($safety), TextCaseConverter::CAMEL_CASE, TextCaseConverter::SPINAL_CASE) ?>">
<a href="<?php echo \Chibi\UrlHelper::route('user', 'toggle-safety', ['safety' => $safety]) ?>" class="<?php echo $this->context->user->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>" title="Searching <?php echo TextHelper::camelCaseToHumanCase(PostSafety::ToString($safety)) ?> posts: <?php echo $this->context->user->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>"> <a href="<?php echo \Chibi\UrlHelper::route('user', 'toggle-safety', ['safety' => $safety]) ?>" class="<?php echo $this->context->user->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>" title="Searching <?php echo PostSafety::toDisplayString($safety) ?> posts: <?php echo $this->context->user->hasEnabledSafety($safety) ? 'enabled' : 'disabled' ?>">
<span><?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety), true) ?></span> <span><?php echo ucfirst(PostSafety::toDisplayString($safety)) ?></span>
</a> </a>
</li> </li>
<?php endif ?> <?php endif ?>

View file

@ -43,7 +43,7 @@
<?php else: ?> <?php else: ?>
<option value="<?php echo $rank ?>"> <option value="<?php echo $rank ?>">
<?php endif ?> <?php endif ?>
<?php echo TextHelper::camelCaseToHumanCase(AccessRank::toString($rank)) ?> <?php echo AccessRank::toDisplayString($rank) ?>
</option> </option>
<?php endforeach ?> <?php endforeach ?>
</select></div> </select></div>

View file

@ -15,7 +15,7 @@
echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs); echo TextHelper::htmlTag('input', TextHelper::HTML_LEAF, $attrs);
?> ?>
<?php echo TextHelper::camelCaseToHumanCase(PostSafety::toString($safety), true) ?> <?php echo ucfirst(PostSafety::toDisplayString($safety)) ?>
</label> </label>
<?php endif ?> <?php endif ?>
<?php endforeach ?> <?php endforeach ?>

View file

@ -30,7 +30,7 @@ CustomAssetViewDecorator::addStylesheet('user-view.css');
<div class="key-value access-rank"> <div class="key-value access-rank">
<span class="key">Access rank:</span> <span class="key">Access rank:</span>
<span class="value" title="<?php echo $val = TextHelper::camelCaseToHumanCase(AccessRank::toString($this->context->transport->user->accessRank)) ?>"> <span class="value" title="<?php echo $val = AccessRank::toDisplayString($this->context->transport->user->accessRank) ?>">
<?php echo $val ?> <?php echo $val ?>
</span> </span>
</div> </div>

View file

@ -11,6 +11,7 @@ setlocale(LC_CTYPE, 'en_US.UTF-8');
ini_set('memory_limit', '128M'); ini_set('memory_limit', '128M');
//basic include calls, autoloader init //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 . 'Markdown.php';
require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'MarkdownExtra.php'; require_once $rootDir . 'lib' . DS . 'php-markdown' . DS . 'Michelf' . DS . 'MarkdownExtra.php';
require_once $rootDir . 'lib' . DS . 'chibi-core' . DS . 'Facade.php'; require_once $rootDir . 'lib' . DS . 'chibi-core' . DS . 'Facade.php';