Issues (177)

src/DTOs/Style/VideoStyle.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Mostafaznv\Larupload\DTOs\Style;
4
5
use Exception;
6
use FFMpeg\Format\Audio\Aac;
7
use FFMpeg\Format\Audio\Flac;
8
use FFMpeg\Format\Audio\Mp3;
9
use FFMpeg\Format\Audio\Wav;
10
use FFMpeg\Format\Video\Ogg;
11
use FFMpeg\Format\Video\WebM;
12
use FFMpeg\Format\Video\X264;
13
use Mostafaznv\Larupload\Enums\LaruploadMediaStyle;
14
15
16
class VideoStyle extends Style
17
{
18
    public readonly LaruploadMediaStyle            $mode;
19
    public readonly X264|WebM|Ogg|Mp3|Aac|Wav|Flac $format;
20
21
    public function __construct(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::SCALE_HEIGHT, X264|WebM|Ogg|Mp3|Aac|Wav|Flac $format = new X264, bool $padding = false)
22
    {
23
        parent::__construct($name, $width, $height, $padding);
24
25
        $this->mode = $mode;
0 ignored issues
show
The property mode is declared read-only in Mostafaznv\Larupload\DTOs\Style\VideoStyle.
Loading history...
26
        $this->format = $format;
0 ignored issues
show
The property format is declared read-only in Mostafaznv\Larupload\DTOs\Style\VideoStyle.
Loading history...
27
28
        $this->validateDimension();
29
    }
30
31
    public static function make(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::SCALE_HEIGHT, X264|WebM|Ogg|Mp3|Aac|Wav|Flac $format = new X264, bool $padding = false): self
32
    {
33
        return new self($name, $width, $height, $mode, $format, $padding);
34
    }
35
36
37
    public function audioFormats(): array
38
    {
39
        return [
40
            Mp3::class, Aac::class, Wav::class, Flac::class
41
        ];
42
    }
43
44
    public function isAudioFormat(): bool
45
    {
46
        return in_array(get_class($this->format), $this->audioFormats());
47
    }
48
49
    public function extension(): ?string
50
    {
51
        if ($this->format instanceof WebM) {
52
            return 'webm';
53
        }
54
        else if ($this->format instanceof Ogg) {
55
            return 'ogg';
56
        }
57
        else if ($this->format instanceof Mp3) {
58
            return 'mp3';
59
        }
60
        else if ($this->format instanceof Aac) {
61
            return 'aac';
62
        }
63
        else if ($this->format instanceof Wav) {
64
            return 'wav';
65
        }
66
        else if ($this->format instanceof Flac) {
67
            return 'flac';
68
        }
69
70
        return 'mp4';
71
    }
72
73
    private function validateDimension(): void
74
    {
75
        if ($this->isAudioFormat()) {
76
            return;
77
        }
78
79
        if ($this->mode === LaruploadMediaStyle::SCALE_HEIGHT) {
80
            if ($this->width === null or $this->width === 0) {
81
                throw new Exception(
82
                    'Width is required when you are in SCALE_HEIGHT mode'
83
                );
84
            }
85
        }
86
        else if ($this->mode === LaruploadMediaStyle::SCALE_WIDTH) {
87
            if ($this->height === null or $this->height === 0) {
88
                throw new Exception(
89
                    'Height is required when you are in SCALE_WIDTH mode'
90
                );
91
            }
92
        }
93
        else if (in_array($this->mode, [LaruploadMediaStyle::CROP, LaruploadMediaStyle::FIT])) {
94
            if (!$this->width or !$this->height) {
95
                throw new Exception(
96
                    'Width and Height are required when you are in CROP/FIT mode'
97
                );
98
            }
99
        }
100
        else if ($this->mode === LaruploadMediaStyle::AUTO) {
101
            if (!$this->width and !$this->height) {
102
                throw new Exception(
103
                    'Width and height are required when you are in auto mode'
104
                );
105
            }
106
        }
107
    }
108
}
109