zend-blog-3-backend

Форк
0
115 строк · 2.4 Кб
1
<?php
2

3
namespace App\Model;
4

5
use App\Service\ImageManager;
6

7
class SrcSet
8
{
9
    private ?SrcSetItem $origin = null;
10
    private ?SrcSetItem $webp = null;
11
    private ?SrcSetItem $avif = null;
12

13
    /**
14
     * @return SrcSetItem|null
15
     */
16
    public function getOrigin(): ?SrcSetItem
17
    {
18
        return $this->origin;
19
    }
20

21
    /**
22
     * @param array $items
23
     *
24
     * @return SrcSet
25
     */
26
    public function setOrigin(array $items): SrcSet
27
    {
28
        $this->origin = new SrcSetItem($items);
29

30
        return $this;
31
    }
32

33
    /**
34
     * @return SrcSetItem|null
35
     */
36
    public function getWebp(): ?SrcSetItem
37
    {
38
        return $this->webp;
39
    }
40

41
    /**
42
     * @param array $items
43
     *
44
     * @return SrcSet
45
     */
46
    public function setWebp(array $items): SrcSet
47
    {
48
        $this->webp = new SrcSetItem($items);
49
        if ($this->origin && !$this->isSizeSmaller($items)) {
50
            $this->webp = null;
51
        }
52

53
        return $this;
54
    }
55

56
    /**
57
     * @return SrcSetItem|null
58
     */
59
    public function getAvif(): ?SrcSetItem
60
    {
61
        return $this->avif;
62
    }
63

64
    /**
65
     * @param array $items
66
     *
67
     * @return SrcSet
68
     */
69
    public function setAvif(array $items): SrcSet
70
    {
71
        $this->avif = new SrcSetItem($items);
72
        if ($this->origin && !$this->isSizeSmaller($items)) {
73
            $this->avif = null;
74
        }
75

76
        return $this;
77
    }
78

79
    public function isSizeSmaller(array $items)
80
    {
81
        if (!$this->origin) {
82
            return true;
83
        }
84

85
        $firstItemSize = 0;
86
        foreach ($items as $item) {
87
            $firstItemSize = filesize(ImageManager::getUploadsDir() . '/' . $item['path']);
88
            break;
89
        }
90

91
        $originItemSize = 0;
92
        foreach ($this->origin->getItems() as $item) {
93
            $originItemSize = filesize(ImageManager::getUploadsDir() . '/' . $item['path']);
94
            break;
95
        }
96

97
        return $firstItemSize < $originItemSize;
98
    }
99

100
    public function toArray(): array
101
    {
102
        $data = [];
103
        if ($this->origin && count($this->origin->getItems())) {
104
            $data['origin'] = $this->origin->toArray();
105
        }
106
        if ($this->avif && count($this->avif->getItems())) {
107
            $data['avif'] = $this->avif->toArray();
108
        }
109
        if ($this->webp && count($this->webp->getItems())) {
110
            $data['webp'] = $this->webp->toArray();
111
        }
112

113
        return $data;
114
    }
115
}
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.