• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / assets / 14744861512

30 Apr 2025 01:38AM UTC coverage: 94.413% (+6.8%) from 87.571%
14744861512

push

github

dg
assets: added new properties

2 of 2 new or added lines in 2 files covered. (100.0%)

4 existing lines in 2 files now uncovered.

169 of 179 relevant lines covered (94.41%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/src/Assets/Helpers.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Nette\Assets;
6

7
use Nette;
8

9

10
/**
11
 * Static helper class providing utility functions for working with assets.
12
 */
13
final class Helpers
14
{
15
        use Nette\StaticClass;
16

17
        private const ExtensionToMime = [
18
                'avif' => 'image/avif', 'gif' => 'image/gif', 'ico' => 'image/vnd.microsoft.icon', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'png' => 'image/png', 'svg' => 'image/svg+xml', 'webp' => 'image/webp',
19
                'js' => 'application/javascript', 'mjs' => 'application/javascript',
20
                'css' => 'text/css',
21
                'aac' => 'audio/aac', 'flac' => 'audio/flac', 'm4a' => 'audio/mp4', 'mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'wav' => 'audio/wav',
22
                'avi' => 'video/x-msvideo', 'mkv' => 'video/x-matroska', 'mov' => 'video/quicktime', 'mp4' => 'video/mp4', 'ogv' => 'video/ogg', 'webm' => 'video/webm',
23
        ];
24

25

26
        public static function createAssetFromUrl(string $url, ?string $path, array $args = []): Asset
1✔
27
        {
28
                $args['url'] = $url;
1✔
29
                $args['sourcePath'] = $path;
1✔
30
                $mime = (string) $args['mimeType'] ??= self::guessMimeTypeFromExtension($url);
1✔
31
                $class = match (true) {
1✔
32
                        $mime === 'application/javascript' => ScriptAsset::class,
1✔
33
                        $mime === 'text/css' => StyleAsset::class,
1✔
34
                        str_starts_with($mime, 'image/') => ImageAsset::class,
1✔
35
                        str_starts_with($mime, 'audio/') => AudioAsset::class,
1✔
36
                        str_starts_with($mime, 'video/') => VideoAsset::class,
1✔
37
                        default => GenericAsset::class,
1✔
38
                };
39
                return new $class(...$args);
1✔
40
        }
41

42

43
        public static function guessMimeTypeFromExtension(string $path): ?string
1✔
44
        {
45
                return self::ExtensionToMime[strtolower(pathinfo($path, PATHINFO_EXTENSION))] ?? null;
1✔
46
        }
47

48

49
        /**
50
         * Validates an array of options against allowed optional and required keys.
51
         * @throws \InvalidArgumentException if there are unsupported or missing options
52
         */
53
        public static function checkOptions(array $array, array $optional = [], array $required = []): void
1✔
54
        {
55
                if ($keys = array_diff(array_keys($array), $optional, $required)) {
1✔
56
                        throw new \InvalidArgumentException('Unsupported asset options: ' . implode(', ', $keys));
1✔
57
                }
58
                if ($keys = array_diff($required, array_keys($array))) {
1✔
UNCOV
59
                        throw new \InvalidArgumentException('Missing asset options: ' . implode(', ', $keys));
×
60
                }
61
        }
1✔
62

63

64
        /**
65
         * Estimates the duration (in seconds) of an MP3 file, assuming constant bitrate (CBR).
66
         * @throws \RuntimeException If the file cannot be opened, MP3 sync bits aren't found, or bitrate is invalid/unsupported.
67
         */
68
        public static function guessMP3Duration(string $path): float
1✔
69
        {
70
                if (
71
                        ($header = @file_get_contents($path, length: 10000)) === false // @ - file may not exist
1✔
72
                        || ($fileSize = @filesize($path)) === false
1✔
73
                ) {
UNCOV
74
                        throw new \RuntimeException(sprintf("Failed to open file '%s'. %s", $path, Nette\Utils\Helpers::getLastError()));
×
75
                }
76

77
                $frameOffset = strpos($header, "\xFF\xFB"); // 0xFB indicates MPEG Version 1, Layer III, no protection bit.
1✔
78
                if ($frameOffset === false) {
1✔
79
                        throw new \RuntimeException('Failed to find MP3 frame sync bits.');
1✔
80
                }
81

82
                $frameHeader = substr($header, $frameOffset, 4);
1✔
83
                $headerBits = unpack('N', $frameHeader)[1];
1✔
84
                $bitrateIndex = ($headerBits >> 12) & 0xF;
1✔
85
                $bitrate = [null, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320][$bitrateIndex] ?? null;
1✔
86
                if ($bitrate === null) {
1✔
UNCOV
87
                        throw new \RuntimeException('Invalid or unsupported bitrate index.');
×
88
                }
89

90
                return $fileSize * 8 / $bitrate / 1000;
1✔
91
        }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc