|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/json-schema-to-php-ast for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/json-schema-to-php-ast/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\JsonSchemaToPhpAst; |
| 12 | + |
| 13 | +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; |
| 14 | +use OpenCodeModeling\CodeAst\Builder\ClassBuilderCollection; |
| 15 | +use OpenCodeModeling\CodeAst\Builder\ClassPropertyBuilder; |
| 16 | +use OpenCodeModeling\CodeAst\Package\ClassInfoList; |
| 17 | +use OpenCodeModeling\JsonSchemaToPhp\Type\ArrayType; |
| 18 | +use OpenCodeModeling\JsonSchemaToPhp\Type\ObjectType; |
| 19 | +use OpenCodeModeling\JsonSchemaToPhp\Type\ReferenceType; |
| 20 | +use OpenCodeModeling\JsonSchemaToPhp\Type\ScalarType; |
| 21 | +use OpenCodeModeling\JsonSchemaToPhp\Type\TypeDefinition; |
| 22 | +use OpenCodeModeling\JsonSchemaToPhp\Type\TypeSet; |
| 23 | +use PhpParser\NodeTraverser; |
| 24 | +use PhpParser\Parser; |
| 25 | +use PhpParser\PrettyPrinterAbstract; |
| 26 | + |
| 27 | +final class ClassGenerator |
| 28 | +{ |
| 29 | + private ClassInfoList $classInfoList; |
| 30 | + private ValueObjectFactory $valueObjectFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var callable |
| 34 | + */ |
| 35 | + private $classNameFilter; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var callable |
| 39 | + */ |
| 40 | + private $propertyNameFilter; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var callable |
| 44 | + */ |
| 45 | + private $methodNameFilter; |
| 46 | + |
| 47 | + public function __construct( |
| 48 | + ClassInfoList $classInfoList, |
| 49 | + ValueObjectFactory $valueObjectFactory, |
| 50 | + callable $classNameFilter, |
| 51 | + callable $propertyNameFilter, |
| 52 | + callable $methodNameFilter |
| 53 | + ) { |
| 54 | + $this->classInfoList = $classInfoList; |
| 55 | + $this->valueObjectFactory = $valueObjectFactory; |
| 56 | + $this->classNameFilter = $classNameFilter; |
| 57 | + $this->propertyNameFilter = $propertyNameFilter; |
| 58 | + $this->methodNameFilter = $methodNameFilter; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param ClassBuilder $classBuilder Main class |
| 63 | + * @param ClassBuilderCollection $classBuilderCollection Collection for other classes |
| 64 | + * @param TypeSet $typeSet |
| 65 | + * @param string $srcFolder Source folder for namespace imports |
| 66 | + * @param string|null $className Class name of other classes |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function generateClasses( |
| 70 | + ClassBuilder $classBuilder, |
| 71 | + ClassBuilderCollection $classBuilderCollection, |
| 72 | + TypeSet $typeSet, |
| 73 | + string $srcFolder, |
| 74 | + string $className = null |
| 75 | + ): void { |
| 76 | + $type = $typeSet->first(); |
| 77 | + |
| 78 | + $classInfo = $this->classInfoList->classInfoForPath($srcFolder); |
| 79 | + $classNamespace = $classInfo->getClassNamespaceFromPath($srcFolder); |
| 80 | + |
| 81 | + if ($type instanceof ReferenceType |
| 82 | + && $refType = $type->resolvedType() |
| 83 | + ) { |
| 84 | + $type = $refType->first(); |
| 85 | + } |
| 86 | + |
| 87 | + switch (true) { |
| 88 | + case $type instanceof ObjectType: |
| 89 | + /** @var TypeSet $propertyTypeSet */ |
| 90 | + foreach ($type->properties() as $propertyName => $propertyTypeSet) { |
| 91 | + $propertyClassName = ($this->classNameFilter)($propertyName); |
| 92 | + $propertyPropertyName = ($this->propertyNameFilter)($propertyName); |
| 93 | + |
| 94 | + $propertyType = $propertyTypeSet->first(); |
| 95 | + switch (true) { |
| 96 | + case $propertyType instanceof ArrayType: |
| 97 | + foreach ($propertyType->items() as $itemTypeSet) { |
| 98 | + $itemType = $itemTypeSet->first(); |
| 99 | + |
| 100 | + if (null === $itemType) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + $itemClassName = ($this->classNameFilter)($itemType->name()); |
| 104 | + $itemPropertyName = ($this->propertyNameFilter)($itemType->name()); |
| 105 | + |
| 106 | + $this->generateClasses( |
| 107 | + ClassBuilder::fromScratch($itemClassName, $classNamespace)->setFinal(true), |
| 108 | + $classBuilderCollection, |
| 109 | + $itemTypeSet, |
| 110 | + $srcFolder, |
| 111 | + $itemPropertyName |
| 112 | + ); |
| 113 | + } |
| 114 | + // no break |
| 115 | + case $propertyType instanceof ObjectType: |
| 116 | + $this->generateClasses( |
| 117 | + ClassBuilder::fromScratch($propertyClassName, $classNamespace)->setFinal(true), |
| 118 | + $classBuilderCollection, |
| 119 | + $propertyTypeSet, |
| 120 | + $srcFolder, |
| 121 | + $propertyClassName |
| 122 | + ); |
| 123 | + $classBuilder->addNamespaceImport($classNamespace . '\\' . $propertyClassName); |
| 124 | + $classBuilder->addProperty(ClassPropertyBuilder::fromScratch($propertyPropertyName, $propertyClassName)); |
| 125 | + break; |
| 126 | + case $propertyType instanceof ReferenceType: |
| 127 | + if ($propertyRefType = $propertyType->resolvedType()) { |
| 128 | + $this->generateClasses( |
| 129 | + ClassBuilder::fromScratch($propertyClassName, $classNamespace)->setFinal(true), |
| 130 | + $classBuilderCollection, |
| 131 | + $propertyRefType, |
| 132 | + $srcFolder, |
| 133 | + $propertyType->name() |
| 134 | + ); |
| 135 | + $propertyClassName = ($this->classNameFilter)($propertyType->name()); |
| 136 | + $classBuilder->addNamespaceImport($classNamespace . '\\' . $propertyClassName); |
| 137 | + } |
| 138 | + $classBuilder->addProperty( |
| 139 | + ClassPropertyBuilder::fromScratch($propertyPropertyName, $propertyClassName) |
| 140 | + ); |
| 141 | + break; |
| 142 | + case $propertyType instanceof ScalarType: |
| 143 | + $classBuilderCollection->add( |
| 144 | + $this->generateValueObject($propertyClassName, $classNamespace, $propertyType) |
| 145 | + ); |
| 146 | + $classBuilder->addNamespaceImport($classNamespace . '\\' . $propertyClassName); |
| 147 | + $classBuilder->addProperty( |
| 148 | + ClassPropertyBuilder::fromScratch( |
| 149 | + $propertyPropertyName, |
| 150 | + $propertyClassName |
| 151 | + ) |
| 152 | + ); |
| 153 | + break; |
| 154 | + default: |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + $classBuilderCollection->add($classBuilder); |
| 159 | + break; |
| 160 | + case $type instanceof ScalarType: |
| 161 | + $classBuilderCollection->add( |
| 162 | + $this->generateValueObject(($this->classNameFilter)($className), $classNamespace, $type) |
| 163 | + ); |
| 164 | + break; |
| 165 | + case $type instanceof ArrayType: |
| 166 | + $arrayClassBuilder = $this->generateValueObject(($this->classNameFilter)($className), $classNamespace, $type); |
| 167 | + $this->addNamespaceImport($arrayClassBuilder, $type); |
| 168 | + $classBuilderCollection->add($arrayClassBuilder); |
| 169 | + break; |
| 170 | + default: |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + public function generateValueObject(string $className, string $classNamespace, TypeDefinition $definition): ClassBuilder |
| 176 | + { |
| 177 | + $classBuilder = $this->valueObjectFactory->classBuilder($definition); |
| 178 | + $classBuilder->setName($className) |
| 179 | + ->setNamespace($classNamespace) |
| 180 | + ->setStrict(true) |
| 181 | + ->setFinal(true); |
| 182 | + |
| 183 | + return $classBuilder; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @param ClassBuilderCollection $classBuilderCollection |
| 188 | + * @param Parser $parser |
| 189 | + * @param PrettyPrinterAbstract $printer |
| 190 | + * @return array<string, string> List of filename => code |
| 191 | + */ |
| 192 | + public function generateFiles( |
| 193 | + ClassBuilderCollection $classBuilderCollection, |
| 194 | + Parser $parser, |
| 195 | + PrettyPrinterAbstract $printer |
| 196 | + ): array { |
| 197 | + $files = []; |
| 198 | + |
| 199 | + $previousNamespace = '__invalid//namespace__'; |
| 200 | + |
| 201 | + foreach ($classBuilderCollection as $classBuilder) { |
| 202 | + if ($previousNamespace !== $classBuilder->getNamespace()) { |
| 203 | + $previousNamespace = $classBuilder->getNamespace(); |
| 204 | + $classInfo = $this->classInfoList->classInfoForNamespace($previousNamespace); |
| 205 | + $path = $classInfo->getPath($classBuilder->getNamespace() . '\\' . $classBuilder->getName()); |
| 206 | + } |
| 207 | + $filename = $classInfo->getFilenameFromPathAndName($path, $classBuilder->getName()); |
| 208 | + |
| 209 | + $nodeTraverser = new NodeTraverser(); |
| 210 | + $classBuilder->injectVisitors($nodeTraverser, $parser); |
| 211 | + |
| 212 | + $files[$filename] = $printer->prettyPrintFile($nodeTraverser->traverse([])); |
| 213 | + } |
| 214 | + |
| 215 | + return $files; |
| 216 | + } |
| 217 | + |
| 218 | + private function addNamespaceImport(ClassBuilder $classBuilder, TypeDefinition $typeDefinition): void |
| 219 | + { |
| 220 | + switch (true) { |
| 221 | + case $typeDefinition instanceof ArrayType: |
| 222 | + foreach ($typeDefinition->items() as $itemTypeSet) { |
| 223 | + $itemType = $itemTypeSet->first(); |
| 224 | + |
| 225 | + if (null === $itemType) { |
| 226 | + continue; |
| 227 | + } |
| 228 | + |
| 229 | + if ($itemType instanceof ReferenceType |
| 230 | + && $refType = $itemType->resolvedType() |
| 231 | + ) { |
| 232 | + $itemType = $refType->first(); |
| 233 | + } |
| 234 | + $itemClassName = ($this->classNameFilter)($itemType->name()); |
| 235 | + $classBuilder->addNamespaceImport($classBuilder->getNamespace() . '\\' . $itemClassName); |
| 236 | + } |
| 237 | + break; |
| 238 | + default: |
| 239 | + break; |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments