summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/SemanticDataFactory.php
blob: 2b7d620bef9efe64d54a43d0ba2a6edbf49f619b (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
<?php

namespace SMW\Tests\Utils;

use RuntimeException;
use SMW\DIWikiPage;
use SMW\SemanticData;
use Title;

/**
 * @license GNU GPL v2+
 * @since   2.0
 *
 * @author mwjames
 */
class SemanticDataFactory {

	private $subject = null;

	/**
	 * @since 2.0
	 *
	 * @param Title|string $title
	 */
	public function setTitle( $title ) {

		if ( is_string( $title ) ) {
			$title = Title::newFromText( $title );
		}

		if ( $title instanceof Title ) {
			return $this->setSubject( DIWikiPage::newFromTitle( $title ) );
		}

		throw new RuntimeException( "Something went wrong" );
	}

	/**
	 * @since 2.0
	 *
	 * @param DIWikiPage $subject
	 */
	public function setSubject( DIWikiPage $subject ) {
		$this->subject = $subject;
		return $this;
	}

	/**
	 * @since  2.0
	 *
	 * @param string $title
	 *
	 * @return SemanticData
	 * @throws RuntimeException
	 */
	public function newEmptySemanticData( $title = null ) {

		if ( $title instanceof DIWikiPage ) {
			$this->setSubject( $title );
		} elseif ( $title !== null ) {
			$this->setTitle( $title );
		}

		if ( $this->subject !== null ) {
			return new SemanticData( $this->subject );
		}

		throw new RuntimeException( "Something went wrong" );
	}

	/**
	 * @since 2.0
	 */
	public function null() {
		$this->subject = null;
		return this;
	}

}