1<?php
2/**
3 * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
4 *
5 * @author Julius Härtl <jus@bitgrid.net>
6 *
7 * @license GNU AGPL version 3 or any later version
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24namespace OCA\Deck\Activity;
25
26class ChangeSet implements \JsonSerializable {
27	private $before;
28	private $after;
29	private $diff = false;
30
31	public function __construct($before = null, $after = null) {
32		if ($before !== null) {
33			$this->setBefore($before);
34		}
35		if ($after !== null) {
36			$this->setAfter($after);
37		}
38	}
39
40	public function enableDiff() {
41		$this->diff = true;
42	}
43
44	public function getDiff() {
45		return $this->diff;
46	}
47
48	public function setBefore($before) {
49		if (is_object($before)) {
50			$this->before = clone $before;
51		} else {
52			$this->before = $before;
53		}
54	}
55
56	public function setAfter($after) {
57		if (is_object($after)) {
58			$this->after = clone $after;
59		} else {
60			$this->after = $after;
61		}
62	}
63
64	public function getBefore() {
65		return $this->before;
66	}
67
68	public function getAfter() {
69		return $this->after;
70	}
71
72	/**
73	 * Specify data which should be serialized to JSON
74	 *
75	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
76	 * @return mixed data which can be serialized by <b>json_encode</b>,
77	 * which is a value of any type other than a resource.
78	 * @since 5.4.0
79	 */
80	public function jsonSerialize() {
81		return [
82			'before' => $this->getBefore(),
83			'after' => $this->getAfter(),
84			'diff' => $this->getDiff(),
85			'type' => get_class($this->before)
86		];
87	}
88}
89