Skip to content

Commit

Permalink
Merge pull request #193 from Korbeil/fix/188
Browse files Browse the repository at this point in the history
Add API Platform demo into fixtures
  • Loading branch information
Korbeil committed Dec 9, 2019
2 parents 1f6bfb9 + 3348cf5 commit 6207f78
Show file tree
Hide file tree
Showing 90 changed files with 5,561 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/JsonSchema/Generator/Model/PropertyGenerator.php
Expand Up @@ -27,7 +27,7 @@ abstract protected function getParser();

protected function createProperty(Property $property, $namespace, $default = null, bool $required = false): Stmt
{
$propertyName = $property->getPhpName();
$propertyName = $this->getNaming()->getPropertyName($property->getPhpName());
$propertyStmt = new Stmt\PropertyProperty($propertyName);

if (null === $default) {
Expand Down
66 changes: 21 additions & 45 deletions src/JsonSchema/Generator/Naming.php
Expand Up @@ -29,54 +29,23 @@ class Naming
)\b
/ix';

/**
* Get a property name.
*
* @param $name
*
* @return string
*/
public function getPropertyName($name)
public function getPropertyName(string $name): string
{
$name = $this->replaceDollar($name);
$name = $this->cleaning($name);

return Inflector::camelize($name);
return $name;
}

/**
* Get a method name given a prefix.
*
* @param $prefix
* @param $name
*
* @return string
*/
public function getPrefixedMethodName($prefix, $name)
public function getPrefixedMethodName(string $prefix, string $name): string
{
$name = $this->replaceDollar($name);
$name = $this->cleaning($name);

return sprintf('%s%s', $prefix, Inflector::classify($name));
}

/**
* Get a class name.
*
* @param $name
*
* @return string
*/
public function getClassName($name)
public function getClassName(string $name): string
{
$name = $this->replaceDollar($name);
$name = preg_replace_callback('#[/\{\}]+(\w)#', function ($matches) {
return ucfirst($matches[1]);
}, $name);

// Doctrine Inflector does not seem to handle some characters (like dots) well.
// So replace invalid char by an underscore to allow Doctrine to uppercase word correctly.
$name = trim(preg_replace('/[^a-z0-9 ]+/iu', '_', $name));

$name = Inflector::classify($name);
$name = $this->cleaning($name, true);

if (preg_match(self::BAD_CLASS_NAME_REGEX, $name)) {
$name = '_' . $name;
Expand All @@ -85,19 +54,26 @@ public function getClassName($name)
return $name;
}

/**
* @param $name
*
* @return mixed
*/
protected function replaceDollar($name)
protected function cleaning(string $name, bool $class = false): string
{
if (preg_match('/\$/', $name)) {
$name = preg_replace_callback('/\$([a-z])/', function ($matches) {
return 'dollar' . ucfirst($matches[1]);
}, $name);
}

return $name;
$name = preg_replace_callback('#[/\{\}]+(\w)#', function ($matches) {
return ucfirst($matches[1]);
}, $name);

// Doctrine Inflector does not seem to handle some characters (like dots, @, :) well.
// So replace invalid char by an underscore to allow Doctrine to uppercase word correctly.
$name = trim(preg_replace('/[^a-z0-9 ]+/iu', '_', $name));

if ($class) {
return Inflector::classify($name);
}

return Inflector::camelize($name);
}
}
8 changes: 8 additions & 0 deletions src/OpenApi/Tests/fixtures/api-platform-demo/.jane-openapi
@@ -0,0 +1,8 @@
<?php

return [
'openapi-file' => 'https://demo.api-platform.com/docs.json',
'namespace' => 'ApiPlatform\Demo',
'directory' => __DIR__ . '/generated',
'use-fixer' => false,
];

0 comments on commit 6207f78

Please sign in to comment.