summaryrefslogtreecommitdiffstats
path: root/modules-available/roomplanner/inc/composedroom.inc.php
blob: 11e8455ef9495b1fea8a94a8b89ea67eda6a53e7 (plain) (blame)
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
<?php

class ComposedRoom
{
	/**
	 * @var string How to compose contained rooms. Value is either horizontal or vertical.
	 */
	public $orientation = 'horizontal';

	/**
	 * @var int[] Order in which contained rooms are composed. List of locationid.
	 */
	public $list;

	/**
	 * @var bool Whether composed room is active, ie. visible in PVS.
	 */
	public $enabled;

	/**
	 * @var int locationid of contained room that is the controlling room;
	 */
	public $controlRoom;

	public function __construct($data)
	{
		if ($data instanceof ComposedRoom) {
			foreach ($data as $k => $v) {
				$this->{$k} = $v;
			}
		} else {
			if (is_array($data) && isset($data['roomplan'])) {
				// From DB
				$data = json_decode($data['roomplan'], true);
			} elseif (is_string($data)) {
				// Just JSON
				$data = json_decode($data, true);
			}
			if (is_array($data)) {
				foreach ($this as $k => $v) {
					if (isset($data[$k])) {
						$this->{$k} = $data[$k];
					}
				}
			}
		}
		$this->sanitize();
	}

	/**
	 * Make sure all member vars have the proper type
	 */
	private function sanitize()
	{
		$this->orientation = ($this->orientation === 'horizontal' ? 'horizontal' : 'vertical');
		settype($this->enabled, 'bool');
		settype($this->list, 'array');
		settype($this->controlRoom, 'int');
		foreach ($this->list as &$v) {
			settype($v, 'int');
		}
		$this->list = array_values($this->list);
		if (!empty($this->list) && !in_array($this->controlRoom, $this->list)) {
			$this->controlRoom = $this->list[0];
		}
	}

	/**
	 * @return false|string JSON
	 */
	public function serialize()
	{
		$this->sanitize();
		return json_encode($this);
	}

}