1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use FFMpeg\Coordinate\TimeCode; |
7
|
|
|
use FFMpeg\Media\MediaTypeInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method mixed save(FormatInterface $format, $outputPathfile) |
11
|
|
|
*/ |
12
|
|
|
class Media |
13
|
|
|
{ |
14
|
|
|
protected $file; |
15
|
|
|
|
16
|
|
|
protected $media; |
17
|
|
|
|
18
|
|
|
public function __construct(File $file, MediaTypeInterface $media) |
19
|
|
|
{ |
20
|
|
|
$this->file = $file; |
21
|
|
|
$this->media = $media; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function isFrame(): bool |
25
|
|
|
{ |
26
|
|
|
return $this instanceof Frame; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getFile(): File |
30
|
|
|
{ |
31
|
|
|
return $this->file; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function export(): MediaExporter |
35
|
|
|
{ |
36
|
|
|
return new MediaExporter($this); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getFrameFromString(string $timecode): Frame |
40
|
|
|
{ |
41
|
|
|
return $this->getFrameFromTimecode( |
42
|
|
|
TimeCode::fromString($timecode) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getFrameFromSeconds(float $quantity): Frame |
47
|
|
|
{ |
48
|
|
|
return $this->getFrameFromTimecode( |
49
|
|
|
TimeCode::fromSeconds($quantity) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): Frame |
54
|
|
|
{ |
55
|
|
|
$frame = $this->media->frame($timecode); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return new Frame($this->getFile(), $frame); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function addFilter(): Media |
61
|
|
|
{ |
62
|
|
|
$arguments = func_get_args(); |
63
|
|
|
|
64
|
|
|
if (isset($arguments[0]) && $arguments[0] instanceof Closure) { |
65
|
|
|
call_user_func_array($arguments[0], [$this->media->filters()]); |
66
|
|
|
} else { |
67
|
|
|
call_user_func_array([$this->media, 'addFilter'], $arguments); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function selfOrArgument($argument) |
74
|
|
|
{ |
75
|
|
|
return ($argument === $this->media) ? $this : $argument; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function __call($method, $parameters) |
79
|
|
|
{ |
80
|
|
|
return $this->selfOrArgument( |
81
|
|
|
call_user_func_array([$this->media, $method], $parameters) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: