0

Final Class Yiisoft\Hydrator\Attribute\Parameter\TrimResolver

InheritanceYiisoft\Hydrator\Attribute\Parameter\TrimResolver
ImplementsYiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface

Resolver for {@see Trim} attribute.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string|null $characters = null, boolean $multibyte = false, string|null $encoding = null )
$characters string|null

The list of characters to strip when it is not specified in the attribute. With .. you can specify a range of characters, both in the default and in the multibyte mode. This syntax is deprecated and will be removed in the next major version.

$multibyte boolean

Whether to use multibyte-aware trimming when it is not specified in the attribute.

$encoding string|null

The encoding to use in multibyte mode when it is not specified in the attribute.

                public function __construct(
    private readonly ?string $characters = null,
    private readonly bool $multibyte = false,
    private readonly ?string $encoding = null,
) {
    TrimCharacters::checkMultibyteFunctionsExist($multibyte);
}

            
getParameterValue() public method

public Yiisoft\Hydrator\Result getParameterValue ( Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface $attribute, Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext $context )
$attribute Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface
$context Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext

                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;
    $multibyte = $attribute->multibyte ?? $this->multibyte;
    $encoding = $attribute->encoding ?? $this->encoding;
    TrimCharacters::checkDeprecatedRanges($characters, $multibyte, $encoding);
    if (!$multibyte) {
        return Result::success(
            $characters === null ? trim($resolvedValue) : trim($resolvedValue, $characters),
        );
    }
    return Result::success(
        mb_trim(
            $resolvedValue,
            $characters === null ? null : TrimCharacters::expandRanges($characters, $encoding),
            $encoding,
        ),
    );
}