summaryrefslogtreecommitdiff
path: root/bin/reevotech/vendor/addwiki/mediawiki-datamodel/src/LogList.php
blob: 9ce302636a37961cd9a95acbe42c6d53cbf29ea3 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

namespace Mediawiki\DataModel;

use InvalidArgumentException;
use JsonSerializable;
use RuntimeException;

/**
 * Represents a collection of Log classes
 * @author Addshore
 */
class LogList implements JsonSerializable {

	/**
	 * @var Log[]
	 */
	private $logs;

	/**
	 * @param Log[] $logs
	 */
	public function __construct( $logs = array() ) {
		$this->logs = array();
		$this->addLogs( $logs );
	}

	/**
	 * @param Log[]|LogList $logs
	 *
	 * @throws InvalidArgumentException
	 */
	public function addLogs( $logs ) {
		if( !is_array( $logs ) && !$logs instanceof LogList ) {
			throw new InvalidArgumentException( '$logs needs to either be an array or a LogList object' );
		}
		if( $logs instanceof LogList ) {
			$logs = $logs->toArray();
		}
		foreach( $logs as $log ) {
			$this->addLog( $log );
		}
	}

	/**
	 * @param Log $log
	 */
	public function addLog( Log $log ) {
		$this->logs[$log->getId()] = $log;
	}

	/**
	 * @param int $id
	 *
	 * @return bool
	 */
	public function hasLogWithId( $id ){
		return array_key_exists( $id, $this->logs );
	}

	/**
	 * @param Log $log
	 *
	 * @return bool
	 */
	public function hasLog( Log $log ){
		return array_key_exists( $log->getId(), $this->logs );
	}

	/**
	 * @return Log|null Log or null if there is no log
	 */
	public function getLatest() {
		if( empty( $this->logs ) ) {
			return null;
		}
		return $this->logs[ max( array_keys( $this->logs ) ) ];
	}

	/**
	 * @since 0.6
	 * @return Log|null Log or null if there is no log
	 */
	public function getOldest() {
		if( empty( $this->logs ) ) {
			return null;
		}
		return $this->logs[ min( array_keys( $this->logs ) ) ];
	}

	/**
	 * @since 0.6
	 * @return bool
	 */
	public function isEmpty() {
		return empty( $this->logs );
	}

	/**
	 * @param int $id
	 *
	 * @throws RuntimeException
	 * @return Log
	 */
	public function get( $id ){
		if( $this->hasLogWithId( $id ) ){
			return $this->logs[$id];
		}
		throw new RuntimeException( 'No such Log loaded in LogList object' );
	}

	/**
	 * @return Log[]
	 */
	public function toArray() {
		return $this->logs;
	}

	/**
	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
	 */
	public function jsonSerialize() {
		return $this->toArray();
	}

	/**
	 * @param array $json
	 *
	 * @return self
	 */
	public static function jsonDeserialize( $json ) {
		$self = new LogList();
		foreach ( $json as $logJson ) {
			$self->addLog( Log::jsonDeserialize( $logJson ) );
		}
		return $self;
	}
}