Joli\Jane\Generator\NormalizerGenerator::generate PHP Method

generate() public method

Generate a set of files given a schema
public generate ( mixed $schema, string $className, Joli\Jane\Generator\Context\Context $context ) : File[]
$schema mixed Schema to generate from
$className string Class to generate
$context Joli\Jane\Generator\Context\Context Context for generation
return File[]
    public function generate($schema, $className, Context $context)
    {
        $files = [];
        $classes = [];
        foreach ($context->getObjectClassMap() as $class) {
            $methods = [];
            $modelFqdn = $context->getNamespace() . "\\Model\\" . $class->getName();
            $methods[] = $this->createSupportsDenormalizationMethod($modelFqdn);
            $methods[] = $this->createSupportsNormalizationMethod($modelFqdn);
            $methods[] = $this->createDenormalizeMethod($modelFqdn, $context, $class->getProperties());
            $methods[] = $this->createNormalizeMethod($modelFqdn, $context, $class->getProperties());
            $normalizerClass = $this->createNormalizerClass($class->getName() . 'Normalizer', $methods);
            $classes[] = $normalizerClass->name;
            $namespace = new Stmt\Namespace_(new Name($context->getNamespace() . "\\Normalizer"), [new Stmt\Use_([new Stmt\UseUse(new Name('Joli\\Jane\\Runtime\\Reference'))]), new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface'))]), new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface'))]), new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\\Component\\Serializer\\Normalizer\\SerializerAwareNormalizer'))]), $normalizerClass]);
            $files[] = new File($context->getDirectory() . '/Normalizer/' . $class->getName() . 'Normalizer.php', $namespace, self::FILE_TYPE_NORMALIZER);
        }
        $files[] = new File($context->getDirectory() . '/Normalizer/NormalizerFactory.php', new Stmt\Namespace_(new Name($context->getNamespace() . "\\Normalizer"), [$this->createNormalizerFactoryClass($classes)]), self::FILE_TYPE_NORMALIZER);
        return $files;
    }

Usage Example

Beispiel #1
0
 /**
  * Generate a list of files
  *
  * @param string $swaggerSpec Location of the specification
  * @param string $namespace   Namespace of the library
  * @param string $directory   Path for the root directory of the generated files
  *
  * @return File[]
  */
 public function generate($swaggerSpec, $namespace, $directory)
 {
     /** @var Swagger $swagger */
     $context = $this->createContext($swaggerSpec, 'Client', $namespace, $directory);
     $files = [];
     $files = array_merge($files, $this->modelGenerator->generate($context->getRootReference(), 'Client', $context));
     $files = array_merge($files, $this->normalizerGenerator->generate($context->getRootReference(), 'Client', $context));
     $clients = $this->clientGenerator->generate($context->getRootReference(), $namespace, $context);
     foreach ($clients as $node) {
         $files[] = new File($directory . DIRECTORY_SEPARATOR . 'Resource' . DIRECTORY_SEPARATOR . $node->stmts[2]->name . '.php', $node, '');
     }
     return $files;
 }