AddDataSubscriber::supportsClass()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[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
namespace Xabbuh\XApi\Serializer\Event;
13
14
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
15
use JMS\Serializer\EventDispatcher\PreDeserializeEvent;
16
17
/**
18
 * Event subscriber adding data to objects being deserialized.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
abstract class AddDataSubscriber implements EventSubscriberInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public static function getSubscribedEvents()
28
    {
29
        return array(
30
            array(
31
                'event' => 'serializer.pre_deserialize',
32
                'method' => 'onPreDeserialize',
33
            ),
34
        );
35
    }
36
37
    public function onPreDeserialize(PreDeserializeEvent $event)
38
    {
39
        $type = $event->getType();
40
41
        if ($this->supportsClass($type['name'])) {
42
            $data = $event->getData();
43
44
            if ($this->isDataModificationNeeded($data)) {
45
                $data = array_merge($data, $this->getAdditionalData());
46
            }
47
48
            $event->setData($data);
49
        }
50
    }
51
52
    abstract protected function supportsClass($class);
53
54
    abstract protected function isDataModificationNeeded($data);
55
56
    abstract protected function getAdditionalData();
57
}
58