Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportRequest
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIntendedName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIntendedText
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIntendedSummary
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getImportDetailsHash
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\ImportException;
6use FileImporter\Exceptions\InvalidArgumentException;
7use FileImporter\Exceptions\LocalizedImportException;
8
9/**
10 * Alterations to be made usually provided by a user.
11 *
12 * @license GPL-2.0-or-later
13 * @author Addshore
14 */
15class ImportRequest {
16
17    private SourceUrl $url;
18    /** @var null|string */
19    private $intendedName;
20    /** @var null|string */
21    private $intendedText;
22    /** @var null|string */
23    private $intendedSummary;
24    /** @var string */
25    private $importDetailsHash;
26
27    /**
28     * @param string $url
29     * @param string|null $intendedName null for no intended change
30     * @param string|null $intendedText null for no intended change
31     * @param string|null $intendedSummary null for no intended change
32     * @param string $importDetailsHash
33     *
34     * @throws ImportException when the provided URL can't be parsed
35     */
36    public function __construct(
37        string $url,
38        string $intendedName = null,
39        string $intendedText = null,
40        string $intendedSummary = null,
41        string $importDetailsHash = ''
42    ) {
43        try {
44            $this->url = new SourceUrl( $url );
45        } catch ( InvalidArgumentException $e ) {
46            throw new LocalizedImportException( [ 'fileimporter-cantparseurl', $url ], $e );
47        }
48
49        if ( $intendedName !== null ) {
50            $intendedName = trim( $intendedName );
51        }
52
53        if ( $intendedText !== null ) {
54            /**
55             * White spaces and carriage returns are trimmed (inline with \MediaWiki\EditPage\EditPage) so that we can
56             * actually detect if the text to be saved has changed at all.
57             */
58            // TODO: This is identical to TextContent::normalizeLineEndings(). Just call that?
59            $intendedText = str_replace( [ "\r\n", "\r" ], "\n", rtrim( $intendedText ) );
60        }
61
62        $this->intendedName = $intendedName === '' ? null : $intendedName;
63        $this->intendedText = $intendedText;
64        $this->intendedSummary = $intendedSummary;
65        $this->importDetailsHash = $importDetailsHash;
66    }
67
68    public function getUrl(): SourceUrl {
69        return $this->url;
70    }
71
72    /**
73     * @return string|null Guaranteed to be a trimmed, non-empty string, or null
74     */
75    public function getIntendedName(): ?string {
76        return $this->intendedName;
77    }
78
79    /**
80     * @return null|string
81     */
82    public function getIntendedText(): ?string {
83        return $this->intendedText;
84    }
85
86    /**
87     * @return null|string
88     */
89    public function getIntendedSummary() {
90        return $this->intendedSummary;
91    }
92
93    /**
94     * @see \FileImporter\Data\ImportDetails::getOriginalHash
95     */
96    public function getImportDetailsHash(): string {
97        return $this->importDetailsHash;
98    }
99
100}