Passed
Pull Request — master (#2)
by Vitaliy
03:08 queued 36s
created

SerializationContextCollector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the FiveLab Resource package
7
 *
8
 * (c) FiveLab
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace FiveLab\Component\Resource\Serializer\Context\Collector;
15
16
use FiveLab\Component\Resource\Serializer\Context\MutableSerializationContext;
17
use FiveLab\Component\Resource\Serializer\Context\ResourceSerializationContext;
18
19
/**
20
 * Chain collector for collect serialization context.
21
 *
22
 * @author Vitaliy Zhuk <[email protected]>
23
 */
24
class SerializationContextCollector implements SerializationContextCollectorInterface
25
{
26
    /**
27
     * @var \SplQueue<SerializationContextCollectorInterface>
28
     */
29
    private \SplQueue $collectors;
30
31
    /**
32
     * Constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->collectors = new \SplQueue();
37
    }
38
39
    /**
40
     * Add the collector to chain
41
     *
42
     * @param SerializationContextCollectorInterface $collector
43
     */
44
    public function add(SerializationContextCollectorInterface $collector): void
45
    {
46
        $this->collectors->enqueue($collector);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function collect(): ResourceSerializationContext
53
    {
54
        $serializationContext = new MutableSerializationContext([]);
55
56
        foreach ($this->collectors as $collector) {
57
            $context = $collector->collect();
58
            $serializationContext = $serializationContext->merge($context);
59
        }
60
61
        return $serializationContext->copy();
62
    }
63
}
64