streams/Zotlabs/Lib/ASCollection.php

159 lines
3.7 KiB
PHP
Raw Normal View History

2019-08-27 02:54:21 +00:00
<?php
namespace Zotlabs\Lib;
use Zotlabs\Lib\ActivityStreams;
use Zotlabs\Lib\Activity;
/**
2020-08-05 05:54:04 +00:00
* 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.
2020-08-05 05:54:04 +00:00
* Use $class->get() to return an array of collection members.
2019-08-27 02:54:21 +00:00
*/
class ASCollection {
private $channel = null;
private $nextpage = null;
private $limit = 0;
private $direction = 0; // 0 = forward, 1 = reverse
private $data = [];
2020-08-07 05:22:51 +00:00
private $history = [];
function __construct($obj, $channel = null, $direction = 0, $limit = 0) {
2019-08-27 02:54:21 +00:00
$this->channel = $channel;
$this->direction = $direction;
$this->limit = $limit;
2019-08-27 02:54:21 +00:00
if (is_array($obj)) {
$data = $obj;
}
2019-08-27 02:54:21 +00:00
if (is_string($obj)) {
$data = Activity::fetch($obj,$channel);
2020-08-07 05:22:51 +00:00
$this->history[] = $obj;
}
2020-08-05 05:54:04 +00:00
if (! is_array($data)) {
return;
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
if (! in_array($data['type'], ['Collection','OrderedCollection'])) {
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'];
}
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
if (isset($data['items']) && is_array($data['items'])) {
$this->data = (($this->direction) ? array_reverse($data['items']) : $data['items']);
2020-08-05 05:54:04 +00:00
}
elseif (isset($data['orderedItems']) && is_array($data['orderedItems'])) {
$this->data = (($this->direction) ? array_reverse($data['orderedItems']) : $data['orderedItems']);
}
if ($limit) {
if (count($this->data) > $limit) {
$this->data = array_slice($this->data,0,$limit);
return;
}
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
do {
$x = $this->next();
} while ($x);
}
function get() {
2019-08-27 02:54:21 +00:00
return $this->data;
}
function next() {
if (! $this->nextpage) {
2020-08-05 05:54:04 +00:00
return false;
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
2020-08-07 05:22:51 +00:00
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;
}
2019-08-27 02:54:21 +00:00
2020-08-05 05:54:04 +00:00
if (! is_array($data)) {
return false;
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
if (! in_array($data['type'], ['CollectionPage','OrderedCollectionPage'])) {
return false;
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
2019-08-27 02:54:21 +00:00
$this->setnext($data);
2020-08-05 05:54:04 +00:00
if (isset($data['items']) && is_array($data['items'])) {
$this->data = array_merge($this->data,(($this->direction) ? array_reverse($data['items']) : $data['items']));
2020-08-05 05:54:04 +00:00
}
elseif (isset($data['orderedItems']) && is_array($data['orderedItems'])) {
$this->data = array_merge($this->data,(($this->direction) ? array_reverse($data['orderedItems']) : $data['orderedItems']));
}
if ($limit) {
if (count($this->data) > $limit) {
$this->data = array_slice($this->data,0,$limit);
$this->nextpage = false;
return true;
}
2020-08-05 05:54:04 +00:00
}
return true;
2019-08-27 02:54:21 +00:00
}
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;
}
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
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;
}
2020-08-05 05:54:04 +00:00
}
logger('nextpage: ' . $this->nextpage, LOGGER_DEBUG);
2019-08-27 02:54:21 +00:00
}
2020-08-05 05:54:04 +00:00
}