Completed
Push — 2.0.x ( 8a3809 )
by Tim
08:50
created

ObjectConstructor::construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * AppserverIo\RestApi\ObjectConstructor
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/restapi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RestApi;
22
23
use JMS\Serializer\DeserializationContext;
24
use JMS\Serializer\Metadata\ClassMetadata;
25
use JMS\Serializer\VisitorInterface;
26
use JMS\Serializer\Construction\ObjectConstructorInterface;
27
use AppserverIo\Psr\Application\ApplicationInterface;
28
use AppserverIo\Lang\Reflection\ReflectionClass;
29
30
/**
31
 * Implementations of this interface construct new objects during deserialization.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2018 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/appserver-io/restapi
37
 * @link      http://www.appserver.io
38
 */
39
class ObjectConstructor implements ObjectConstructorInterface
40
{
41
42
    /**
43
     * The application instance.
44
     *
45
     * @var \AppserverIo\Psr\Application\ApplicationInterface
46
     */
47
    protected $application;
48
49
    /**
50
     * Initializes the object constructor instance with the application
51
     *
52
     * @param \AppserverIo\Psr\Application\ApplicationInterface $application
53
     */
54
    public function __construct(ApplicationInterface $application)
55
    {
56
        $this->application = $application;
57
    }
58
59
    /**
60
     * Constructs a new object.
61
     *
62
     * Implementations could for example create a new object calling "new", use
63
     * "unserialize" techniques, reflection, or other means.
64
     *
65
     * @param VisitorInterface $visitor
66
     * @param ClassMetadata $metadata
67
     * @param mixed $data
68
     * @param array $type ["name" => string, "params" => array]
69
     * @param DeserializationContext $context
70
     *
71
     * @return object
72
     */
73
    public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
74
    {
75
76
        // create a reflection class implementation to load the annotations
77
        $reflectionClass = new ReflectionClass($metadata->name);
78
79
        // try to lookup the instance in the container
80
        return $this->application->search($reflectionClass->getShortName());
81
    }
82
}
83