FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
serializer/AbstractCollectionNormalizer.php at main · api-platform/serializer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
api-platform
/
serializer
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
0
Star
30
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
serializer
/
AbstractCollectionNormalizer.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
161 lines (135 loc) · 5.42 KB
Breadcrumbs
serializer
/
AbstractCollectionNormalizer.php
Copy path
File metadata and controls
161 lines (135 loc) · 5.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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
;
use
ApiPlatform
\
Metadata
\
Operation
;
use
ApiPlatform
\
Metadata
\
Resource
\
Factory
\
ResourceMetadataCollectionFactoryInterface
;
use
ApiPlatform
\
Metadata
\
ResourceClassResolverInterface
;
use
ApiPlatform
\
State
\
Pagination
\
PaginatorInterface
;
use
ApiPlatform
\
State
\
Pagination
\
PartialPaginatorInterface
;
use
Symfony
\
Component
\
Serializer
\
Normalizer
\
NormalizerAwareInterface
;
use
Symfony
\
Component
\
Serializer
\
Normalizer
\
NormalizerAwareTrait
;
use
Symfony
\
Component
\
Serializer
\
Normalizer
\
NormalizerInterface
;
use
Symfony
\
Component
\
Serializer
\
Serializer
;
/**
* Base collection normalizer.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
abstract
class
AbstractCollectionNormalizer
implements
NormalizerInterface, NormalizerAwareInterface
{
use
ContextTrait {
initContext
as
protected
;
}
use
NormalizerAwareTrait;
use
OperationContextTrait;
/**
* This constant must be overridden in the child class.
*/
public
const
FORMAT
=
'
to-override
'
;
public
function
__construct
(
protected
ResourceClassResolverInterface
$
resourceClassResolver
,
protected
string
$
pageParameterName
,
protected
?
ResourceMetadataCollectionFactoryInterface
$
resourceMetadataFactory
=
null
)
{
}
/**
* {@inheritdoc}
*/
public
function
supportsNormalization
(
mixed
$
data
, ?
string
$
format
=
null
,
array
$
context
= []):
bool
{
return
static
::
FORMAT
===
$
format
&&
is_iterable
(
$
data
);
}
/**
* {@inheritdoc}
*/
public
function
getSupportedTypes
(?
string
$
format
):
array
{
/*
* At this point, support anything that is_iterable(), i.e. array|Traversable
* for non-objects, symfony uses 'native-'.\gettype($data) :
* https://github.com/tucksaun/symfony/blob/400685a68b00b0932f8ef41096578872b643099c/src/Symfony/Component/Serializer/Serializer.php#L254
*/
if
(
static
::
FORMAT
===
$
format
) {
return
[
'
native-array
'
=>
true
,
'
\Traversable
'
=>
true
,
];
}
return
[];
}
/**
* {@inheritdoc}
*
* @param iterable $data
*/
public
function
normalize
(
mixed
$
data
, ?
string
$
format
=
null
,
array
$
context
= []):
array
|
string
|
int
|
float
|
bool
|
\
ArrayObject
|
null
{
if
(!
isset
(
$
context
[
'
resource_class
'
]) ||
isset
(
$
context
[
'
api_sub_level
'
])) {
return
$
this
->
normalizeRawCollection
(
$
data
,
$
format
,
$
context
);
}
$
resourceClass
=
$
this
->
resourceClassResolver
->
getResourceClass
(
$
data
,
$
context
[
'
resource_class
'
]);
$
collectionContext
=
$
this
->
initContext
(
$
resourceClass
,
$
context
);
$
normalizedData
= [];
$
paginationData
=
$
this
->
getPaginationData
(
$
data
,
$
collectionContext
);
$
childContext
=
$
this
->
createOperationContext
(
$
collectionContext
,
$
resourceClass
);
if
(
isset
(
$
collectionContext
[
'
force_resource_class
'
])) {
$
childContext
[
'
force_resource_class
'
] =
$
collectionContext
[
'
force_resource_class
'
];
}
$
itemsData
=
$
this
->
getItemsData
(
$
data
,
$
format
,
$
childContext
);
return
array_merge_recursive
(
$
normalizedData
,
$
paginationData
,
$
itemsData
);
}
/**
* Normalizes a raw collection (not API resources).
*/
protected
function
normalizeRawCollection
(
iterable
$
object
, ?
string
$
format
=
null
,
array
$
context
= []):
array
|
\
ArrayObject
{
if
([] ===
$
object
&& (
$
context
[Serializer::
EMPTY_ARRAY_AS_OBJECT
] ??
false
)) {
return
new
\
ArrayObject
();
}
$
data
= [];
foreach
(
$
object
as
$
index
=>
$
obj
) {
$
data
[
$
index
] =
$
this
->
normalizer
->
normalize
(
$
obj
,
$
format
,
$
context
);
}
return
$
data
;
}
/**
* Gets the pagination configuration.
*/
protected
function
getPaginationConfig
(
iterable
$
object
,
array
$
context
= []):
array
{
$
currentPage
=
$
lastPage
=
$
itemsPerPage
=
$
pageTotalItems
=
$
totalItems
=
null
;
$
paginated
=
$
paginator
=
false
;
if
(
$
object
instanceof
PartialPaginatorInterface) {
$
paginated
=
$
paginator
=
true
;
if
(
$
object
instanceof
PaginatorInterface) {
$
paginated
=
1.
!==
$
lastPage
=
$
object
->
getLastPage
();
$
totalItems
=
$
object
->
getTotalItems
();
}
else
{
$
pageTotalItems
= (
float
)
\count
(
$
object
);
}
$
currentPage
=
$
object
->
getCurrentPage
();
$
itemsPerPage
=
$
object
->
getItemsPerPage
();
}
elseif
(
is_countable
(
$
object
)) {
$
totalItems
=
\count
(
$
object
);
}
return
[
$
paginator
,
$
paginated
,
$
currentPage
,
$
itemsPerPage
,
$
lastPage
,
$
pageTotalItems
,
$
totalItems
];
}
protected
function
getOperation
(
array
$
context
= []):
Operation
{
$
metadata
=
$
this
->
resourceMetadataFactory
->
create
(
$
context
[
'
resource_class
'
] ??
''
);
return
$
metadata
->
getOperation
(
$
context
[
'
operation_name
'
] ??
null
);
}
/**
* Gets the pagination data.
*/
abstract
protected
function
getPaginationData
(
iterable
$
object
,
array
$
context
= []):
array
;
/**
* Gets items data.
*/
abstract
protected
function
getItemsData
(
iterable
$
object
, ?
string
$
format
=
null
,
array
$
context
= []):
array
;
}
Back
|
FazBrowse Home
|
New Git URL