-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into PostProcessorHooks
# Conflicts: # src/SchemaProcessor/RenderQueue.php
- Loading branch information
Showing
67 changed files
with
921 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPModelGenerator\Model\Property; | ||
|
||
use PHPModelGenerator\Exception\SchemaException; | ||
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema; | ||
use PHPModelGenerator\Model\SchemaDefinition\JsonSchemaTrait; | ||
|
||
/** | ||
* Class AbstractProperty | ||
* | ||
* @package PHPModelGenerator\Model\Property | ||
*/ | ||
abstract class AbstractProperty implements PropertyInterface | ||
{ | ||
use JsonSchemaTrait; | ||
|
||
/** @var string */ | ||
protected $name = ''; | ||
/** @var string */ | ||
protected $attribute = ''; | ||
|
||
/** | ||
* Property constructor. | ||
* | ||
* @param string $name | ||
* @param JsonSchema $jsonSchema | ||
* | ||
* @throws SchemaException | ||
*/ | ||
public function __construct(string $name, JsonSchema $jsonSchema) | ||
{ | ||
$this->name = $name; | ||
$this->jsonSchema = $jsonSchema; | ||
|
||
$this->attribute = $this->processAttributeName($name); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getAttribute(): string | ||
{ | ||
return ($this->isInternal() ? '_' : '') . $this->attribute; | ||
} | ||
|
||
/** | ||
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute | ||
* | ||
* @param string $name | ||
* | ||
* @return string | ||
* | ||
* @throws SchemaException | ||
*/ | ||
protected function processAttributeName(string $name): string | ||
{ | ||
$attributeName = preg_replace_callback( | ||
'/([a-z][a-z0-9]*)([A-Z])/', | ||
function ($matches) { | ||
return "{$matches[1]}-{$matches[2]}"; | ||
}, | ||
$name | ||
); | ||
|
||
$elements = array_map( | ||
function ($element) { | ||
return ucfirst(strtolower($element)); | ||
}, | ||
preg_split('/[^a-z0-9]/i', $attributeName) | ||
); | ||
|
||
$attributeName = lcfirst(join('', $elements)); | ||
|
||
if (empty($attributeName)) { | ||
throw new SchemaException( | ||
sprintf( | ||
"Property name '%s' results in an empty attribute name in file %s", | ||
$name, | ||
$this->jsonSchema->getFile() | ||
) | ||
); | ||
} | ||
|
||
return $attributeName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.