PackageNameVersionExtractor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of Bowerphp.
5
 *
6
 * (c) Piotr Olaszewski <piotroo89 [%] gmail dot com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bowerphp\Util;
13
14
/**
15
 * PackageNameVersionExtractor
16
 */
17
class PackageNameVersionExtractor
18
{
19
    /**
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     * @var string
26
     */
27
    public $version;
28
29
    /**
30
     * @param string $name
31
     * @param string $version
32
     */
33
    public function __construct($name, $version)
34
    {
35
        $this->name = $name;
36
        $this->version = $version;
37
    }
38
39
    /**
40
     * @param  string                      $endpoint
41
     * @return PackageNameVersionExtractor
42
     */
43
    public static function fromString($endpoint)
44
    {
45
        $map = explode('#', $endpoint);
46
        $name = isset($map[0]) ? $map[0] : $endpoint;
47
        $version = isset($map[1]) ? $map[1] : '*';
48
49
        // Convert user/package shorthand to GitHub url
50
        if (preg_match('/^([-_a-z0-9]+)\/([-_a-z0-9]+)$/i', $name)) {
51
            $name = 'https://github.com/' . $name . '.git';
52
        }
53
54
        return new self($name, $version);
55
    }
56
}
57