Completed
Push — main ( 1f6f71...689fbc )
by
unknown
04:30
created

Content::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Addwiki\Mediawiki\DataModel;
4
5
use LogicException;
6
7
/**
8
 * Class Representing the content of a revision
9
 *
10
 * @author Addshore
11
 */
12
class Content {
13
14
	/**
15
	 * @var string sha1 hash of the object content upon creation
16
	 */
17
	private $initialHash;
18
19
	/**
20
	 * @var mixed
21
	 */
22
	private $data;
23
24
	/**
25
	 * @var string|null
26
	 */
27
	private $model;
28
29
	/**
30
	 * Should always be called AFTER overriding constructors so a hash can be created
31
	 *
32
	 * @param mixed $data
33
	 * @param string|null $model
34
	 */
35
	public function __construct( $data, $model = null ) {
36
		$this->data = $data;
37
		$this->model = $model;
38
		$this->initialHash = $this->getHash();
39
	}
40
41
	/**
42
	 * @return string
43
	 */
44
	public function getModel() {
45
		return $this->model;
46
	}
47
48
	/**
49
	 * Returns a sha1 hash of the content
50
	 *
51
	 * @throws LogicException
52
	 * @return string
53
	 */
54
	public function getHash() {
55
		$data = $this->getData();
56
		if ( is_object( $data ) ) {
57
			if ( method_exists( $data, 'getHash' ) ) {
58
				return $data->getHash();
59
			} else {
60
				return sha1( serialize( $data ) );
61
			}
62
		}
63
		if ( is_string( $data ) ) {
64
			return sha1( $data );
65
		}
66
		throw new LogicException( "Cant get hash for data of type: " . gettype( $data ) );
67
	}
68
69
	/**
70
	 * Has the content been changed since object construction (this shouldn't happen!)
71
	 *
72
	 * @return bool
73
	 */
74
	public function hasChanged() {
75
		return $this->initialHash !== $this->getHash();
76
	}
77
78
	/**
79
	 * @return mixed
80
	 */
81
	public function getData() {
82
		return $this->data;
83
	}
84
85
}
86