Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaTypeTester
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 testValue
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6declare(strict_types=1);
7
8namespace pvc\html\val_tester;
9
10use pvc\interfaces\http\mime\MimeTypesInterface;
11use pvc\interfaces\validator\ValTesterInterface;
12
13/**
14 * class ValTesterMediaTypeSpecifier
15 * @implements ValTesterInterface<string>
16 *  documentation from https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept#unique_file_type_specifiers
17 *
18 *  A valid case-insensitive filename extension, starting with a period (".") character. For example: .jpg, .pdf, or .doc.
19 *  A valid MIME type string, with no extensions.
20 *  The string audio/* meaning "any audio file".
21 *  The string video/* meaning "any video file".
22 *  The string image/* meaning "any image file".
23 */
24class MediaTypeTester implements ValTesterInterface
25{
26    protected MimeTypesInterface $mimeTypes;
27
28    public function __construct(MimeTypesInterface $mimeTypes)
29    {
30        $this->mimeTypes = $mimeTypes;
31    }
32
33    /**
34     * { @inheritDoc }
35     */
36    public function testValue(mixed $value): bool
37    {
38        $generalized = ['audio/*', 'video/*', 'image/*'];
39        if (in_array($value, $generalized)) {
40            return true;
41        }
42
43        if ($this->mimeTypes->isValidMimeTypeName($value) || $this->mimeTypes->isValidMimeTypeFileExtension($value)) {
44            return true;
45        }
46
47        return false;
48    }
49}