1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2016 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\Serializer\Construction; |
20
|
|
|
|
21
|
|
|
use JMS\Serializer\DeserializationContext; |
22
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
23
|
|
|
use JMS\Serializer\VisitorInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Object constructor that allows deserialization into already constructed objects passed through the deserialization context. |
27
|
|
|
*/ |
28
|
|
|
class InitializedObjectConstructor implements ObjectConstructorInterface |
29
|
|
|
{ |
30
|
|
|
private $fallbackConstructor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ObjectConstructorInterface $fallbackConstructor Fallback object constructor |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct(ObjectConstructorInterface $fallbackConstructor) |
36
|
|
|
{ |
37
|
2 |
|
$this->fallbackConstructor = $fallbackConstructor; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
2 |
|
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context) |
44
|
|
|
{ |
45
|
2 |
|
if ($context->attributes->containsKey('target') && $context->getDepth() === 1) { |
46
|
2 |
|
return $context->attributes->get('target')->get(); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|