Failed Conditions
Push — master ( 45d676...509703 )
by Sébastien
05:56
created

ThumbParams::withSeekTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Video\Adapter\FFMpegCLIValueInterface;
8
use Soluble\MediaTools\Video\Exception\InvalidArgumentException;
9
use Soluble\MediaTools\Video\Filter\Type\VideoFilterInterface;
10
11
class ThumbParams implements ThumbParamsInterface
12
{
13
    /** @var array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> */
14
    protected $params = [];
15
16
    /**
17
     * @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params
18
     *
19
     * @throws InvalidArgumentException in case of unsupported option
20
     */
21
    public function __construct($params = [])
22
    {
23
        $this->ensureSupportedParams($params);
24
        $this->params = $params;
25
    }
26
27
    public function withSeekTime(SeekTime $seekTime): self
28
    {
29
        return new self(array_merge($this->params, [
30
            self::PARAM_SEEK_TIME => $seekTime,
31
        ]));
32
    }
33
34
    public function withVideoFilter(VideoFilterInterface $videoFilter): self
35
    {
36
        return new self(array_merge($this->params, [
37
            self::PARAM_VIDEO_FILTER => $videoFilter,
38
        ]));
39
    }
40
41
    /**
42
     * Set the underlying encoder quality scale. (-qscale:v <int>, alias to -q:v <int>).
43
     *
44
     * @param int $qualityScale a number interpreted by the encoder, generally 1-5
45
     */
46
    public function withQualityScale(int $qualityScale): self
47
    {
48
        return new self(array_merge($this->params, [
49
            self::PARAM_QUALITY_SCALE => $qualityScale,
50
        ]));
51
    }
52
53
    /**
54
     * Add with overwrite option (default).
55
     *
56
     * @see self::withNoOverwrite()
57
     */
58
    public function withOverwrite(): self
59
    {
60
        return new self(array_merge($this->params, [
61
            self::PARAM_OVERWRITE => true
62
        ]));
63
    }
64
65
    /**
66
     * Add protection against output file overwriting.
67
     *
68
     * @see self::witoOverwrite()
69
     */
70
    public function withNoOverwrite(): self
71
    {
72
        return new self(array_merge($this->params, [
73
            self::PARAM_OVERWRITE => false
74
        ]));
75
    }
76
77
    public function withOutputFormat(string $outputFormat): self
78
    {
79
        return new self(array_merge($this->params, [
80
            self::PARAM_OUTPUT_FORMAT => $outputFormat,
81
        ]));
82
    }
83
84
    /**
85
     * Set a built-in param...
86
     *
87
     * @param bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface $paramValue
88
     *
89
     * @throws InvalidArgumentException in case of unsupported builtin param
90
     *
91
     * For static analysis the trick is to return 'self' instead of interface
92
     *
93
     * @return self
94
     */
95
    public function withBuiltInParam(string $paramName, $paramValue): ThumbParamsInterface
96
    {
97
        return new self(array_merge($this->params, [
98
            $paramName => $paramValue,
99
        ]));
100
    }
101
102
    /**
103
     * Return the internal array holding params.
104
     *
105
     * @return array<string,bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface>
106
     */
107
    public function toArray(): array
108
    {
109
        return $this->params;
110
    }
111
112
    public function isParamValid(string $paramName): bool
113
    {
114
        return in_array($paramName, self::BUILTIN_PARAMS, true);
115
    }
116
117
    /**
118
     * @param bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface|null $defaultValue if param does not exists set this one
119
     *
120
     * @return bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface|null
121
     */
122
    public function getParam(string $paramName, $defaultValue = null)
123
    {
124
        return $this->params[$paramName] ?? $defaultValue;
125
    }
126
127
    public function hasParam(string $paramName): bool
128
    {
129
        return array_key_exists($paramName, $this->params);
130
    }
131
132
    /**
133
     * Ensure that all params are supported.
134
     *
135
     * @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params
136
     *
137
     * @throws InvalidArgumentException in case of unsupported option
138
     */
139
    protected function ensureSupportedParams(array $params): void
140
    {
141
        foreach ($params as $paramName => $paramValue) {
142
            if (!$this->isParamValid($paramName)) {
143
                throw new InvalidArgumentException(
144
                    sprintf('Unsupported param "%s" given.', $paramName)
145
                );
146
            }
147
        }
148
    }
149
}
150