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\Handler; |
20
|
|
|
|
21
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
22
|
|
|
use Doctrine\Common\Collections\Collection; |
23
|
|
|
use JMS\Serializer\DeserializationContext; |
24
|
|
|
use JMS\Serializer\DeserializationVisitorInterface; |
25
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
26
|
|
|
use JMS\Serializer\SerializationContext; |
27
|
|
|
use JMS\Serializer\SerializationVisitorInterface; |
28
|
|
|
|
29
|
|
|
final class ArrayCollectionHandler implements SubscribingHandlerInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $initializeExcluded = true; |
35
|
|
|
|
36
|
288 |
|
public function __construct($initializeExcluded = true) |
37
|
|
|
{ |
38
|
288 |
|
$this->initializeExcluded = $initializeExcluded; |
39
|
288 |
|
} |
40
|
|
|
|
41
|
288 |
|
public static function getSubscribingMethods() |
42
|
|
|
{ |
43
|
288 |
|
$methods = array(); |
44
|
288 |
|
$formats = array('json', 'xml', 'yml'); |
45
|
|
|
$collectionTypes = array( |
46
|
288 |
|
'ArrayCollection', |
47
|
|
|
'Doctrine\Common\Collections\ArrayCollection', |
48
|
|
|
'Doctrine\ORM\PersistentCollection', |
49
|
|
|
'Doctrine\ODM\MongoDB\PersistentCollection', |
50
|
|
|
'Doctrine\ODM\PHPCR\PersistentCollection', |
51
|
|
|
); |
52
|
|
|
|
53
|
288 |
|
foreach ($collectionTypes as $type) { |
54
|
288 |
|
foreach ($formats as $format) { |
55
|
288 |
|
$methods[] = array( |
56
|
288 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
57
|
288 |
|
'type' => $type, |
58
|
288 |
|
'format' => $format, |
59
|
288 |
|
'method' => 'serializeCollection', |
60
|
|
|
); |
61
|
|
|
|
62
|
288 |
|
$methods[] = array( |
63
|
288 |
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
64
|
288 |
|
'type' => $type, |
65
|
288 |
|
'format' => $format, |
66
|
288 |
|
'method' => 'deserializeCollection', |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
288 |
|
return $methods; |
72
|
|
|
} |
73
|
|
|
|
74
|
9 |
|
public function serializeCollection(SerializationVisitorInterface $visitor, Collection $collection, array $type, SerializationContext $context) |
75
|
|
|
{ |
76
|
|
|
// We change the base type, and pass through possible parameters. |
77
|
9 |
|
$type['name'] = 'array'; |
78
|
|
|
|
79
|
9 |
|
$context->stopVisiting($collection); |
80
|
|
|
|
81
|
9 |
|
if ($this->initializeExcluded === false) { |
82
|
|
|
$exclusionStrategy = $context->getExclusionStrategy(); |
83
|
|
|
if ($exclusionStrategy !== null && $exclusionStrategy->shouldSkipClass($context->getMetadataFactory()->getMetadataForClass(\get_class($collection)), $context)) { |
84
|
|
|
$context->startVisiting($collection); |
85
|
|
|
|
86
|
|
|
return $visitor->visitArray([], $type, $context); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
9 |
|
$result = $visitor->visitArray($collection->toArray(), $type); |
90
|
|
|
|
91
|
9 |
|
$context->startVisiting($collection); |
92
|
9 |
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
public function deserializeCollection(DeserializationVisitorInterface $visitor, $data, array $type, DeserializationContext $context) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
// See above. |
98
|
6 |
|
$type['name'] = 'array'; |
99
|
|
|
|
100
|
6 |
|
return new ArrayCollection($visitor->visitArray($data, $type)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.