0 follower

Final Class Yiisoft\Hydrator\Attribute\Parameter\ToArrayOfStringsResolver

InheritanceYiisoft\Hydrator\Attribute\Parameter\ToArrayOfStringsResolver
ImplementsYiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface

Method Details

Hide inherited methods

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 ToArrayOfStrings) {
        throw new UnexpectedAttributeException(ToArrayOfStrings::class, $attribute);
    }
    if (!$context->isResolved()) {
        return Result::fail();
    }
    $resolvedValue = $context->getResolvedValue();
    if (is_iterable($resolvedValue)) {
        $array = array_map(
            $this->castValueToString(...),
            $resolvedValue instanceof Traversable ? iterator_to_array($resolvedValue) : $resolvedValue
        );
    } else {
        $value = $this->castValueToString($resolvedValue);
        /**
         * @var string[] $array We assume valid regular expression is used here, so `preg_split()` always returns
         * an array of strings.
         */
        $array = $attribute->splitResolvedValue
            ? preg_split('~' . $attribute->separator . '~u', $value)
            : [$value];
    }
    if ($attribute->trim) {
        $array = array_map(trim(...), $array);
    }
    if ($attribute->removeEmpty) {
        $array = array_filter(
            $array,
            static fn(string $value): bool => $value !== '',
        );
    }
    return Result::success($array);
}