Merge pull request #1522 from ahilles107/port/options_support_in_model

Port/options support in model
This commit is contained in:
Paweł Mikołajczuk 2019-06-01 15:31:09 +02:00 committed by Guilhem N
parent 846e2e3ae4
commit 14e4eedbad
6 changed files with 29 additions and 6 deletions

View File

@ -22,6 +22,7 @@ final class Model extends AbstractAnnotation
public static $_types = [ public static $_types = [
'type' => 'string', 'type' => 'string',
'groups' => '[string]', 'groups' => '[string]',
'options' => '[mixed]',
]; ];
public static $_required = ['type']; public static $_required = ['type'];
@ -40,4 +41,9 @@ final class Model extends AbstractAnnotation
* @var string[] * @var string[]
*/ */
public $groups; public $groups;
/**
* @var mixed[]
*/
public $options;
} }

View File

@ -19,13 +19,16 @@ final class Model
private $groups; private $groups;
private $options;
/** /**
* @param string[]|null $groups * @param string[]|null $groups
*/ */
public function __construct(Type $type, array $groups = null) public function __construct(Type $type, array $groups = null, array $options = null)
{ {
$this->type = $type; $this->type = $type;
$this->groups = $groups; $this->groups = $groups;
$this->options = $options;
} }
/** /**
@ -48,4 +51,12 @@ final class Model
{ {
return md5(serialize([$this->type, $this->groups])); return md5(serialize([$this->type, $this->groups]));
} }
/**
* @return mixed[]|null
*/
public function getOptions()
{
return $this->options;
}
} }

View File

@ -51,7 +51,7 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
$class = $model->getType()->getClassName(); $class = $model->getType()->getClassName();
$form = $this->formFactory->create($class, null, []); $form = $this->formFactory->create($class, null, $model->getOptions() ?? []);
$this->parseForm($schema, $form); $this->parseForm($schema, $form);
} }
@ -101,7 +101,11 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
if (!$builtinFormType = $this->getBuiltinFormType($type)) { if (!$builtinFormType = $this->getBuiltinFormType($type)) {
// if form type is not builtin in Form component. // if form type is not builtin in Form component.
$model = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, get_class($type->getInnerType()))); $model = new Model(
new Type(Type::BUILTIN_TYPE_OBJECT, false, get_class($type->getInnerType())),
null,
$config->getOptions()
);
$property->setRef($this->modelRegistry->register($model)); $property->setRef($this->modelRegistry->register($model));
return; return;

View File

@ -44,7 +44,7 @@ final class ModelRegister
if ($annotation instanceof Schema && $annotation->ref instanceof ModelAnnotation) { if ($annotation instanceof Schema && $annotation->ref instanceof ModelAnnotation) {
$model = $annotation->ref; $model = $annotation->ref;
$annotation->ref = $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups))); $annotation->ref = $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups), $model->options));
// It is no longer an unmerged annotation // It is no longer an unmerged annotation
$this->detach($model, $annotation, $analysis); $this->detach($model, $annotation, $analysis);
@ -90,7 +90,7 @@ final class ModelRegister
} }
$annotation->merge([new $annotationClass([ $annotation->merge([new $annotationClass([
'ref' => $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups))), 'ref' => $this->modelRegistry->register(new Model($this->createType($model->type), $this->getGroups($model, $parentGroups), $model->options)),
])]); ])]);
// It is no longer an unmerged annotation // It is no longer an unmerged annotation

View File

@ -91,7 +91,7 @@ class ApiController
* name="foo", * name="foo",
* in="body", * in="body",
* description="This is a parameter", * description="This is a parameter",
* @SWG\Schema(ref=@Model(type=UserType::class)) * @SWG\Schema(ref=@Model(type=UserType::class, options={"bar": "baz"}))
* ) * )
*/ */
public function submitUserTypeAction() public function submitUserTypeAction()

View File

@ -50,5 +50,7 @@ class UserType extends AbstractType
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => User::class, 'data_class' => User::class,
]); ]);
$resolver->setRequired('bar');
} }
} }