-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemDenormalizer.php
More file actions
62 lines (49 loc) · 1.96 KB
/
ItemDenormalizer.php
File metadata and controls
62 lines (49 loc) · 1.96 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
<?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\GraphQl\Serializer;
use ApiPlatform\Serializer\AbstractItemNormalizer;
/**
* Converts GraphQL inputs to objects (denormalization only).
*
* @author Kévin Dunglas <[email protected]>
*/
final class ItemDenormalizer extends AbstractItemNormalizer
{
public const FORMAT = 'graphql';
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return false;
}
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
}
public function getSupportedTypes(?string $format): array
{
return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
}
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
{
$allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
if (($context['api_denormalize'] ?? false) && \is_array($allowedAttributes) && false !== ($indexId = array_search('id', $allowedAttributes, true))) {
$allowedAttributes[] = '_id';
array_splice($allowedAttributes, (int) $indexId, 1);
}
return $allowedAttributes;
}
protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
{
if ('_id' === $attribute) {
$attribute = 'id';
}
parent::setAttributeValue($object, $attribute, $value, $format, $context);
}
}