Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SourceUrl
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParsedUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace FileImporter\Data;
4
5use FileImporter\Exceptions\InvalidArgumentException;
6
7/**
8 * Class representing a (possibly unnormalized) URL passed into the extension for importing. Can be
9 * any URL (e.g. a Flickr image), not necessarily pointing to a MediaWiki.
10 *
11 * Basic normalization that is true for any URL can be done here (e.g. Unicode normalization).
12 * Normalizations for specific sources (as detected by dedicated SourceUrlCheckers) need to be done
13 * in dedicated SourceUrlNormalizers.
14 *
15 * @license GPL-2.0-or-later
16 * @author Addshore
17 */
18class SourceUrl {
19
20    private const ERROR_SOURCE_URL_UNPARSEABLE = 'sourceUrlUnparseable';
21
22    private string $url;
23    /** @var string[] */
24    private $parsed;
25
26    /**
27     * @param string $url For example, https://en.wikipedia.org/wiki/File:Foo.JPG
28     *
29     * @throws InvalidArgumentException When $url is not parsable
30     */
31    public function __construct( string $url ) {
32        $this->url = trim( $url );
33        $this->parsed = wfParseUrl( $this->url );
34        if ( !$this->parsed ) {
35            throw new InvalidArgumentException( '$url is not parsable',
36                self::ERROR_SOURCE_URL_UNPARSEABLE );
37        }
38    }
39
40    /**
41     * @return string The raw URL
42     */
43    public function getUrl(): string {
44        return $this->url;
45    }
46
47    /**
48     * @return string[] Parsed URL array provided by wfParseUrl
49     */
50    public function getParsedUrl(): array {
51        return $this->parsed;
52    }
53
54    /**
55     * @return string The host, for example "en.wikipedia.org"
56     */
57    public function getHost(): string {
58        return strtolower( $this->parsed['host'] );
59    }
60
61    public function __toString(): string {
62        return $this->url;
63    }
64
65}