mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 23:23:51 +00:00
Merge branch 'master' into notification/completion
This commit is contained in:
@@ -276,7 +276,6 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
fwrite($templateResource, $dataDecrypted);
|
||||
rewind($templateResource);
|
||||
}
|
||||
|
||||
$datas = $context->getData($template, $entity, $contextGenerationData);
|
||||
|
||||
try {
|
||||
|
@@ -46,7 +46,6 @@ class RelatorioDriver implements DriverInterface
|
||||
'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType),
|
||||
];
|
||||
$form = new FormDataPart($formFields);
|
||||
dump(json_encode($data));
|
||||
|
||||
try {
|
||||
$response = $this->relatorioClient->request('POST', $this->url, [
|
||||
|
@@ -23,11 +23,7 @@
|
||||
<input type="hidden" name="entityClassName" value="{{ contextManager.getContextByKey(entity.context).entityClass|e('html_attr') }}" />
|
||||
<input type="text" name="entityId" />
|
||||
|
||||
<ul class="record_actions">
|
||||
<li>
|
||||
<button type="submit" class="btn btn-mini btn-neutral">{{ 'docgen.test generate'|trans }}</button>
|
||||
</li>
|
||||
</ul>
|
||||
<button type="submit" class="btn btn-mini btn-misc"><i class="fa fa-cog"></i>{{ 'docgen.test generate'|trans }}</button>
|
||||
</form>
|
||||
</td>
|
||||
<td>
|
||||
|
@@ -11,9 +11,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Serializer\Helper;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
use function array_merge;
|
||||
use function is_array;
|
||||
|
||||
class NormalizeNullValueHelper
|
||||
{
|
||||
@@ -30,7 +32,7 @@ class NormalizeNullValueHelper
|
||||
$this->discriminatorValue = $discriminatorValue;
|
||||
}
|
||||
|
||||
public function normalize(array $attributes, string $format = 'docgen', ?array $context = [])
|
||||
public function normalize(array $attributes, string $format = 'docgen', ?array $context = [], ?ClassMetadata $classMetadata = null)
|
||||
{
|
||||
$data = [];
|
||||
$data['isNull'] = true;
|
||||
@@ -58,7 +60,7 @@ class NormalizeNullValueHelper
|
||||
|
||||
default:
|
||||
$data[$key] = $this->normalizer->normalize(null, $format, array_merge(
|
||||
$context,
|
||||
$this->getContextForAttribute($key, $context, $classMetadata),
|
||||
['docgen:expects' => $class]
|
||||
));
|
||||
|
||||
@@ -69,4 +71,25 @@ class NormalizeNullValueHelper
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getContextForAttribute(string $key, array $initialContext, ?ClassMetadata $classMetadata): array
|
||||
{
|
||||
if (null === $classMetadata) {
|
||||
return $initialContext;
|
||||
}
|
||||
|
||||
$attributeMetadata = $classMetadata->getAttributesMetadata()[$key] ?? null;
|
||||
|
||||
if (null !== $attributeMetadata) {
|
||||
/** @var \Symfony\Component\Serializer\Mapping\AttributeMetadata $attributeMetadata */
|
||||
$initialContext = array_merge(
|
||||
$initialContext,
|
||||
$attributeMetadata->getNormalizationContextForGroups(
|
||||
is_array($initialContext['groups']) ? $initialContext['groups'] : [$initialContext['groups']]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $initialContext;
|
||||
}
|
||||
}
|
||||
|
@@ -196,7 +196,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
|
||||
$normalizer = new NormalizeNullValueHelper($this->normalizer, $typeKey, $typeValue);
|
||||
|
||||
return $normalizer->normalize($keys, $format, $context);
|
||||
return $normalizer->normalize($keys, $format, $context, $metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,9 +260,13 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
/** @var AttributeMetadata $attribute */
|
||||
$value = $this->propertyAccess->getValue($object, $attribute->getName());
|
||||
$key = $attribute->getSerializedName() ?? $attribute->getName();
|
||||
$isTranslatable = $attribute->getNormalizationContextForGroups(
|
||||
is_array($context['groups']) ? $context['groups'] : [$context['groups']]
|
||||
)['is-translatable'] ?? false;
|
||||
$objectContext = array_merge(
|
||||
$context,
|
||||
$attribute->getNormalizationContextForGroups(
|
||||
is_array($context['groups']) ? $context['groups'] : [$context['groups']]
|
||||
)
|
||||
);
|
||||
$isTranslatable = $objectContext['is-translatable'] ?? false;
|
||||
|
||||
if ($isTranslatable) {
|
||||
$data[$key] = $this->translatableStringHelper
|
||||
@@ -273,7 +277,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
foreach ($value as $k => $v) {
|
||||
$arr[$k] =
|
||||
$this->normalizer->normalize($v, $format, array_merge(
|
||||
$context,
|
||||
$objectContext,
|
||||
$attribute->getNormalizationContextForGroups($expectedGroups)
|
||||
));
|
||||
}
|
||||
@@ -281,11 +285,11 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
} elseif (is_object($value)) {
|
||||
$data[$key] =
|
||||
$this->normalizer->normalize($value, $format, array_merge(
|
||||
$context,
|
||||
$objectContext,
|
||||
$attribute->getNormalizationContextForGroups($expectedGroups)
|
||||
));
|
||||
} elseif (null === $value) {
|
||||
$data[$key] = $this->normalizeNullOutputValue($format, $context, $attribute, $reflection);
|
||||
$data[$key] = $this->normalizeNullOutputValue($format, $objectContext, $attribute, $reflection);
|
||||
} else {
|
||||
$data[$key] = $value;
|
||||
}
|
||||
|
@@ -14,6 +14,7 @@ namespace Chill\DocGeneratorBundle\tests\Serializer\Normalizer;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
@@ -33,6 +34,49 @@ final class DocGenObjectNormalizerTest extends KernelTestCase
|
||||
$this->normalizer = self::$container->get(NormalizerInterface::class);
|
||||
}
|
||||
|
||||
public function testChangeContextOnAttribute()
|
||||
{
|
||||
$object = new TestableParentClass();
|
||||
$actual = $this->normalizer->normalize(
|
||||
$object,
|
||||
'docgen',
|
||||
['groups' => 'docgen:read']
|
||||
);
|
||||
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('child', $actual);
|
||||
$this->assertIsArray($actual['child']);
|
||||
$this->assertArrayHasKey('foo', $actual['child']);
|
||||
$this->assertEquals('bar', $actual['child']['foo']);
|
||||
$this->assertArrayNotHasKey('baz', $actual['child']);
|
||||
|
||||
// test with child = null
|
||||
$object->child = null;
|
||||
$actual = $this->normalizer->normalize(
|
||||
$object,
|
||||
'docgen',
|
||||
['groups' => 'docgen:read']
|
||||
);
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('child', $actual);
|
||||
$this->assertIsArray($actual['child']);
|
||||
$this->assertArrayHasKey('foo', $actual['child']);
|
||||
$this->assertEquals('', $actual['child']['foo']);
|
||||
$this->assertArrayNotHasKey('baz', $actual['child']);
|
||||
|
||||
$actual = $this->normalizer->normalize(
|
||||
null,
|
||||
'docgen',
|
||||
['groups' => 'docgen:read', 'docgen:expects' => TestableParentClass::class],
|
||||
);
|
||||
$this->assertIsArray($actual);
|
||||
$this->assertArrayHasKey('child', $actual);
|
||||
$this->assertIsArray($actual['child']);
|
||||
$this->assertArrayHasKey('foo', $actual['child']);
|
||||
$this->assertEquals('', $actual['child']['foo']);
|
||||
$this->assertArrayNotHasKey('baz', $actual['child']);
|
||||
}
|
||||
|
||||
public function testNormalizationBasic()
|
||||
{
|
||||
$scope = new Scope();
|
||||
@@ -99,3 +143,30 @@ final class DocGenObjectNormalizerTest extends KernelTestCase
|
||||
$this->assertEquals($expected, $normalized, 'test normalization fo an user with null center');
|
||||
}
|
||||
}
|
||||
|
||||
class TestableParentClass
|
||||
{
|
||||
/**
|
||||
* @Serializer\Groups("docgen:read")
|
||||
* @Serializer\Context(normalizationContext={"groups": "docgen:read:foo"}, groups={"docgen:read"})
|
||||
*/
|
||||
public ?TestableChildClass $child;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->child = new TestableChildClass();
|
||||
}
|
||||
}
|
||||
|
||||
class TestableChildClass
|
||||
{
|
||||
/**
|
||||
* @Serializer\Groups("docgen:read")
|
||||
*/
|
||||
public string $baz = 'bloup';
|
||||
|
||||
/**
|
||||
* @Serializer\Groups("docgen:read:foo")
|
||||
*/
|
||||
public string $foo = 'bar';
|
||||
}
|
||||
|
@@ -13,6 +13,7 @@ namespace Chill\DocGeneratorBundle\tests\Service\Context;
|
||||
|
||||
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
@@ -23,6 +24,8 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
*/
|
||||
final class BaseContextDataTest extends KernelTestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
@@ -7,6 +7,7 @@ docgen:
|
||||
Context: Contexte
|
||||
New template: Nouveau gabarit
|
||||
Edit template: Modifier gabarit
|
||||
test generate: Tester la génération
|
||||
With context: 'Avec le contexte :'
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user