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);
$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);
}
Signup or Login in order to comment.