Html   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 115
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A audio() 0 6 1
A getTag() 0 10 1
A video() 0 8 1
A getTrackOptions() 0 9 3
A getSourceOptions() 0 10 3
A getMainOptions() 0 10 3
1
<?php
2
3
namespace Itstructure\MFUploader\helpers;
4
5
use Itstructure\MFUploader\Module;
6
use yii\helpers\{ArrayHelper, Html as BaseHtml};
7
8
/**
9
 * HTML helper.
10
 *
11
 * @package Itstructure\MFUploader\helpers
12
 *
13
 * @author Andrey Girnik <[email protected]>
14
 */
15
class Html extends BaseHtml
16
{
17
    /**
18
     * Render html 5 audio tag structure.
19
     *
20
     * @param string $src
21
     * @param array $options
22
     *
23
     * @return string
24
     */
25
    public static function audio(string $src, array $options = []): string
26
    {
27
        return self::getTag('audio',
28
            self::getMainOptions($options),
29
            self::getSourceOptions($src, $options),
30
            self::getTrackOptions($options)
31
        );
32
    }
33
34
    /**
35
     * Render html 5 video tag structure.
36
     *
37
     * @param string $src
38
     * @param array $options
39
     *
40
     * @return string
41
     */
42
    public static function video(string $src, array $options = []): string
43
    {
44
        return self::getTag('video',
45
            ArrayHelper::merge([
46
                'height' => Module::ORIGINAL_PREVIEW_HEIGHT
47
            ], self::getMainOptions($options)),
48
            self::getSourceOptions($src, $options),
49
            self::getTrackOptions($options)
50
        );
51
    }
52
53
    /**
54
     * Get main options of the mediafile.
55
     *
56
     * @param array $options
57
     *
58
     * @return array
59
     */
60
    private static function getMainOptions(array $options = []): array
61
    {
62
        $mainOptions = [
63
            'controls' => 'controls',
64
            'width' => Module::ORIGINAL_PREVIEW_WIDTH,
65
        ];
66
        if (isset($options['main']) && is_array($options['main'])) {
67
            $mainOptions = ArrayHelper::merge($mainOptions, $options['main']);
68
        }
69
        return $mainOptions;
70
    }
71
72
    /**
73
     * Get source options of the mediafile.
74
     *
75
     * @param string $src
76
     * @param array $options
77
     *
78
     * @return array
79
     */
80
    private static function getSourceOptions(string $src, array $options = []): array
81
    {
82
        $sourceOptions = [
83
            'src' => $src,
84
            'preload' => 'auto'
85
        ];
86
        if (isset($options['source']) && is_array($options['source'])) {
87
            $sourceOptions = ArrayHelper::merge($sourceOptions, $options['source']);
88
        }
89
        return $sourceOptions;
90
    }
91
92
    /**
93
     * Get track options of the mediafile.
94
     *
95
     * @param array $options
96
     *
97
     * @return array
98
     */
99
    private static function getTrackOptions(array $options = []): array
100
    {
101
        $trackOptions = [
102
            'kind' => 'subtitles'
103
        ];
104
        if (isset($options['track']) && is_array($options['track'])) {
105
            $trackOptions = ArrayHelper::merge($trackOptions, $options['track']);
106
        }
107
        return $trackOptions;
108
    }
109
110
    /**
111
     * Get tag of the mediafile.
112
     *
113
     * @param string $tagName
114
     * @param array $mainOptions
115
     * @param array $sourceOptions
116
     * @param array $trackOptions
117
     *
118
     * @return string
119
     */
120
    private static function getTag(
121
        string $tagName,
122
        array $mainOptions,
123
        array $sourceOptions,
124
        array $trackOptions): string {
125
126
        return static::tag(
127
            $tagName,
128
            static::tag('source', '', $sourceOptions) . static::tag('track', '', $trackOptions),
129
            $mainOptions
130
        );
131
    }
132
}
133