1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Exclusion;
6
7use JMS\Serializer\Context;
8use JMS\Serializer\Metadata\ClassMetadata;
9use 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 <schmittjoh@gmail.com>
17 */
18final class DisjunctExclusionStrategy implements ExclusionStrategyInterface
19{
20    /**
21     * @var ExclusionStrategyInterface[]
22     */
23    private $delegates = [];
24
25    /**
26     * @param ExclusionStrategyInterface[] $delegates
27     */
28    public function __construct(array $delegates = [])
29    {
30        $this->delegates = $delegates;
31    }
32
33    public function addStrategy(ExclusionStrategyInterface $strategy): void
34    {
35        $this->delegates[] = $strategy;
36    }
37
38    /**
39     * Whether the class should be skipped.
40     */
41    public function shouldSkipClass(ClassMetadata $metadata, Context $context): bool
42    {
43        foreach ($this->delegates as $delegate) {
44            /** @var $delegate ExclusionStrategyInterface */
45            if ($delegate->shouldSkipClass($metadata, $context)) {
46                return true;
47            }
48        }
49
50        return false;
51    }
52
53    /**
54     * Whether the property should be skipped.
55     */
56    public function shouldSkipProperty(PropertyMetadata $property, Context $context): bool
57    {
58        foreach ($this->delegates as $delegate) {
59            /** @var $delegate ExclusionStrategyInterface */
60            if ($delegate->shouldSkipProperty($property, $context)) {
61                return true;
62            }
63        }
64
65        return false;
66    }
67}
68