Skip to content

Commit

Permalink
Merge pull request #187 from janvernieuwe/issue-185
Browse files Browse the repository at this point in the history
Don't normalize property casing
  • Loading branch information
janvernieuwe authored Sep 19, 2018
2 parents 58858d1 + b7bac2c commit aeceaae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function it_should_prefix_reserved_keywords()
$this->beConstructedWith('MyNamespace', 'Final', ['xor' => 'string']);
$this->getFileInfo('my/some_dir')->getPathname()->shouldReturn('my/some_dir/FinalType.php');
$this->getName()->shouldReturn('FinalType');
$this->getProperties()[0]->getName()->shouldReturn('xorType');
$this->getProperties()[0]->getName()->shouldReturn('xor');
}

function it_has_properties()
Expand Down
15 changes: 14 additions & 1 deletion spec/Phpro/SoapClient/CodeGenerator/Util/NormalizerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ function it_can_normalize_classnames()
{
$this->normalizeClassname('myType')->shouldReturn('MyType');
$this->normalizeClassname('final')->shouldReturn('FinalType');
$this->normalizeClassname('Final')->shouldReturn('FinalType');
$this->normalizeClassname('UpperCased')->shouldReturn('UpperCased');
$this->normalizeClassname('my-./*type_123')->shouldReturn('MyType123');
$this->normalizeClassname('my-./final*type_123')->shouldReturn('MyFinalType123');
}

function it_noramizes_properties()
{
$this->normalizeProperty('prop1')->shouldReturn('prop1');
$this->normalizeProperty('final')->shouldReturn('finalType');
$this->normalizeProperty('final')->shouldReturn('final');
$this->normalizeProperty('Final')->shouldReturn('Final');
$this->normalizeProperty('UpperCased')->shouldReturn('UpperCased');
$this->normalizeProperty('my-./*prop_123')->shouldReturn('myProp_123');
$this->normalizeProperty('My-./*prop_123')->shouldReturn('MyProp_123');
$this->normalizeProperty('My-./final*prop_123')->shouldReturn('MyFinalProp_123');
}

function it_normalizes_datatypes()
Expand All @@ -63,6 +70,12 @@ function it_generates_property_methods()
$this->generatePropertyMethod('get', 'prop1')->shouldReturn('getProp1');
$this->generatePropertyMethod('set', 'prop1')->shouldReturn('setProp1');
$this->generatePropertyMethod('get', 'prop1_test*./')->shouldReturn('getProp1_test');
$this->generatePropertyMethod('get', 'UpperCased')->shouldReturn('getUpperCased');
$this->generatePropertyMethod('get', 'my-./*prop_123')->shouldReturn('getMyProp_123');
$this->generatePropertyMethod('get', 'My-./*prop_123')->shouldReturn('getMyProp_123');
$this->generatePropertyMethod('get', 'My-./final*prop_123')->shouldReturn('getMyFinalProp_123');
$this->generatePropertyMethod('get', 'final')->shouldReturn('getFinal');
$this->generatePropertyMethod('set', 'Final')->shouldReturn('setFinal');
}

function it_gets_classname_from_fqn()
Expand Down
38 changes: 23 additions & 15 deletions src/Phpro/SoapClient/CodeGenerator/Util/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,16 @@ class Normalizer

/**
* @param string $name
* @param bool $ucfirst
*
* @return string
*/
private static function normalizeReservedKeywords(string $name, $ucfirst = true): string
private static function normalizeReservedKeywords(string $name): string
{
if (!\in_array(strtolower($name), self::$reservedKeywords, true)) {
return $name;
}
$name = ucfirst($name);
$name .= 'Type';

return $ucfirst ? ucfirst($name) : lcfirst($name);
return $name.'Type';
}

/**
Expand All @@ -126,6 +123,24 @@ public static function normalizeNamespace(string $namespace): string
return trim(str_replace('/', '\\', $namespace), '\\');
}

/**
* Convert a word to camelCase or CamelCase (not changing first part!)
*
* @param string $word
* @param string $regexp
*
* @return string
*/
private static function camelCase(string $word, string $regexp):string
{
$parts = array_filter(preg_split($regexp, $word));
$keepUnchanged = array_shift($parts);
$parts = array_map('ucfirst', $parts);
array_unshift($parts, $keepUnchanged);

return implode('', $parts);
}

/**
* @param string $name
*
Expand All @@ -134,25 +149,18 @@ public static function normalizeNamespace(string $namespace): string
public static function normalizeClassname(string $name): string
{
$name = self::normalizeReservedKeywords($name);
$className = trim(preg_replace('{[^a-z0-9]+}i', ' ', $name));
$className = ucwords($className);

return str_replace(' ', '', $className);
return ucfirst(self::camelCase($name, '{[^a-z0-9]+}i'));
}

/**
* @param string $property
*
* @return string
*/
public static function normalizeProperty(string $property)
public static function normalizeProperty(string $property): string
{
$property = self::normalizeReservedKeywords($property, false);
$property = trim(preg_replace('{[^a-z0-9_]}i', ' ', $property));
$property = ucwords($property);
$property = lcfirst($property);

return str_replace(' ', '', $property);
return self::camelCase($property, '{[^a-z0-9_]+}i');
}

/**
Expand Down

0 comments on commit aeceaae

Please sign in to comment.