Добавил трансформеры

This commit is contained in:
Rinsvent 2022-04-02 15:34:15 +07:00
parent 8f4424ab81
commit cc958bd72d
9 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,18 @@
<?php
namespace Rinsvent\TransformerBundle\Service\Transformer\Request;
use Rinsvent\TransformerBundle\Service\AbstractTransformer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
abstract class AbstractRequest extends AbstractTransformer
{
/** @var Request */
protected $request;
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers;
use Rinsvent\Transformer\Transformer\Meta;
#[\Attribute]
class Header extends Meta
{
public function __construct(
public string $property,
public ?string $default = null,
public array $tags = ['request_headers']
) {
parent::__construct($tags);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers;
use Rinsvent\Transformer\Transformer\Meta;
use Rinsvent\TransformerBundle\Service\Transformer\Request\AbstractRequest;
class HeaderTransformer extends AbstractRequest
{
/**
* @param Header $meta
*/
public function transform(mixed $data, Meta $meta): mixed
{
return $this->request->headers->get($meta->property, $meta->default);
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers;
use Codeception\Step\Meta;
#[\Attribute]
class UserAgent extends Meta
{
}

View File

@ -0,0 +1,16 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers;
use Rinsvent\Transformer\Transformer\Meta;
class UserAgentTransformer extends HeaderTransformer
{
/**
* @param UserAgent $meta
*/
public function transform(mixed $data, Meta $meta): mixed
{
return $this->request->headers->get('user_agent', $meta->default);
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Server;
use Rinsvent\Transformer\Transformer\Meta;
#[\Attribute]
class Ip extends Meta
{
}

View File

@ -0,0 +1,16 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Server;
use Rinsvent\Transformer\Transformer\Meta;
class IpTransformer extends ServerTransformer
{
/**
* @param Ip $meta
*/
public function transform(mixed $data, Meta $meta): mixed
{
return $this->request->getClientIp();
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Server;
use Rinsvent\Transformer\Transformer\Meta;
#[\Attribute]
class Server extends Meta
{
public function __construct(
public string $property,
public ?string $default = null,
public array $tags = ['request_server']
) {
parent::__construct($tags);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Server;
use Rinsvent\Transformer\Transformer\Meta;
use Rinsvent\TransformerBundle\Service\Transformer\Request\AbstractRequest;
class ServerTransformer extends AbstractRequest
{
/**
* @param Server $meta
*/
public function transform(mixed $data, Meta $meta): mixed
{
return $this->request->server->get($meta->property, $meta->default);
}
}