Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Validator
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 checkUrl
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 checkTitle
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20namespace MediaWiki\Extension\RealMe;
21
22use MediaWiki\Title\Title;
23use MediaWiki\Utils\UrlUtils;
24use MessageLocalizer;
25
26class Validator {
27
28    /** @var MessageLocalizer */
29    private $localizer;
30
31    /** @var UrlUtils */
32    private $urlUtils;
33
34    public function __construct( MessageLocalizer $localizer, UrlUtils $urlUtils ) {
35        $this->localizer = $localizer;
36        $this->urlUtils = $urlUtils;
37    }
38
39    /**
40     * Validate the specified URL, returning errors if necessary
41     */
42    public function checkUrl( string $url ): array {
43        $errors = [];
44        $parsed = $this->urlUtils->parse( $url );
45        if ( !$parsed ) {
46            $errors[] = $this->localizer->msg( 'realme-preference-error-invalid' )
47                ->plaintextParams( $url );
48            return $errors;
49        }
50
51        if ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https' ) {
52            $errors[] = $this->localizer->msg( 'realme-preference-error-not-http' )
53                ->plaintextParams( $url, $parsed['scheme'] . $parsed['delimiter'] );
54        }
55
56        return $errors;
57    }
58
59    /**
60     * Validate the specified title, returning errors if necessary
61     */
62    public function checkTitle( string $title ): array {
63        $errors = [];
64        $titleObj = Title::newFromText( $title );
65        if ( $titleObj ) {
66            if ( $titleObj->getPrefixedText() !== $title ) {
67                $errors[] = $this->localizer->msg( 'realme-config-error-canonical' )
68                    ->plaintextParams( $titleObj->getPrefixedText(), $title );
69            }
70        } else {
71            $errors[] = $this->localizer->msg( 'realme-config-error-invalidtitle' )
72                ->plaintextParams( $title );
73        }
74
75        return $errors;
76    }
77}