summaryrefslogtreecommitdiff
path: root/src/phorkie/ForkRemote.php
blob: f8e319a3e095586804dbab821080d08deca9d2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace phorkie;

class ForkRemote
{
    /**
     * Contains error message when parse() failed
     */
    public $error;

    protected $url;

    /**
     * Array with keys (URL title) and values (arrays of urls)
     * Only supported URLs are included.
     *
     * @var array
     */
    protected $arGitUrls;



    public function __construct($url)
    {
        $this->url = trim($url);
    }

    public function parse()
    {
        $hp = new HtmlParser();
        $ret = $hp->extractGitUrls($this->url);
        $this->arGitUrls = $hp->getGitUrls();
        $this->error = $hp->error;

        return $ret;
    }

    /**
     * Iterate through all git urls and return one if there is only
     * one supported one.
     *
     * @return mixed Boolean false or array with keys "url" and "title"
     */
    public function getUniqueGitUrl()
    {
        $nFound = 0;
        foreach ($this->arGitUrls as $title => $arUrls) {
            foreach ($arUrls as $url) {
                $nFound++;
                $uniqueUrl = array('url' => $url, 'title' => $title);
            }
        }

        if ($nFound == 1) {
            return $uniqueUrl;
        }
        return false;
    }

    public function getGitUrls()
    {
        return $this->arGitUrls;
    }

    /**
     * Get the URL from which the git URL was derived, often
     * the HTTP URL.
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    public function setUrl($url)
    {
        $this->url = $url;
    }
}
?>