1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Serializer; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\ResourceClassResolverInterface; |
17
|
|
|
use ApiPlatform\Core\DataProvider\PaginatorInterface; |
18
|
|
|
use ApiPlatform\Core\DataProvider\PartialPaginatorInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
21
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Base collection normalizer. |
25
|
|
|
* |
26
|
|
|
* @author Baptiste Meyer <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface |
29
|
|
|
{ |
30
|
|
|
use ContextTrait { initContext as protected; } |
31
|
|
|
use NormalizerAwareTrait; |
32
|
|
|
|
33
|
|
|
const FORMAT = self::FORMAT; |
34
|
|
|
|
35
|
|
|
protected $resourceClassResolver; |
36
|
|
|
protected $pageParameterName; |
37
|
|
|
|
38
|
|
|
public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName) |
39
|
|
|
{ |
40
|
|
|
$this->resourceClassResolver = $resourceClassResolver; |
41
|
|
|
$this->pageParameterName = $pageParameterName; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function supportsNormalization($data, $format = null) |
48
|
|
|
{ |
49
|
|
|
return static::FORMAT === $format && (is_array($data) || $data instanceof \Traversable); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
* |
55
|
|
|
* @param iterable $object |
56
|
|
|
*/ |
57
|
|
|
public function normalize($object, $format = null, array $context = []) |
58
|
|
|
{ |
59
|
|
|
$data = []; |
60
|
|
View Code Duplication |
if (isset($context['api_sub_level'])) { |
|
|
|
|
61
|
|
|
foreach ($object as $index => $obj) { |
62
|
|
|
$data[$index] = $this->normalizer->normalize($obj, $format, $context); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $data; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$context = $this->initContext( |
69
|
|
|
$this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true), |
70
|
|
|
$context |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return array_merge_recursive( |
74
|
|
|
$data, |
75
|
|
|
$this->getPaginationData($object, $context), |
76
|
|
|
$this->getItemsData($object, $format, $context) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the pagination configuration. |
82
|
|
|
* |
83
|
|
|
* @param iterable $object |
84
|
|
|
* @param array $context |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function getPaginationConfig($object, array $context = []): array |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$currentPage = $lastPage = $itemsPerPage = $pageTotalItems = $totalItems = null; |
91
|
|
|
|
92
|
|
|
if ($paginated = $paginator = $object instanceof PartialPaginatorInterface) { |
93
|
|
View Code Duplication |
if ($object instanceof PaginatorInterface) { |
|
|
|
|
94
|
|
|
$paginated = 1. !== $lastPage = $object->getLastPage(); |
95
|
|
|
$totalItems = $object->getTotalItems(); |
96
|
|
|
} else { |
97
|
|
|
$pageTotalItems = (float) count($object); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$currentPage = $object->getCurrentPage(); |
101
|
|
|
$itemsPerPage = $object->getItemsPerPage(); |
102
|
|
|
} elseif (is_array($object) || $object instanceof \Countable) { |
103
|
|
|
$totalItems = count($object); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets the pagination data. |
111
|
|
|
* |
112
|
|
|
* @param iterable $object |
113
|
|
|
* @param array $context |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
abstract protected function getPaginationData($object, array $context = []): array; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets items data. |
121
|
|
|
* |
122
|
|
|
* @param iterable $object |
123
|
|
|
* @param string|null $format |
124
|
|
|
* @param array $context |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
abstract protected function getItemsData($object, string $format = null, array $context = []): array; |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.