activitypub/src/ContentNation/ActivityPub/Factory.php

303 lines
7.4 KiB
PHP

<?php
/**
* ContentNation.net activitpub server implementation
*
* @link https://contentnation.net
* @copyright Copyright (c) Sascha Nitsch Unternehmenberatung GmbH
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* See the LICENSE file in the project root for more information.
*/
namespace ContentNation\ActivityPub;
/**
* Factory class for creating various ActivityPub classes
*/
class Factory {
/**
* get a new create message
* @retval Message\Create new instance
*/
static function createMessage() : Message\Create {
require_once("Message/Create.php");
return new Message\Create();
}
/**
* get a new Document object
* @retval Common\Document new instance
*/
static function newDocument() : Common\Document {
require_once("Common/Document.php");
return new Common\Document();
}
/**
* get a new Image object
* @retval Common\Image new instance
*/
static function newImage() : Common\Image {
require_once("Common/Image.php");
return new Common\Image();
}
/**
* get a new response for instance requests
* @retval \Response\Instance new instance
*/
static function newInstanceResponse() : Response\Instance {
return new Response\Instance();
}
/**
* get a new Article object
* @retval Common\Article new instance
*/
static function newArticle() : Common\Article {
require_once("Common/Article.php");
return new Common\Article();
}
/**
* get a new Collection object
* @retval Common\Collection new instance
*/
static function newCollection() : Common\Collection {
require_once("Common/Collection.php");
return new Common\Collection();
}
/**
* get a new Note object
* @retval Common\Note new instance
*/
static function newNote() : Common\Note {
require_once("Common/Note.php");
return new Common\Note();
}
/**
* get a new Outbox object
* @retval Common\Outbox new instance
*/
static function newOutbox() : Common\Outbox {
require_once("Common/Outbox.php");
return new Common\Outbox();
}
/**
* get a new OrderedCollection object
* @retval Common\OrderedCollection new instance
*/
static function newOrderedCollection() : Common\OrderedCollection {
require_once("Common/OrderedCollection.php");
return new Common\OrderedCollection();
}
/**
* get a new Page object
* @retval Common\Page new instance
*/
static function newPage() : Common\Page {
require_once("Common/Page.php");
return new Common\Page();
}
/**
* get a new stats object
* @retval Common\Stats new instance
*/
static function newStats() : Common\Stats {
return new Common\Stats();
}
/**
* get a new contact object
* @retval Common\User new instance
*/
static function newUser() : Common\User {
return new Common\User();
}
/**
* get a new Tag object
* @retval Common\Tag new instance
*/
static function newTag() : Common\Tag {
require_once("Common/Tag.php");
return new Common\Tag();
}
/**
* get a new Follow object
* @retval Common\Follow new instance
*/
static function newFollow() : Common\Follow {
require_once("Common/Follow.php");
return new Common\Follow();
}
/**
* get a new Question object
* @retval Common\Question new instance
*/
static function newQuestion() : Common\Question {
require_once("Common/Question.php");
return new Common\Question();
}
/**
* get a new Undo object
* @retval Common\Undo new instance
*/
static function newUndo() : Common\Undo {
require_once("Common/Undo.php");
return new Common\Undo();
}
/**
* get a new Video object
* @retval Common\Video new instance
*/
static function newVideo() : Common\Video {
require_once("Common/Video.php");
return new Common\Video();
}
////////////////////// Activities
/**
* get a new Accept object
* @return Common\Accept new instance
*/
static function newAccept() : Common\Accept {
require_once("Common/Accept.php");
return new Common\Accept();
}
/**
* get a new Announce object
* @return Common\Announce new instance
*/
static function newAnnounce() : Common\Announce {
require_once("Common/Announce.php");
return new Common\Announce();
}
/**
* get a new Create object
* @return Common\Create new instance
*/
static function newCreate() : Common\Create {
require_once("Common/Create.php");
return new Common\Create();
}
/**
* get a new Delete object
* @return Common\Delete new instance
*/
static function newDelete() : Common\Delete {
require_once("Common/Delete.php");
return new Common\Delete();
}
/**
* get a new Event object
* @return Common\Event new instance
*/
static function newEvent() : Common\Event {
require_once("Common/Event.php");
return new Common\Event();
}
/**
* create object tree from json
* @param array<string, mixed> $json input json
* @return Common\APObject|false object or false on error
*/
static function newFromJson($json, string $jsonstring) {
if (gettype($json) !== "array") {
error_log("newFromJson called with ".gettype($json). " => ". debug_backtrace()[1]['function'] . " json: " . print_r($json, true));
return false;
}
if (!array_key_exists('type', $json)) {
return false;
}
$return = false;
switch($json['type']) {
case 'Article':
$return = Factory::newArticle();
break;
case 'Document':
$return = Factory::newDocument();
break;
case 'Event':
$return = Factory::newEvent();
break;
case 'Follow':
$return = Factory::newFollow();
break;
case 'Image':
$return = Factory::newImage();
/*error_log("Image: ". print_r($json, true));
error_log("Image json " . $jsonstring);*/
break;
case 'Note':
$return = Factory::newNote();
break;
case 'Question':
$return = Factory::newQuestion();
break;
case 'Video':
// error_log("newFromJson: video " . $jsonstring);
$return = Factory::newVideo();
break;
default:
error_log("newFromJson: unknown type: '" . $json['type'] . "' " . $jsonstring);
error_log(print_r($json, true));
}
if ($return !== false && $return->fromJson($json)) {
return $return;
}
return false;
}
/**
* create object tree from json
* @param array<string, mixed> $json input json
* @return Common\Activity|false object or false on error
*/
static function newActivityFromJson($json) {
if (!array_key_exists('type', $json)) {
return false;
}
$return = false;
switch($json['type']) {
case 'Accept':
$return = Factory::newAccept();
break;
case 'Announce':
$return = Factory::newAnnounce();
break;
case 'Create':
$return = Factory::newCreate();
break;
case 'Delete':
$return = Factory::newDelete();
break;
case 'Follow':
$return = Factory::newFollow();
break;
case 'Undo':
$return = Factory::newUndo();
break;
default:
error_log("newActivityFromJson " . print_r($json, true));
echo "unknown activity type: '" . $json['type'] . "'\n";
}
if ($return !== false && $return->fromJson($json)) {
return $return;
}
return false;
}
}