ApiPlatform\Core\Serializer\SerializerContextBuilder::createFromRequest PHP Method

createFromRequest() public method

public createFromRequest ( Request $request, boolean $normalization, array $attributes = null ) : array
$request Symfony\Component\HttpFoundation\Request
$normalization boolean
$attributes array
return array
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null) : array
    {
        if (null === $attributes) {
            $attributes = RequestAttributesExtractor::extractAttributes($request);
        }
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
        $key = $normalization ? 'normalization_context' : 'denormalization_context';
        if (isset($attributes['collection_operation_name'])) {
            $context = $resourceMetadata->getCollectionOperationAttribute($attributes['collection_operation_name'], $key, [], true);
            $context['collection_operation_name'] = $attributes['collection_operation_name'];
        } else {
            $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true);
            $context['item_operation_name'] = $attributes['item_operation_name'];
        }
        if (!$normalization && !isset($context['api_allow_update'])) {
            $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod();
        }
        $context['resource_class'] = $attributes['resource_class'];
        $context['request_uri'] = $request->getRequestUri();
        return $context;
    }

Usage Example

 public function testReuseExistingAttributes()
 {
     $expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false];
     $this->assertEquals($expected, $this->builder->createFromRequest(new Request(), false, ['resource_class' => 'Foo', 'item_operation_name' => 'get']));
 }
SerializerContextBuilder