activitypub/src/ContentNation/ActivityPub/Common/OrderedCollection.php

72 lines
1.6 KiB
PHP

<?php
namespace ContentNation\ActivityPub\Common;
require_once("Collection.php");
class OrderedCollection extends Collection {
/**
* nested items
* @var APObject[]
*/
protected $items=[];
public function __construct() {
parent::__construct();
parent::addContext('https://www.w3.org/ns/activitystreams');
}
/**
* convert internal state to php array
* @return array<string,mixed>
*/
public function toObject() {
$return = parent::toObject();
$return['type'] = 'OrderedCollection';
if ($this->totalItems > 0) {
foreach($this->items as $item) {
$return['OrderedItems'][] = $item->toObject();
}
}
return $return;
}
/**
* create object from json
* @param array<string,mixed> $json input json
* @return bool true on success
*/
public function fromJson($json) : bool {
return parent::fromJson($json);
}
public function append(APObject &$item) : void {
$this->items[] = $item;
$this->totalItems = sizeof($this->items);
}
/**
* get item with given index
* @return APObject|false
*/
public function get(int $index) {
if ($index >= 0) {
if ($index >= $this->totalItems) {
return false;
}
return $this->items[$index];
} else {
if ($this->totalItems+ $index < 0) {
return false;
}
return $this->items[$this->totalItems + $index];
}
}
/**
* set items
* @param APObject[] $items
*/
public function setItems(&$items) : void {
$this->items = $items;
$this->totalItems = sizeof($items);
}
}