Passed
Pull Request — master (#965)
by Asmir
02:38
created

DisjunctExclusionStrategy::addStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Exclusion;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\Metadata\ClassMetadata;
9
use JMS\Serializer\Metadata\PropertyMetadata;
10
11
/**
12
 * Disjunct Exclusion Strategy.
13
 *
14
 * This strategy is short-circuiting and will skip a class, or property as soon as one of the delegates skips it.
15
 *
16
 * @author Johannes M. Schmitt <[email protected]>
17
 */
18
final class DisjunctExclusionStrategy implements ExclusionStrategyInterface
19
{
20
    private $delegates = [];
21
22
    /**
23
     * @param ExclusionStrategyInterface[] $delegates
24
     */
25 6
    public function __construct(array $delegates = [])
26
    {
27 6
        $this->delegates = $delegates;
28 6
    }
29
30
    public function addStrategy(ExclusionStrategyInterface $strategy): void
31
    {
32
        $this->delegates[] = $strategy;
33
    }
34
35
    /**
36
     * Whether the class should be skipped.
37
     *
38
     * @param ClassMetadata $metadata
39
     *
40
     * @return boolean
41
     */
42 3
    public function shouldSkipClass(ClassMetadata $metadata, Context $context): bool
43
    {
44 3
        foreach ($this->delegates as $delegate) {
45
            /** @var $delegate ExclusionStrategyInterface */
46 3
            if ($delegate->shouldSkipClass($metadata, $context)) {
47 3
                return true;
48
            }
49
        }
50
51 1
        return false;
52
    }
53
54
    /**
55
     * Whether the property should be skipped.
56
     *
57
     * @param PropertyMetadata $property
58
     *
59
     * @return boolean
60
     */
61 3
    public function shouldSkipProperty(PropertyMetadata $property, Context $context): bool
62
    {
63 3
        foreach ($this->delegates as $delegate) {
64
            /** @var $delegate ExclusionStrategyInterface */
65 3
            if ($delegate->shouldSkipProperty($property, $context)) {
66 3
                return true;
67
            }
68
        }
69
70 1
        return false;
71
    }
72
}
73