diff --git a/spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php b/spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php index 645e5b65..0c893d84 100644 --- a/spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php +++ b/spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php @@ -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() diff --git a/spec/Phpro/SoapClient/CodeGenerator/Util/NormalizerSpec.php b/spec/Phpro/SoapClient/CodeGenerator/Util/NormalizerSpec.php index b5fa5bde..6ccd59a0 100644 --- a/spec/Phpro/SoapClient/CodeGenerator/Util/NormalizerSpec.php +++ b/spec/Phpro/SoapClient/CodeGenerator/Util/NormalizerSpec.php @@ -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() @@ -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() diff --git a/src/Phpro/SoapClient/CodeGenerator/Util/Normalizer.php b/src/Phpro/SoapClient/CodeGenerator/Util/Normalizer.php index 1aa5eccc..9a8ab288 100644 --- a/src/Phpro/SoapClient/CodeGenerator/Util/Normalizer.php +++ b/src/Phpro/SoapClient/CodeGenerator/Util/Normalizer.php @@ -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'; } /** @@ -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 * @@ -134,10 +149,8 @@ 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')); } /** @@ -145,14 +158,9 @@ public static function normalizeClassname(string $name): string * * @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'); } /**