Completed
Pull Request — master (#869)
by Asmir
10:42
created

DisjunctExclusionStrategy   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 60
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addStrategy() 0 4 1
A __construct() 0 8 2
A shouldSkipClass() 0 11 3
A shouldSkipProperty() 0 11 3
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\Exclusion;
20
21
use JMS\Serializer\Context;
22
use JMS\Serializer\Metadata\ClassMetadata;
23
use JMS\Serializer\Metadata\PropertyMetadata;
24
use PhpCollection\Sequence;
25
use PhpCollection\SequenceInterface;
26
27
/**
28
 * Disjunct Exclusion Strategy.
29
 *
30
 * This strategy is short-circuiting and will skip a class, or property as soon as one of the delegates skips it.
31
 *
32
 * @author Johannes M. Schmitt <[email protected]>
33
 */
34
class DisjunctExclusionStrategy implements ExclusionStrategyInterface
35
{
36
    /** @var \PhpCollection\SequenceInterface */
37
    private $delegates;
38
39
    /**
40
     * @param ExclusionStrategyInterface[]|SequenceInterface $delegates
41
     */
42 6
    public function __construct($delegates)
43
    {
44 6
        if (!$delegates instanceof SequenceInterface) {
45 6
            $delegates = new Sequence($delegates);
46
        }
47
48 6
        $this->delegates = $delegates;
49 6
    }
50
51
    public function addStrategy(ExclusionStrategyInterface $strategy)
52
    {
53
        $this->delegates->add($strategy);
54
    }
55
56
    /**
57
     * Whether the class should be skipped.
58
     *
59
     * @param ClassMetadata $metadata
60
     *
61
     * @return boolean
62
     */
63 3
    public function shouldSkipClass(ClassMetadata $metadata, Context $context)
64
    {
65 3
        foreach ($this->delegates as $delegate) {
66
            /** @var $delegate ExclusionStrategyInterface */
67 3
            if ($delegate->shouldSkipClass($metadata, $context)) {
68 3
                return true;
69
            }
70
        }
71
72 1
        return false;
73
    }
74
75
    /**
76
     * Whether the property should be skipped.
77
     *
78
     * @param PropertyMetadata $property
79
     *
80
     * @return boolean
81
     */
82 3
    public function shouldSkipProperty(PropertyMetadata $property, Context $context)
83
    {
84 3
        foreach ($this->delegates as $delegate) {
85
            /** @var $delegate ExclusionStrategyInterface */
86 3
            if ($delegate->shouldSkipProperty($property, $context)) {
87 3
                return true;
88
            }
89
        }
90
91 1
        return false;
92
    }
93
}
94