AbstractImagesEvent::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\Events\Image;
4
5
use CultuurNet\UDB3\Media\Image;
6
use CultuurNet\UDB3\Media\ImageCollection;
7
use CultuurNet\UDB3\Offer\Events\AbstractEvent;
8
9
abstract class AbstractImagesEvent extends AbstractEvent
10
{
11
    /**
12
     * @var ImageCollection
13
     */
14
    protected $images;
15
16
    final public function __construct(string $eventId, ImageCollection $images)
17
    {
18
        parent::__construct($eventId);
19
        $this->images = $images;
20
    }
21
22
    public function getImages(): ImageCollection
23
    {
24
        return $this->images;
25
    }
26
27
    public function serialize(): array
28
    {
29
        $serializedData =  parent::serialize() + array(
30
            'images' => array_map(
31
                function (Image $image) {
32
                    return $image->serialize();
33
                },
34
                $this->images->toArray()
35
            ),
36
        );
37
38
        $mainImage = $this->images->getMain();
39
        if ($mainImage) {
40
            $serializedData[] = $mainImage->serialize();
41
        }
42
43
        return $serializedData;
44
    }
45
46
    public static function deserialize(array $data): AbstractImagesEvent
47
    {
48
        $images = ImageCollection::fromArray(
49
            array_map(
50
                function ($imageData) {
51
                    return Image::deserialize($imageData);
52
                },
53
                $data['images']
54
            )
55
        );
56
57
        return new static(
58
            $data['item_id'],
59
            isset($data['main_image'])
60
                ? $images->withMain(Image::deserialize($data['main_image']))
61
                : $images
62
        );
63
    }
64
}
65