generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Strip whitespace (or other characters) from the beginning of a resolved string value. | ||
* | ||
* @see https://www.php.net/manual/function.ltrim.php | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class LeftTrim implements ParameterAttributeInterface | ||
{ | ||
/** | ||
* @param string|null $characters The list all characters that you want to be stripped. With `..` you can specify | ||
* a range of characters. | ||
*/ | ||
public function __construct( | ||
public readonly ?string $characters = null, | ||
) { | ||
} | ||
|
||
public function getResolver(): string | ||
{ | ||
return LeftTrimResolver::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\Result; | ||
|
||
final class LeftTrimResolver implements ParameterAttributeResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly ?string $characters = null, | ||
) { | ||
} | ||
|
||
public function getParameterValue( | ||
ParameterAttributeInterface $attribute, | ||
ParameterAttributeResolveContext $context | ||
): Result { | ||
if (!$attribute instanceof LeftTrim) { | ||
throw new UnexpectedAttributeException(LeftTrim::class, $attribute); | ||
} | ||
|
||
if (!$context->isResolved()) { | ||
return Result::fail(); | ||
} | ||
|
||
$resolvedValue = $context->getResolvedValue(); | ||
if (!is_string($resolvedValue)) { | ||
return Result::fail(); | ||
} | ||
|
||
$characters = $attribute->characters ?? $this->characters; | ||
|
||
return Result::success( | ||
$characters === null ? ltrim($resolvedValue) : ltrim($resolvedValue, $characters) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Strip whitespace (or other characters) from the end of a resolved string value. | ||
* | ||
* @see https://www.php.net/manual/function.rtrim.php | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class RightTrim implements ParameterAttributeInterface | ||
{ | ||
/** | ||
* @param string|null $characters The list all characters that you want to be stripped. With `..` you can specify | ||
* a range of characters. | ||
*/ | ||
public function __construct( | ||
public readonly ?string $characters = null, | ||
) { | ||
} | ||
|
||
public function getResolver(): string | ||
{ | ||
return RightTrimResolver::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\Result; | ||
|
||
final class RightTrimResolver implements ParameterAttributeResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly ?string $characters = null, | ||
) { | ||
} | ||
|
||
public function getParameterValue( | ||
ParameterAttributeInterface $attribute, | ||
ParameterAttributeResolveContext $context | ||
): Result { | ||
if (!$attribute instanceof RightTrim) { | ||
throw new UnexpectedAttributeException(RightTrim::class, $attribute); | ||
} | ||
|
||
if (!$context->isResolved()) { | ||
return Result::fail(); | ||
} | ||
|
||
$resolvedValue = $context->getResolvedValue(); | ||
if (!is_string($resolvedValue)) { | ||
return Result::fail(); | ||
} | ||
|
||
$characters = $attribute->characters ?? $this->characters; | ||
|
||
return Result::success( | ||
$characters === null ? rtrim($resolvedValue) : rtrim($resolvedValue, $characters) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Strip whitespace (or other characters) from the beginning and end of a resolved string value. | ||
* | ||
* @see https://www.php.net/manual/function.trim.php | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class Trim implements ParameterAttributeInterface | ||
{ | ||
/** | ||
* @param string|null $characters The list all characters that you want to be stripped. With `..` you can specify | ||
* a range of characters. | ||
*/ | ||
public function __construct( | ||
public readonly ?string $characters = null, | ||
) { | ||
} | ||
|
||
public function getResolver(): string | ||
{ | ||
return TrimResolver::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Attribute\Parameter; | ||
|
||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\Result; | ||
|
||
final class TrimResolver implements ParameterAttributeResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly ?string $characters = null, | ||
) { | ||
} | ||
|
||
public function getParameterValue( | ||
ParameterAttributeInterface $attribute, | ||
ParameterAttributeResolveContext $context | ||
): Result { | ||
if (!$attribute instanceof Trim) { | ||
throw new UnexpectedAttributeException(Trim::class, $attribute); | ||
} | ||
|
||
if (!$context->isResolved()) { | ||
return Result::fail(); | ||
} | ||
|
||
$resolvedValue = $context->getResolvedValue(); | ||
if (!is_string($resolvedValue)) { | ||
return Result::fail(); | ||
} | ||
|
||
$characters = $attribute->characters ?? $this->characters; | ||
|
||
return Result::success( | ||
$characters === null ? trim($resolvedValue) : trim($resolvedValue, $characters) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\Attribute\Parameter; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use Yiisoft\Hydrator\ArrayData; | ||
use Yiisoft\Hydrator\Attribute\Parameter\LeftTrim; | ||
use Yiisoft\Hydrator\Attribute\Parameter\LeftTrimResolver; | ||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\AttributeHandling\ResolverFactory\ContainerAttributeResolverFactory; | ||
use Yiisoft\Hydrator\Hydrator; | ||
use Yiisoft\Hydrator\Result; | ||
use Yiisoft\Hydrator\Tests\Support\Attribute\Counter; | ||
use Yiisoft\Hydrator\Tests\Support\Attribute\CounterResolver; | ||
use Yiisoft\Hydrator\Tests\Support\Classes\CounterClass; | ||
use Yiisoft\Hydrator\Tests\Support\TestHelper; | ||
use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
||
final class LeftTrimTest extends TestCase | ||
{ | ||
public static function dataBase(): iterable | ||
{ | ||
yield ['test ', new LeftTrim(), ' test ']; | ||
yield [' test ', new LeftTrim('t'), ' test ']; | ||
yield ['est', new LeftTrim('t'), 'test']; | ||
} | ||
|
||
#[DataProvider('dataBase')] | ||
public function testBase(string $expected, LeftTrim $attribute, mixed $value): void | ||
{ | ||
$resolver = new LeftTrimResolver(); | ||
$context = new ParameterAttributeResolveContext( | ||
TestHelper::getFirstParameter(static fn(?string $a) => null), | ||
Result::success($value), | ||
new ArrayData(), | ||
); | ||
|
||
$result = $resolver->getParameterValue($attribute, $context); | ||
|
||
$this->assertTrue($result->isResolved()); | ||
$this->assertEquals($expected, $result->getValue()); | ||
} | ||
|
||
public function testWithHydrator(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
$object = new class () { | ||
#[LeftTrim] | ||
public ?string $a = null; | ||
}; | ||
|
||
$hydrator->hydrate($object, ['a' => ' hello ']); | ||
|
||
$this->assertSame('hello ', $object->a); | ||
} | ||
|
||
public function testNotResolve(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
$object = new class () { | ||
#[LeftTrim] | ||
public ?string $a = null; | ||
}; | ||
|
||
$hydrator->hydrate($object, ['a' => new stdClass()]); | ||
|
||
$this->assertNull($object->a); | ||
} | ||
|
||
public function testNotResolvedValue(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
$object = new class () { | ||
#[LeftTrim] | ||
public ?string $a = null; | ||
}; | ||
|
||
$hydrator->hydrate($object, ['b' => ' test ']); | ||
|
||
$this->assertNull($object->a); | ||
} | ||
|
||
public function testUnexpectedAttributeException(): void | ||
{ | ||
$hydrator = new Hydrator( | ||
attributeResolverFactory: new ContainerAttributeResolverFactory( | ||
new SimpleContainer([ | ||
CounterResolver::class => new LeftTrimResolver(), | ||
]), | ||
), | ||
); | ||
$object = new CounterClass(); | ||
|
||
$this->expectException(UnexpectedAttributeException::class); | ||
$this->expectExceptionMessage( | ||
'Expected "' . LeftTrim::class . '", but "' . Counter::class . '" given.' | ||
); | ||
$hydrator->hydrate($object); | ||
} | ||
|
||
public function testOverrideDefaultCharacters(): void | ||
{ | ||
$hydrator = new Hydrator( | ||
attributeResolverFactory: new ContainerAttributeResolverFactory( | ||
new SimpleContainer([ | ||
LeftTrimResolver::class => new LeftTrimResolver(characters: '_-'), | ||
]), | ||
), | ||
); | ||
$object = new class () { | ||
#[LeftTrim(characters: '*')] | ||
public ?string $a = null; | ||
}; | ||
|
||
$hydrator->hydrate($object, ['a' => '*test*']); | ||
|
||
$this->assertSame('test*', $object->a); | ||
} | ||
} |
Oops, something went wrong.