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

master v0.0.2
Rinsvent 3 years ago
parent 8f4424ab81
commit cc958bd72d
  1. 18
      src/Service/Transformer/Request/AbstractRequest.php
  2. 17
      src/Service/Transformer/Request/Headers/Header.php
  3. 17
      src/Service/Transformer/Request/Headers/HeaderTransformer.php
  4. 11
      src/Service/Transformer/Request/Headers/UserAgent.php
  5. 16
      src/Service/Transformer/Request/Headers/UserAgentTransformer.php
  6. 11
      src/Service/Transformer/Request/Server/Ip.php
  7. 16
      src/Service/Transformer/Request/Server/IpTransformer.php
  8. 17
      src/Service/Transformer/Request/Server/Server.php
  9. 17
      src/Service/Transformer/Request/Server/ServerTransformer.php

@ -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();
}
}

@ -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);
}
}

@ -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);
}
}

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

@ -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();
}
}

@ -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);
}
}

@ -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…
Cancel
Save