forked from grumpydevelop/federator
extended factory and constructing from json
renaming to not mix total count with paging
This commit is contained in:
parent
207d876254
commit
474631dff2
9 changed files with 82 additions and 22 deletions
|
@ -87,7 +87,7 @@ class Followers implements \Federator\Api\FedUsers\FedUsersInterface
|
||||||
// Pagination navigation
|
// Pagination navigation
|
||||||
$lastPage = max(0, ceil($totalItems / $pageSize) - 1);
|
$lastPage = max(0, ceil($totalItems / $pageSize) - 1);
|
||||||
|
|
||||||
if ($page === "" || $followers->count() == 0) {
|
if ($page === "" || $followers->getCount() == 0) {
|
||||||
$followers->setFirst($baseUrl . '?page=0');
|
$followers->setFirst($baseUrl . '?page=0');
|
||||||
$followers->setLast($baseUrl . '?page=' . $lastPage);
|
$followers->setLast($baseUrl . '?page=' . $lastPage);
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ class Following implements \Federator\Api\FedUsers\FedUsersInterface
|
||||||
// Pagination navigation
|
// Pagination navigation
|
||||||
$lastPage = max(0, ceil($totalItems / $pageSize) - 1);
|
$lastPage = max(0, ceil($totalItems / $pageSize) - 1);
|
||||||
|
|
||||||
if ($page === "" || $following->count() == 0) {
|
if ($page === "" || $following->getCount() == 0) {
|
||||||
$following->setFirst($baseUrl . '?page=0');
|
$following->setFirst($baseUrl . '?page=0');
|
||||||
$following->setLast($baseUrl . '?page=' . $lastPage);
|
$following->setLast($baseUrl . '?page=' . $lastPage);
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,20 @@ class Collection extends APObject
|
||||||
*/
|
*/
|
||||||
public function fromJson($json)
|
public function fromJson($json)
|
||||||
{
|
{
|
||||||
return parent::fromJson($json);
|
$success = parent::fromJson($json);
|
||||||
|
if (!$success) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (array_key_exists('totalItems', $json)) {
|
||||||
|
$this->totalItems = $json['totalItems'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('first', $json)) {
|
||||||
|
$this->first = $json['first'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('last', $json)) {
|
||||||
|
$this->last = $json['last'];
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,11 +74,16 @@ class Collection extends APObject
|
||||||
$this->totalItems = $totalItems;
|
$this->totalItems = $totalItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function count(): int
|
public function getTotalItems(): int
|
||||||
{
|
{
|
||||||
return $this->totalItems;
|
return $this->totalItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getFirst(): string
|
||||||
|
{
|
||||||
|
return $this->first;
|
||||||
|
}
|
||||||
|
|
||||||
public function setFirst(string $url): void
|
public function setFirst(string $url): void
|
||||||
{
|
{
|
||||||
$this->first = $url;
|
$this->first = $url;
|
||||||
|
|
|
@ -25,7 +25,7 @@ class Followers extends OrderedCollectionPage
|
||||||
public function setItems(&$items)
|
public function setItems(&$items)
|
||||||
{
|
{
|
||||||
// Optionally: type check that all $items are Activity objects
|
// Optionally: type check that all $items are Activity objects
|
||||||
$this->items = $items;
|
$this->orderedItems = $items;
|
||||||
$this->totalItems = sizeof($items);
|
$this->totalItems = sizeof($items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ class Following extends OrderedCollectionPage
|
||||||
public function setItems(&$items)
|
public function setItems(&$items)
|
||||||
{
|
{
|
||||||
// Optionally: type check that all $items are Activity objects
|
// Optionally: type check that all $items are Activity objects
|
||||||
$this->items = $items;
|
$this->orderedItems = $items;
|
||||||
$this->totalItems = sizeof($items);
|
$this->totalItems = sizeof($items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ class OrderedCollection extends Collection
|
||||||
*
|
*
|
||||||
* @var APObject[]|string[]
|
* @var APObject[]|string[]
|
||||||
*/
|
*/
|
||||||
protected $items = [];
|
protected $orderedItems = [];
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
@ -32,8 +32,8 @@ class OrderedCollection extends Collection
|
||||||
{
|
{
|
||||||
$return = parent::toObject();
|
$return = parent::toObject();
|
||||||
$return['type'] = 'OrderedCollection';
|
$return['type'] = 'OrderedCollection';
|
||||||
if ($this->totalItems > 0) {
|
if (sizeof($this->orderedItems) > 0) {
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->orderedItems as $item) {
|
||||||
if (is_string($item)) {
|
if (is_string($item)) {
|
||||||
$return['orderedItems'][] = $item;
|
$return['orderedItems'][] = $item;
|
||||||
} elseif (is_object($item)) {
|
} elseif (is_object($item)) {
|
||||||
|
@ -52,7 +52,20 @@ class OrderedCollection extends Collection
|
||||||
*/
|
*/
|
||||||
public function fromJson($json)
|
public function fromJson($json)
|
||||||
{
|
{
|
||||||
return parent::fromJson($json);
|
$success = parent::fromJson($json);
|
||||||
|
if (!$success) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (array_key_exists('orderedItems', $json)) {
|
||||||
|
foreach ($json['orderedItems'] as $item) {
|
||||||
|
$obj = \Federator\Data\ActivityPub\Factory::newActivityFromJson($item);
|
||||||
|
if ($obj !== false) {
|
||||||
|
$this->orderedItems[] = $obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,8 +74,7 @@ class OrderedCollection extends Collection
|
||||||
*/
|
*/
|
||||||
public function append(&$item): void
|
public function append(&$item): void
|
||||||
{
|
{
|
||||||
$this->items[] = $item;
|
$this->orderedItems[] = $item;
|
||||||
$this->totalItems = sizeof($this->items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,18 +85,22 @@ class OrderedCollection extends Collection
|
||||||
public function get(int $index)
|
public function get(int $index)
|
||||||
{
|
{
|
||||||
if ($index >= 0) {
|
if ($index >= 0) {
|
||||||
if ($index >= $this->totalItems) {
|
if ($index >= sizeof($this->orderedItems)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return $this->items[$index];
|
return $this->orderedItems[$index];
|
||||||
} else {
|
} else {
|
||||||
if ($this->totalItems + $index < 0) {
|
if (sizeof($this->orderedItems) + $index < 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return $this->items[$this->totalItems + $index];
|
return $this->orderedItems[sizeof($this->orderedItems) + $index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCount(): int
|
||||||
|
{
|
||||||
|
return sizeof($this->orderedItems);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* set items
|
* set items
|
||||||
*
|
*
|
||||||
|
@ -93,7 +109,6 @@ class OrderedCollection extends Collection
|
||||||
*/
|
*/
|
||||||
public function setItems(&$items)
|
public function setItems(&$items)
|
||||||
{
|
{
|
||||||
$this->items = $items;
|
$this->orderedItems = $items;
|
||||||
$this->totalItems = sizeof($items);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,29 @@ class OrderedCollectionPage extends OrderedCollection
|
||||||
*/
|
*/
|
||||||
public function fromJson($json)
|
public function fromJson($json)
|
||||||
{
|
{
|
||||||
return parent::fromJson($json);
|
$success = parent::fromJson($json);
|
||||||
|
if (!$success) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (array_key_exists('next', $json)) {
|
||||||
|
$this->next = $json['next'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('prev', $json)) {
|
||||||
|
$this->prev = $json['prev'];
|
||||||
|
}
|
||||||
|
if (array_key_exists('partOf', $json)) {
|
||||||
|
$this->partOf = $json['partOf'];
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get next url
|
||||||
|
* @return string next URL
|
||||||
|
*/
|
||||||
|
public function getNext()
|
||||||
|
{
|
||||||
|
return $this->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -35,8 +35,7 @@ class Outbox extends OrderedCollectionPage
|
||||||
public function setItems(&$items)
|
public function setItems(&$items)
|
||||||
{
|
{
|
||||||
// Optionally: type check that all $items are Activity objects
|
// Optionally: type check that all $items are Activity objects
|
||||||
$this->items = $items;
|
$this->orderedItems = $items;
|
||||||
$this->totalItems = sizeof($items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,7 @@ class Factory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create object tree from json
|
* create object tree from json
|
||||||
* @param array<string, mixed> $json input json
|
* @param array<string, mixed>|mixed $json input json
|
||||||
* @return Common\APObject|null object or false on error
|
* @return Common\APObject|null object or false on error
|
||||||
*/
|
*/
|
||||||
public static function newFromJson($json, string $jsonstring)
|
public static function newFromJson($json, string $jsonstring)
|
||||||
|
@ -67,6 +67,12 @@ class Factory
|
||||||
case 'Inbox':
|
case 'Inbox':
|
||||||
$return = new Common\Inbox();
|
$return = new Common\Inbox();
|
||||||
break;
|
break;
|
||||||
|
case 'OrderedCollection':
|
||||||
|
$return = new Common\OrderedCollection();
|
||||||
|
break;
|
||||||
|
case 'OrderedCollectionPage':
|
||||||
|
$return = new Common\OrderedCollectionPage();
|
||||||
|
break;
|
||||||
case 'Tombstone':
|
case 'Tombstone':
|
||||||
$return = new Common\APObject("Tombstone");
|
$return = new Common\APObject("Tombstone");
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Reference in a new issue