добавил toString трансформер
This commit is contained in:
parent
ef4dd7d183
commit
79694fb346
14
src/Transformer/ToString.php
Normal file
14
src/Transformer/ToString.php
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
26
src/Transformer/ToStringTransformer.php
Normal file
26
src/Transformer/ToStringTransformer.php
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
tests/unit/ToStringTransformTest.php
Normal file
22
tests/unit/ToStringTransformTest.php
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
11
tests/unit/fixtures/Foo.php
Normal file
11
tests/unit/fixtures/Foo.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace tests\unit\fixtures;
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return 'to_string';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user