Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kohinos
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
9
Issues
9
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cooperatic-mlc
kohinos
Commits
fe99830c
Commit
fe99830c
authored
Oct 04, 2020
by
Mathieu Poisbeau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#22: API Prestataire -> le media est désormais exposé
en lecture (GET) seulement
parent
f0598687
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
services.yaml
config/services.yaml
+4
-0
ApiNormalizer.php
src/Serializer/ApiNormalizer.php
+56
-0
No files found.
config/services.yaml
View file @
fe99830c
...
...
@@ -480,3 +480,7 @@ services:
decorates
:
'
api_platform.swagger.normalizer.api_gateway'
arguments
:
[
'
@App\Swagger\SwaggerDecorator.inner'
]
autoconfigure
:
false
App\Serializer\ApiNormalizer
:
decorates
:
'
api_platform.serializer.normalizer.item'
arguments
:
[
'
@App\Serializer\ApiNormalizer.inner'
]
src/Serializer/ApiNormalizer.php
0 → 100644
View file @
fe99830c
<?php
namespace
App\Serializer
;
use
Symfony\Component\Serializer\Normalizer\NormalizerInterface
;
use
Symfony\Component\Serializer\SerializerAwareInterface
;
use
Symfony\Component\Serializer\SerializerInterface
;
use
Sonata\MediaBundle\Provider\Pool
;
use
App\Entity\Prestataire
;
/**
* This class allows to expose the media of a Prestataire in ApiPlatform
* (because it's not possible with a Media from SonataMediaBundle)
*/
final
class
ApiNormalizer
implements
NormalizerInterface
,
SerializerAwareInterface
{
private
$decorated
;
private
$pool
;
public
function
__construct
(
NormalizerInterface
$decorated
,
Pool
$pool
)
{
$this
->
decorated
=
$decorated
;
$this
->
pool
=
$pool
;
}
public
function
supportsNormalization
(
$data
,
$format
=
null
)
{
return
$this
->
decorated
->
supportsNormalization
(
$data
,
$format
);
}
public
function
normalize
(
$object
,
$format
=
null
,
array
$context
=
[])
{
$data
=
$this
->
decorated
->
normalize
(
$object
,
$format
,
$context
);
// In the case of normalizing a Prestataire...
if
(
!
$object
instanceof
Prestataire
)
{
return
$data
;
}
// ... we retrieve its associated Sonata Media (from the Sonata Media's provider, from the providers pool)
if
(
is_array
(
$data
)
&&
$media
=
$object
->
getMedia
())
{
if
(
$provider
=
$this
->
pool
->
getProvider
(
$media
->
getProviderName
()))
{
$data
[
'media'
]
=
$provider
->
getHelperProperties
(
$media
,
'reference'
);
}
}
return
$data
;
}
public
function
setSerializer
(
SerializerInterface
$serializer
)
{
if
(
$this
->
decorated
instanceof
SerializerAwareInterface
)
{
$this
->
decorated
->
setSerializer
(
$serializer
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment