Completed
Push — master ( e18231...8c2386 )
by Amin
02:57
created

StreamCollection::first()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
13
namespace Streaming\MediaInfo\Streams;
14
15
16
class StreamCollection implements \Countable, \IteratorAggregate
17
{
18
    private $streams;
19
20
    /**
21
     * @param array $streams
22
     */
23
    public function __construct(array $streams)
24
    {
25
        $this->streams = $streams;
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    public function all()
32
    {
33
        return $this->streams;
34
    }
35
36
    /**
37
     * @return StreamCollection
38
     */
39
    public function audios()
40
    {
41
        $audios = array_filter($this->streams, function (Stream $stream) {
42
            return $stream->isAudio();
43
        });
44
45
        return new static(array_values($audios));
46
    }
47
48
    /**
49
     * @return StreamCollection
50
     */
51
    public function videos()
52
    {
53
        $videos =  array_filter($this->streams, function (Stream $stream) {
54
            return $stream->isVideo();
55
        });
56
57
        return new static(array_values($videos));
58
    }
59
60
    /**
61
     * @return mixed|null
62
     */
63
    public function general()
64
    {
65
        foreach ($this->streams as $stream){
66
            if ($stream instanceof Stream && $stream->get('@type') === "General"){
0 ignored issues
show
Bug introduced by
'@type' of type string is incompatible with the type array expected by parameter $attr of Streaming\MediaInfo\Streams\Stream::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            if ($stream instanceof Stream && $stream->get(/** @scrutinizer ignore-type */ '@type') === "General"){
Loading history...
67
                return $stream;
68
            }
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * @return null | Stream
76
     */
77
    public function first()
78
    {
79
        $stream = current($this->streams);
80
81
        return $stream ?: null;
82
    }
83
84
    /**
85
     * @return int
86
     */
87
    public function count()
88
    {
89
        return count($this->streams);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getIterator()
96
    {
97
        return new \ArrayIterator($this->streams);
98
    }
99
}
100