Completed
Pull Request — master (#349)
by Luc
05:13
created

LabelsImported::deserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Organizer\Events;
4
5
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Label;
6
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\LabelName;
7
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Labels;
8
9
class LabelsImported extends OrganizerEvent
10
{
11
    /**
12
     * @var Labels
13
     */
14
    private $labels;
15
16
    /**
17
     * @param string $organizerId
18
     * @param Labels $labels
19
     */
20
    public function __construct(
21
        $organizerId,
22
        Labels $labels
23
    ) {
24
        parent::__construct($organizerId);
25
        $this->labels = $labels;
26
    }
27
28
    /**
29
     * @return Labels
30
     */
31
    public function getLabels()
32
    {
33
        return $this->labels;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function deserialize(array $data)
40
    {
41
        $labels = new Labels();
42
        foreach ($data['labels'] as $label) {
43
            $labels = $labels->with(new Label(
44
                new LabelName($label['label']),
45
                $label['visibility']
46
            ));
47
        }
48
49
        return new static(
50
            $data['organizer_id'],
51
            $labels
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function serialize()
59
    {
60
        $labels = [];
61
        foreach ($this->getLabels() as $label) {
62
            /** @var Label $label */
63
            $labels[] = [
64
                'label' => $label->getName()->toString(),
65
                'visibility' => $label->isVisible(),
66
            ];
67
        }
68
69
        return parent::serialize() + [
70
            'labels' => $labels,
71
        ];
72
    }
73
}
74