Passed
Push — master ( 6309f1...8af892 )
by Pascal
02:12
created

InteractsWithHttpHeaders::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg;
4
5
use Illuminate\Support\Collection;
6
7
trait InteractsWithHttpHeaders
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $headers = [];
13
14
    public function getHeaders(): array
15
    {
16
        return $this->headers;
17
    }
18
19
    public function setHeaders(array $headers = []): self
20
    {
21
        $this->headers = $headers;
22
23
        return $this;
24
    }
25
26
    /**
27
     * Searches in the $input array for the key bu the $path, and then
28
     * prepend the $values in front of that key.
29
     *
30
     * @param array $input
31
     * @param string $path
32
     * @param array $values
33
     * @return array
34
     */
35
    protected static function mergeBeforePathInput(array $input, string $path, array $values = []): array
36
    {
37
        $key = array_search($path, $input) - 1;
38
39
        return static::mergeBeforeKey($input, $key, $values);
40
    }
41
42
    /**
43
     * Prepend the given $values in front of the $key in $input.
44
     *
45
     * @param array $input
46
     * @param integer $key
47
     * @param array $values
48
     * @return array
49
     */
50
    protected static function mergeBeforeKey(array $input, int $key, array $values = []): array
51
    {
52
        return array_merge(
53
            array_slice($input, 0, $key),
54
            $values,
55
            array_slice($input, $key)
56
        );
57
    }
58
59
    /**
60
     * Maps the headers into a key-value string for FFmpeg. Returns
61
     * an array of arguments to pass into the command.
62
     *
63
     * @param array $headers
64
     * @return array
65
     */
66
    public static function compileHeaders(array $headers = []): array
67
    {
68
        if (empty($headers)) {
69
            return [];
70
        }
71
72
        $headers = Collection::make($headers)->map(function ($value, $key) {
73
            return "{$key}: {$value}";
74
        })->filter()->push("")->implode("\r\n");
75
76
        return ['-headers', $headers];
77
    }
78
}
79