Skip to content

Commit d51e9a6

Browse files
committed
MixedType added
1 parent 7678ef6 commit d51e9a6

File tree

8 files changed

+83
-239
lines changed

8 files changed

+83
-239
lines changed

src/CodexSoft/JsonApi/Documentation/FormExampleGenerator.php

Lines changed: 0 additions & 236 deletions
This file was deleted.

src/CodexSoft/JsonApi/Documentation/SwaggerGenerator/SwaggerGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use CodexSoft\JsonApi\Documentation\Collector\ResponseDoc;
1111
use CodexSoft\JsonApi\Form\Type\BooleanType\BooleanType;
1212
use CodexSoft\JsonApi\Form\Type\JsonType\JsonType;
13+
use CodexSoft\JsonApi\Form\Type\MixedType\MixedType;
1314
use CodexSoft\JsonApi\Helper\Loggable;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Form\Extension\Core\Type;
@@ -36,6 +37,7 @@ class SwaggerGenerator
3637
Type\TimeType::class => 'string',
3738
Type\UrlType::class => 'string',
3839
JsonType::class => 'object',
40+
MixedType::class => 'mixed',
3941
];
4042

4143
/** @var ApiDoc */

src/CodexSoft/JsonApi/DocumentedFormAction.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CodexSoft\JsonApi\Documentation\Collector\FormDocCollector;
66
use CodexSoft\JsonApi\Form\Extensions\FormFieldDefaultValueExtension;
77
use CodexSoft\JsonApi\Form\Type\BooleanType\BooleanType;
8+
use CodexSoft\JsonApi\Form\Type\MixedType\MixedType;
89
use CodexSoft\JsonApi\Response\DefaultErrorResponse;
910
use CodexSoft\JsonApi\Response\DefaultSuccessResponse;
1011
use CodexSoft\JsonApi\Documentation\Collector\Interfaces\SwagenActionExternalFormInterface;
@@ -242,6 +243,7 @@ protected function addDefaultFormValuesToEmptyRequest(Request $request, string $
242243
// дефолтное значение подставляем только для скаляров из приведенного списка
243244
if (!\in_array($typeClass, [
244245
BooleanType::class,
246+
MixedType::class,
245247
Type\EmailType::class,
246248
Type\IntegerType::class,
247249
Type\NumberType::class,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CodexSoft\JsonApi\Form\Fields;
4+
5+
use CodexSoft\JsonApi\Form\Type\MixedType\MixedType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
8+
class MixedField extends AbstractField
9+
{
10+
11+
public function import(FormBuilderInterface $builder, string $name)
12+
{
13+
parent::import($builder, $name);
14+
$builder->add($name, MixedType::class, $this->options);
15+
}
16+
17+
}

src/CodexSoft/JsonApi/Form/SymfonyFormExampleGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CodexSoft\DateAndTime\DateAndTime;
66
use CodexSoft\JsonApi\Form\Type\BooleanType\BooleanType;
77
use CodexSoft\JsonApi\Form\Type\JsonType\JsonType;
8+
use CodexSoft\JsonApi\Form\Type\MixedType\MixedType;
89
use CodexSoft\JsonApi\Response\ResponseWrappedDataInterface;
910
use Psr\Log\LoggerInterface;
1011
use Psr\Log\NullLogger;
@@ -334,6 +335,10 @@ protected function generateScalarExample(AbstractType $innerType, array $constra
334335
return [];
335336
}
336337

338+
if ($innerType instanceof MixedType) {
339+
return 42;
340+
}
341+
337342
return 'UNKNOWN DATA TYPE';
338343

339344
}

src/CodexSoft/JsonApi/Form/Type/JsonType/JsonType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
1313
{
1414
$builder->resetModelTransformers();
1515
$builder->addModelTransformer(new JsonTransformer);
16-
//$builder->resetClientTransformers();
17-
//$builder->appendClientTransformer(new JsonTransformer());
1816
}
1917

2018
public function getDefaultOptions(array $options)
@@ -31,4 +29,4 @@ public function getName()
3129
{
3230
return 'codexsoft_jsonapi_json_type';
3331
}
34-
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace CodexSoft\JsonApi\Form\Type\MixedType;
4+
5+
use Symfony\Component\Form\DataTransformerInterface;
6+
7+
class MixedTransformer implements DataTransformerInterface
8+
{
9+
10+
public function reverseTransform($value)
11+
{
12+
return json_decode($value);
13+
}
14+
15+
/**
16+
* @param \DateTime $value
17+
* @return array|string
18+
*/
19+
public function transform($value)
20+
{
21+
return json_encode($value);
22+
}
23+
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace CodexSoft\JsonApi\Form\Type\MixedType;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
class MixedType extends AbstractType
10+
{
11+
12+
public function buildForm(FormBuilderInterface $builder, array $options)
13+
{
14+
$builder->resetModelTransformers();
15+
$builder->addModelTransformer(new MixedTransformer());
16+
}
17+
18+
public function getDefaultOptions(array $options)
19+
{
20+
return [];
21+
}
22+
23+
public function configureOptions(OptionsResolver $resolver)
24+
{
25+
$resolver->setDefault('allow_extra_fields', true);
26+
}
27+
28+
public function getName()
29+
{
30+
return 'codexsoft_jsonapi_mixed_type';
31+
}
32+
}

0 commit comments

Comments
 (0)