Добавил трансформеры
This commit is contained in:
parent
8f4424ab81
commit
cc958bd72d
18
src/Service/Transformer/Request/AbstractRequest.php
Normal file
18
src/Service/Transformer/Request/AbstractRequest.php
Normal 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();
|
||||
}
|
||||
}
|
17
src/Service/Transformer/Request/Headers/Header.php
Normal file
17
src/Service/Transformer/Request/Headers/Header.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
11
src/Service/Transformer/Request/Headers/UserAgent.php
Normal file
11
src/Service/Transformer/Request/Headers/UserAgent.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers;
|
||||
|
||||
use Codeception\Step\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class UserAgent extends Meta
|
||||
{
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
11
src/Service/Transformer/Request/Server/Ip.php
Normal file
11
src/Service/Transformer/Request/Server/Ip.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rinsvent\Data2DTOBundle\Service\Transformer\Request\Server;
|
||||
|
||||
use Rinsvent\Transformer\Transformer\Meta;
|
||||
|
||||
#[\Attribute]
|
||||
class Ip extends Meta
|
||||
{
|
||||
|
||||
}
|
16
src/Service/Transformer/Request/Server/IpTransformer.php
Normal file
16
src/Service/Transformer/Request/Server/IpTransformer.php
Normal 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();
|
||||
}
|
||||
}
|
17
src/Service/Transformer/Request/Server/Server.php
Normal file
17
src/Service/Transformer/Request/Server/Server.php
Normal 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);
|
||||
}
|
||||
}
|
17
src/Service/Transformer/Request/Server/ServerTransformer.php
Normal file
17
src/Service/Transformer/Request/Server/ServerTransformer.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user