Completed
Push — master ( ae9c65...55772a )
by Asmir
07:43 queued 05:39
created

PropelCollectionHandler::deserializeCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 2
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 JMS\Serializer\Context;
22
use JMS\Serializer\GraphNavigator;
23
use JMS\Serializer\VisitorInterface;
24
use PropelCollection;
25
26
class PropelCollectionHandler implements SubscribingHandlerInterface
27
{
28 28
    public static function getSubscribingMethods()
29
    {
30 28
        $methods = array();
31 28
        $formats = array('json', 'xml', 'yml');
32
        //Note: issue when handling inheritance
33
        $collectionTypes = array(
34 28
            'PropelCollection',
35
            'PropelObjectCollection',
36
            'PropelArrayCollection',
37
            'PropelOnDemandCollection'
38
        );
39
40 28
        foreach ($collectionTypes as $type) {
41 28
            foreach ($formats as $format) {
42 28
                $methods[] = array(
43 28
                    'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
44 28
                    'type' => $type,
45 28
                    'format' => $format,
46 28
                    'method' => 'serializeCollection',
47
                );
48
49 28
                $methods[] = array(
50 28
                    'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
51 28
                    'type' => $type,
52 28
                    'format' => $format,
53 28
                    'method' => 'deserializeCollection',
54
                );
55
            }
56
        }
57
58 28
        return $methods;
59
    }
60
61 1
    public function serializeCollection(VisitorInterface $visitor, PropelCollection $collection, array $type, Context $context)
62
    {
63
        // We change the base type, and pass through possible parameters.
64 1
        $type['name'] = 'array';
65
66 1
        return $visitor->visitArray($collection->getData(), $type, $context);
67
    }
68
69
    public function deserializeCollection(VisitorInterface $visitor, $data, array $type, Context $context)
70
    {
71
        // See above. Set parameter type to PropelCollection<T> or PropelCollection<K,V>
72
        $type['name'] = 'array';
73
74
        $collection = new PropelCollection();
75
        $collection->setData($visitor->visitArray($data, $type, $context));
76
77
        return $collection;
78
    }
79
}
80