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

Stream::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
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 Stream implements \Countable
17
{
18
    private $stream;
19
20
    /**
21
     * @param array $stream
22
     */
23
    public function __construct(array  $stream)
24
    {
25
        $this->stream = $stream;
26
27
        return $this;
28
    }
29
30
    /**
31
     * @param array $attr
32
     * @param bool $string
33
     * @return mixed
34
     */
35
    public function get($attr = ['*'], $string = true)
36
    {
37
        if (!is_array($attr)) {
0 ignored issues
show
introduced by
The condition is_array($attr) is always true.
Loading history...
38
            $attr = array($attr);
39
        }
40
41
        $out = array_filter($this->stream, function ($key) use ($attr) {
42
            return in_array($key, $attr) || current($attr) === "*";
43
        }, ARRAY_FILTER_USE_KEY);
44
45
        return ($string) ? implode(',', $out) : $out;
46
    }
47
48
    /**
49
     * @param $attr
50
     * @param bool $string
51
     * @return mixed
52
     */
53
    public function except($attr, $string = false)
54
    {
55
        if (!is_array($attr)) {
56
            $attr = array($attr);
57
        }
58
59
        $out = array_filter($this->stream, function ($key) use ($attr) {
60
            return !in_array($key, $attr);
61
        }, ARRAY_FILTER_USE_KEY);
62
63
        return ($string) ? implode(',', $out) : $out;
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function isAudio()
70
    {
71
        return $this->get('@type', true) === 'Audio';
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

71
        return $this->get(/** @scrutinizer ignore-type */ '@type', true) === 'Audio';
Loading history...
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function isVideo()
78
    {
79
        return $this->get('@type', true) === 'Video';
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

79
        return $this->get(/** @scrutinizer ignore-type */ '@type', true) === 'Video';
Loading history...
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function keys()
86
    {
87
        return array_keys($this->stream);
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function all()
94
    {
95
        return $this->stream;
96
    }
97
98
    /**
99
     * @param $attr
100
     * @return bool
101
     */
102
    public function has($attr)
103
    {
104
        return isset($this->stream[$attr]);
105
    }
106
107
    /**
108
     * @return int
109
     */
110
    public function count()
111
    {
112
        return count($this->stream);
113
    }
114
}