/** * Generate a model given a schema * * @param mixed $schema Schema to generate from * @param string $className Class to generate * @param Context $context Context for generation * * @return File[] */ public function generate($schema, $className, Context $context) { $files = []; foreach ($context->getObjectClassMap() as $class) { $properties = []; $methods = []; foreach ($class->getProperties() as $property) { $properties[] = $this->createProperty($property->getName(), $property->getType()); $methods[] = $this->createGetter($property->getName(), $property->getType()); $methods[] = $this->createSetter($property->getName(), $property->getType()); } $model = $this->createModel($class->getName(), $properties, $methods); $namespace = new Stmt\Namespace_(new Name($context->getNamespace() . "\\Model"), [$model]); $files[] = new File($context->getDirectory() . '/Model/' . $class->getName() . '.php', $namespace, self::FILE_TYPE_MODEL); } return $files; }
/** * Generate a set of files given a schema * * @param mixed $schema Schema to generate from * @param string $className Class to generate * @param 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; }
/** * @param $status * @param $schema * @param Context $context * * @return null|Stmt\If_ */ protected function createResponseDenormalizationStatement($status, $schema, Context $context) { $resolvedSchema = null; $array = false; if ($schema instanceof Reference) { $resolvedSchema = $this->getResolver()->resolve($schema); } if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) { $resolvedSchema = $this->getResolver()->resolve($schema->getItems()); $array = true; } if ($resolvedSchema === null) { return [null, null]; } $class = $context->getObjectClassMap()[spl_object_hash($resolvedSchema)]; $class = $context->getNamespace() . "\\Model\\" . $class->getName(); if ($array) { $class .= "[]"; } return ["\\" . $class, new Stmt\If_(new Expr\BinaryOp\Equal(new Scalar\String_($status), new Expr\MethodCall(new Expr\Variable('response'), 'getStatusCode')), ['stmts' => [new Stmt\Return_(new Expr\MethodCall(new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'), 'deserialize', [new Arg(new Expr\Cast\String_(new Expr\MethodCall(new Expr\Variable('response'), 'getBody'))), new Arg(new Scalar\String_($class)), new Arg(new Scalar\String_('json'))]))]])]; }
/** * @param BodyParameter $parameter * @param Context $context * * @return array */ protected function getClass(BodyParameter $parameter, Context $context) { $resolvedSchema = null; $array = false; $schema = $parameter->getSchema(); if ($schema instanceof Reference) { $resolvedSchema = $this->resolver->resolve($schema); } if ($schema instanceof Schema && $schema->getType() == "array" && $schema->getItems() instanceof Reference) { $resolvedSchema = $this->resolver->resolve($schema->getItems()); $array = true; } if ($resolvedSchema === null) { return [$schema->getType(), null]; } $class = $context->getObjectClassMap()[spl_object_hash($resolvedSchema)]; $class = "\\" . $context->getNamespace() . "\\Model\\" . $class->getName(); if ($array) { $class .= "[]"; } return [$class, $array]; }
/** * (@inheritDoc} */ protected function createDenormalizationValueStatement(Context $context, Expr $input) { $fqdn = $context->getNamespace() . '\\Model\\' . $this->className; return new Expr\MethodCall(new Expr\PropertyFetch(new Expr\Variable('this'), 'serializer'), 'deserialize', [new Arg($input), new Arg(new Scalar\String_($fqdn)), new Arg(new Scalar\String_('raw')), new Arg(new Expr\Variable('context'))]); }