aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/ASCollection.php
blob: c72b2fd041470a30bd475d39a2dcd74c5ddcf8b2 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

namespace Zotlabs\Lib;

/**
 * Class for dealing with fetching ActivityStreams collections (ordered or unordered, normal or paged).
 * Construct with either an existing object or url and an optional channel to sign requests.
 * $direction is 0 (default) to fetch from the beginning, and 1 to fetch from the end and reverse order the resultant array.
 * An optional limit to the number of records returned may also be specified.
 * Use $class->get() to return an array of collection members.
 */
class ASCollection {

	private $channel = null;
	private $nextpage = null;
	private $limit = 0;
	private $direction = 0;  // 0 = forward, 1 = reverse
	private $data = [];
	private $history = [];

	function __construct($obj, $channel = null, $direction = 0, $limit = 0) {

		$this->channel   = $channel;
		$this->direction = $direction;
		$this->limit     = $limit;

		$data = null;

		if (is_array($obj)) {
			$data = $obj;
		}

		if (is_string($obj)) {
			$data            = Activity::fetch($obj, $channel);
			$this->history[] = $obj;
		}

		if (!is_array($data)) {
			return;
		}

		if (!in_array($data['type'], ['Collection', 'OrderedCollection', 'OrderedCollectionPage'])) {
			return false;
		}

		if ($this->direction) {
			if (array_key_exists('last', $data) && $data['last']) {
				$this->nextpage = $data['last'];
			}
		}
		else {
			if (array_key_exists('first', $data) && $data['first']) {
				$this->nextpage = $data['first'];
			}
		}

		if (isset($data['items']) && is_array($data['items'])) {
			$this->data = (($this->direction) ? array_reverse($data['items']) : $data['items']);
		}
		elseif (isset($data['orderedItems']) && is_array($data['orderedItems'])) {
			$this->data = (($this->direction) ? array_reverse($data['orderedItems']) : $data['orderedItems']);
		}

		if ($this->limit) {
			if (count($this->data) > $limit) {
				$this->data = array_slice($this->data, 0, $limit);
				return;
			}
		}

		do {
			$x = $this->next();
		} while ($x);
	}

	function get() {
		return $this->data;
	}

	function next() {

		if (!$this->nextpage) {
			return false;
		}

		if (is_array($this->nextpage)) {
			$data = $this->nextpage;
		}

		if (is_string($this->nextpage)) {
			if (in_array($this->nextpage, $this->history)) {
				// recursion detected
				return false;
			}
			$data            = Activity::fetch($this->nextpage, $this->channel);
			$this->history[] = $this->nextpage;
		}

		if (!is_array($data)) {
			return false;
		}

		if (!in_array($data['type'], ['CollectionPage', 'OrderedCollectionPage'])) {
			return false;
		}

		$this->setnext($data);

		if (isset($data['items']) && is_array($data['items'])) {
			$this->data = array_merge($this->data, (($this->direction) ? array_reverse($data['items']) : $data['items']));
		}
		elseif (isset($data['orderedItems']) && is_array($data['orderedItems'])) {
			$this->data = array_merge($this->data, (($this->direction) ? array_reverse($data['orderedItems']) : $data['orderedItems']));
		}

		if ($this->limit) {
			if (count($this->data) > $this->limit) {
				$this->data     = array_slice($this->data, 0, $this->limit);
				$this->nextpage = false;
				return true;
			}
		}

		return true;
	}

	function setnext($data) {
		if ($this->direction) {
			if (array_key_exists('prev', $data) && $data['prev']) {
				$this->nextpage = $data['prev'];
			}
			elseif (array_key_exists('first', $data) && $data['first']) {
				$this->nextpage = $data['first'];
			}
			else {
				$this->nextpage = false;
			}
		}
		else {
			if (array_key_exists('next', $data) && $data['next']) {
				$this->nextpage = $data['next'];
			}
			elseif (array_key_exists('last', $data) && $data['last']) {
				$this->nextpage = $data['last'];
			}
			else {
				$this->nextpage = false;
			}
		}
		logger('nextpage: ' . $this->nextpage, LOGGER_DEBUG);
	}
}