добавил toString трансформер

master v0.0.2
Rinsvent 3 years ago
parent ef4dd7d183
commit 79694fb346
  1. 14
      src/Transformer/ToString.php
  2. 26
      src/Transformer/ToStringTransformer.php
  3. 22
      tests/unit/ToStringTransformTest.php
  4. 11
      tests/unit/fixtures/Foo.php

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Rinsvent\Transformer\Transformer;
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]
class ToString extends Meta
{
public function __construct(
public array $tags = ['default'],
) {
parent::__construct(...func_get_args());
}
}

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Rinsvent\Transformer\Transformer;
use function PHPUnit\Framework\equalTo;
class ToStringTransformer implements TransformerInterface
{
/**
* @param string|null $data
* @param ToString $meta
* @return string|null
*/
public function transform(mixed $data, Meta $meta): mixed
{
if ($data === null) {
return $data;
}
try {
return (string)$data;
} catch (\Throwable) {
return $data;
}
}
}

@ -0,0 +1,22 @@
<?php
namespace Rinsvent\DTO2Data\Tests\Converter;
use tests\unit\fixtures\Foo;
use Rinsvent\Transformer\Transformer;
class ToStringTransformTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;
// tests
public function testSuccessFillRequestData()
{
$t = new Transformer();
$actual = $t->transform(new Foo(), new Transformer\ToString());
$this->assertEquals('to_string', $actual);
}
}

@ -0,0 +1,11 @@
<?php
namespace tests\unit\fixtures;
class Foo
{
public function __toString(): string
{
return 'to_string';
}
}
Loading…
Cancel
Save