Проинтегрировал новую версию библиотеки

v0.0.3
Rinsvent 3 years ago
parent 4cb8dc6f94
commit c1fd2cd189
  1. 11
      src/Attribute/ResponseSchema.php
  2. 29
      src/EventListener/ResponseListener.php

@ -0,0 +1,11 @@
<?php
namespace Rinsvent\ResponseBundle\Attribute;
#[\Attribute(\Attribute::IS_REPEATABLE)]
class ResponseSchema
{
public function __construct(
public string $schemaClass,
) {}
}

@ -2,7 +2,9 @@
namespace Rinsvent\ResponseBundle\EventListener;
use Rinsvent\AttributeExtractor\MethodExtractor;
use Rinsvent\DTO2Data\Dto2DataConverter;
use Rinsvent\ResponseBundle\Attribute\ResponseSchema;
use Rinsvent\ResponseBundle\Response\JsonResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
@ -27,7 +29,32 @@ class ResponseListener
}
$dto2dataConverter = new Dto2DataConverter();
$data = $dto2dataConverter->convert($data);
$schema = $this->grabSchema($event);
$data = $dto2dataConverter->convert($data, $schema);
$response->setData($data);
}
private function grabSchema(ResponseEvent $event): string
{
$request = $event->getRequest();
$controller = $request->get('_controller');
if (is_string($controller)) {
$controller = explode('::', $controller);
}
if (is_callable($controller)) {
if (is_object($controller[0])) {
$controller[0] = get_class($controller[0]);
}
}
if (!is_array($controller) || !count($controller) === 2) {
throw new \InvalidArgumentException('Response schema not found');
}
$methodExtractor = new MethodExtractor($controller[0], $controller[1]);
/** @var ResponseSchema $responseSchema */
while ($responseSchema = $methodExtractor->fetch(ResponseSchema::class)) {
return $responseSchema->schemaClass;
}
throw new \InvalidArgumentException('Response schema not found');
}
}

Loading…
Cancel
Save