Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/SchemaProcessor/PostProcessor/EnumFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

class EnumFilter implements TransformingFilterInterface
{
public const FILTER_TOKEN_GENERATOR_ENUM = 'php_model_generator_enum';

public function getAcceptedTypes(): array
{
return ['string', 'integer', 'null'];
}

public function getToken(): string
{
return 'php_model_generator_enum';
return self::FILTER_TOKEN_GENERATOR_ENUM;
}

public function getFilter(): array
Expand Down
43 changes: 30 additions & 13 deletions src/SchemaProcessor/PostProcessor/EnumPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
$enumSignature = ArrayHash::hash($json, ['enum', 'enum-map', 'title', '$id']);
$enumName = $json['title']
?? basename($json['$id'] ?? $schema->getClassName() . ucfirst($property->getName()));
$enumName = preg_replace('#\W#', '_', $enumName);

$jsonEnumMap = $json['enum-map'] ?? null;
if (!isset($this->generatedEnums[$enumSignature])) {
$this->generatedEnums[$enumSignature] = [
'name' => $enumName,
Expand All @@ -87,7 +89,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
$schema->getJsonSchema(),
$enumName,
$values,
$json['enum-map'] ?? null,
$jsonEnumMap,
),
];
} else {
Expand Down Expand Up @@ -115,7 +117,8 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
$property->setType($inputType, new PropertyType($name, !$property->isRequired()), true);

if ($property->getDefaultValue() && in_array($property->getJsonSchema()->getJson()['default'], $values)) {
$property->setDefaultValue("$name::{$property->getJsonSchema()->getJson()['default']}", true);
$defaultValueEnumCaseName = $this->getCaseName($property->getJsonSchema()->getJson()['default'], $schema->getJsonSchema());
$property->setDefaultValue("$name::{$defaultValueEnumCaseName}", true);
}

// remove the enum validator as the validation is performed by the PHP enum
Expand Down Expand Up @@ -153,6 +156,7 @@ private function checkForExistingTransformingFilter(PropertyInterface $property)

if ($validator instanceof FilterValidator
&& $validator->getFilter() instanceof TransformingFilterInterface
&& $validator->getFilter()->getToken() !== EnumFilter::FILTER_TOKEN_GENERATOR_ENUM
) {
throw new SchemaException(sprintf(
"Can't apply enum filter to an already transformed value on property %s in file %s",
Expand Down Expand Up @@ -234,17 +238,7 @@ private function renderEnum(
array $values,
?array $map,
): string {
$cases = [];

foreach ($values as $value) {
$caseName = ucfirst(NormalizedName::from($map ? array_search($value, $map, true) : $value, $jsonSchema));

if (preg_match('/^\d/', $caseName) === 1) {
$caseName = "_$caseName";
}

$cases[$caseName] = var_export($value, true);
}
$cases = $this->valuesToCases($values, $map, $jsonSchema);

$backedType = null;
switch ($this->getArrayTypes($values)) {
Expand Down Expand Up @@ -288,4 +282,27 @@ private function renderEnum(

return $fqcn;
}

private function valuesToCases(array $values, ?array $map, JsonSchema $jsonSchema): array
{
$cases = [];

foreach ($values as $value) {
$name = $map ? array_search($value, $map, true) : $value;
$caseName = $this->getCaseName($name, $jsonSchema);

$cases[$caseName] = var_export($value, true);
}
return $cases;
}

private function getCaseName(string $name, JsonSchema $jsonSchema): string
{
$caseName = ucfirst(NormalizedName::from($name, $jsonSchema));

if (preg_match('/^\d/', $caseName) === 1) {
$caseName = "_$caseName";
}
return $caseName;
}
}
2 changes: 2 additions & 0 deletions src/Templates/Validator/ComposedItem.phptpl
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@
$proposedValue = $proposedValue ?? $value;
{% endif %}

{% if mergedProperty %}
if (is_object($value)) {
$modifiedValues = array_merge($modifiedValues, $this->{{ modifiedValuesMethod }}($originalModelData, $value));
}
{% endif %}
{% if viewHelper.isMutableBaseValidator(generatorConfiguration, isBaseValidator) %}
{% if not generatorConfiguration.collectErrors() %}
if (isset($validatorIndex)) {
Expand Down
Loading