-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperationContextTrait.php
50 lines (40 loc) · 1.52 KB
/
OperationContextTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Serializer;
/**
* @internal
*/
trait OperationContextTrait
{
/**
* This context is created when working on a relation context or items of a collection. It cleans the previously given
* context as the operation changes.
*/
protected function createOperationContext(array $context, ?string $resourceClass = null): array
{
if (isset($context['operation']) && !isset($context['root_operation'])) {
$context['root_operation'] = $context['operation'];
}
if (isset($context['operation_name']) || isset($context['graphql_operation_name'])) {
$context['root_operation_name'] = $context['operation_name'] ?? $context['graphql_operation_name'];
}
unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class']);
if (!$resourceClass) {
return $context;
}
if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
$context['item_uri_template'] = $operation->getItemUriTemplate();
}
unset($context['operation'], $context['operation_name']);
$context['resource_class'] = $resourceClass;
return $context;
}
}