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

DisjunctExclusionStrategy::shouldSkipClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 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