0 follower

Final Class Yiisoft\Input\Http\Attribute\Data\FromBodyResolver

InheritanceYiisoft\Input\Http\Attribute\Data\FromBodyResolver
ImplementsYiisoft\Hydrator\Attribute\Data\DataAttributeResolverInterface

Resolver for {@see FromBody} attribute.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\RequestProvider\RequestProviderInterface $requestProvider )
$requestProvider \Yiisoft\RequestProvider\RequestProviderInterface

The request provider.

                public function __construct(
    private RequestProviderInterface $requestProvider,
) {
}

            
prepareData() public method

public \Yiisoft\Hydrator\DataInterface prepareData ( \Yiisoft\Hydrator\Attribute\Data\DataAttributeInterface $attribute, \Yiisoft\Hydrator\DataInterface $data )
$attribute \Yiisoft\Hydrator\Attribute\Data\DataAttributeInterface
$data \Yiisoft\Hydrator\DataInterface

                public function prepareData(DataAttributeInterface $attribute, DataInterface $data): DataInterface
{
    if (!$attribute instanceof FromBody) {
        throw new UnexpectedAttributeException(FromBody::class, $attribute);
    }
    $array = [];
    $parsedBody = $this->requestProvider->get()->getParsedBody();
    if (is_array($parsedBody)) {
        $name = $attribute->getName();
        if ($name === null) {
            $array = $parsedBody;
        } else {
            $value = ArrayHelper::getValueByPath($parsedBody, $name);
            if (is_array($value)) {
                $array = $value;
            }
        }
    }
    return new ArrayData($array, $attribute->getMap(), $attribute->isStrict());
}