Skip to content

Commit

Permalink
string camelCase <--> snake_case operations moved out to nayjest/str-…
Browse files Browse the repository at this point in the history
…case-converter package
  • Loading branch information
Nayjest committed Jun 4, 2015
1 parent 76de9de commit 0d30fd2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -9,7 +9,8 @@
}
],
"require": {
"php": ">=5.4"
"php": ">=5.4",
"nayjest/str-case-converter": "~1"
},
"require-dev": {
"phpunit/phpunit": "4.*"
Expand Down
28 changes: 4 additions & 24 deletions src/Manipulator.php
@@ -1,6 +1,7 @@
<?php
namespace Nayjest\Manipulator;

use Nayjest\StrCaseConverter\Str;
use ReflectionClass;

/**
Expand Down Expand Up @@ -71,7 +72,7 @@ public static function assignBySetters($instance, array $fields)
{
$assignedProperties = [];
foreach ($fields as $key => $value) {
$methodName = 'set' . self::snakeToCamelCase($key);
$methodName = 'set' . Str::toCamelCase($key);
if (method_exists($instance, $methodName)) {
$instance->$methodName($value);
$assignedProperties[] = $key;
Expand All @@ -97,27 +98,6 @@ public static function assign($instance, array $fields)
);
}

protected static function snakeToCamelCase($str)
{
return str_replace(
' ',
'',
ucwords(str_replace(array('-', '_'), ' ', $str))
);
}

protected static function camelToSnakeCase($str)
{
$str = lcfirst($str);
$lowerCase = strtolower($str);
$result = '';
$length = strlen($str);
for ($i = 0; $i < $length; $i++) {
$result .= ($str[$i] === $lowerCase[$i] ? '' : '_') . $lowerCase[$i];
}
return $result;
}

protected static $writable = [];

/**
Expand All @@ -143,7 +123,7 @@ public static function getWritable($src, $useSetters = true)
if ($useSetters) {
$setters = self::getSetters($class);
foreach ($setters as $setter) {
self::$writable[$cacheKey][] = self::camelToSnakeCase(substr($setter, 3));
self::$writable[$cacheKey][] = Str::toSnakeCase(substr($setter, 3));
}
}
}
Expand Down Expand Up @@ -226,7 +206,7 @@ public static function getValues($src, array $propNames)
}
foreach ($propNames as $key) {
if (array_key_exists($key, $values)) continue;
$getter = 'get' . self::snakeToCamelCase($key);
$getter = 'get' . Str::toCamelCase($key);
if (method_exists($src, $getter)) {
$values[$key] = $src->{$getter}();
}
Expand Down

0 comments on commit 0d30fd2

Please sign in to comment.