Passed
Push — main ( 8bfeba...683260 )
by Teodoro
01:57
created

Versioned::version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tleckie\Assets\Versioned;
6
7
use function sprintf;
8
9
/**
10
 * Class Versioned
11
 *
12
 * @package  Tleckie\Assets\Versioned
13
 * @category Versioned
14
 * @author   Teodoro Leckie Westberg <[email protected]>
15
 */
16
class Versioned implements VersionedInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected string $version = '';
22
23
    /**
24
     * @var string
25
     */
26
    protected string $format = '';
27
28
    /**
29
     * Versioned constructor.
30
     *
31
     * @param string $version
32
     * @param string $format
33
     */
34
    public function __construct(string $version, string $format = "%s?%s")
35
    {
36
        $this->version = $version;
37
        $this->format = $format;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function applyVersion(string $asset): string
44
    {
45
        return preg_replace(
46
            '#\/{2,}#',
47
            '/',
48
            sprintf($this->format, $asset, $this->version())
49
        );
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function version(): string
56
    {
57
        return $this->version;
58
    }
59
}
60