-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathFlexibleEntityDenormalizer.php
64 lines (56 loc) · 1.77 KB
/
FlexibleEntityDenormalizer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of Pomm's SymfonyBidge package.
*
* (c) 2017 Grégoire HUBERT <hubert.greg@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PommProject\SymfonyBridge\Serializer\Normalizer;
use PommProject\Foundation\Pomm;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Denormalizer for flexible entities.
*
* @package PommSymfonyBridge
* @copyright 2017 Grégoire HUBERT
* @author Nicolas Joseph
* @license X11 {@link http://opensource.org/licenses/mit-license.php}
*/
class FlexibleEntityDenormalizer implements DenormalizerInterface
{
private $pomm;
public function __construct(Pomm $pomm)
{
$this->pomm = $pomm;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (isset($context['session:name'])) {
$session = $this->pomm->getSession($context['session:name']);
} else {
$session = $this->pomm->getDefaultSession();
}
if (isset($context['model:name'])) {
$model_name = $context['model:name'];
} else {
$model_name = "${class}Model";
}
$model = $session->getModel($model_name);
return $model->createEntity($data);
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
$reflection = new \ReflectionClass($type);
$interfaces = $reflection->getInterfaces();
// @TODO Use FlexibleEntityInterface::class with php >= 5.5
return isset($interfaces['PommProject\ModelManager\Model\FlexibleEntity\FlexibleEntityInterface']);
}
}