ActivityPub is an implementation of ActivityPub layers in PHP.
It provides two layers:
- A client to server protocol, or "Social API" This protocol permits a client to act on behalf of a user.
- A server to server protocol, or "Federation Protocol" This protocol is used to distribute activities between actors on different servers, tying them into the same social graph.
As the two layers are implemented, it aims to be an ActivityPub conformant Federated Server
All normalized types are implemented too. If you need to create a new one, just extend existing types.
See the full documentation or an overview below.
- Supports PHP7+
composer require landrok/activitypubAll core types are provided:
use ActivityPub\Type\Core\Activity;
use ActivityPub\Type\Core\Collection;
use ActivityPub\Type\Core\CollectionPage;
use ActivityPub\Type\Core\IntransitiveActivity;
use ActivityPub\Type\Core\Link;
use ActivityPub\Type\Core\ObjectType;
use ActivityPub\Type\Core\OrderedCollection;
use ActivityPub\Type\Core\OrderedCollectionPage;All extended types are provided:
use ActivityPub\Type\Extended\Actor\Application;
use ActivityPub\Type\Extended\Actor\Group;
use ActivityPub\Type\Extended\Actor\Organization;
use ActivityPub\Type\Extended\Actor\Person;
use ActivityPub\Type\Extended\Actor\Service;use ActivityPub\Type\Extended\Activity\Accept;
use ActivityPub\Type\Extended\Activity\Add;
use ActivityPub\Type\Extended\Activity\Announce;
use ActivityPub\Type\Extended\Activity\Arrive;
use ActivityPub\Type\Extended\Activity\Block;
use ActivityPub\Type\Extended\Activity\Create;
use ActivityPub\Type\Extended\Activity\Delete;
use ActivityPub\Type\Extended\Activity\Dislike;
use ActivityPub\Type\Extended\Activity\Flag;
use ActivityPub\Type\Extended\Activity\Follow;
use ActivityPub\Type\Extended\Activity\Ignore;
use ActivityPub\Type\Extended\Activity\Invite;
use ActivityPub\Type\Extended\Activity\Join;
use ActivityPub\Type\Extended\Activity\Leave;
use ActivityPub\Type\Extended\Activity\Like;
use ActivityPub\Type\Extended\Activity\Listen;
use ActivityPub\Type\Extended\Activity\Move;
use ActivityPub\Type\Extended\Activity\Offer;
use ActivityPub\Type\Extended\Activity\Question;
use ActivityPub\Type\Extended\Activity\Read;
use ActivityPub\Type\Extended\Activity\Reject;
use ActivityPub\Type\Extended\Activity\Remove;
use ActivityPub\Type\Extended\Activity\TentativeAccept;
use ActivityPub\Type\Extended\Activity\TentativeReject;
use ActivityPub\Type\Extended\Activity\Travel;
use ActivityPub\Type\Extended\Activity\Undo;
use ActivityPub\Type\Extended\Activity\Update;
use ActivityPub\Type\Extended\Activity\View;use ActivityPub\Type\Extended\Object\Article;
use ActivityPub\Type\Extended\Object\Audio;
use ActivityPub\Type\Extended\Object\Document;
use ActivityPub\Type\Extended\Object\Event;
use ActivityPub\Type\Extended\Object\Image;
use ActivityPub\Type\Extended\Object\Mention;
use ActivityPub\Type\Extended\Object\Note;
use ActivityPub\Type\Extended\Object\Page;
use ActivityPub\Type\Extended\Object\Place;
use ActivityPub\Type\Extended\Object\Profile;
use ActivityPub\Type\Extended\Object\Relationship;
use ActivityPub\Type\Extended\Object\Tombstone;
use ActivityPub\Type\Extended\Object\Video;Whatever be your object or link, you can get all properties names with
getProperties() method.
use ActivityPub\Type;
$link = Type::create('Link');
print_r(
$link->getProperties()
);Would output something like:
Array
(
[0] => type
[1] => id
[2] => name
[3] => nameMap
[4] => href
[5] => hreflang
[6] => mediaType
[7] => rel
[8] => height
[9] => preview
[10] => width
)
In order to dump all properties and associated values, use toArray()
method.
use ActivityPub\Type;
$link = Type::create('Link');
$link->setName('An example');
$link->setHref('http://example.com');
print_r(
$link->toArray()
);Would output something like:
Array
(
[type] => Link
[name] => An example
[href] => http://example.com
)
There are 3 equivalent ways to get a value.
use ActivityPub\Type;
$note = Type::create('Note');
// Each method returns the same value
echo $note->id;
echo $note->get('id');
echo $note->getId();There are 3 equivalent ways to set a value.
use ActivityPub\Type;
$note = Type::create('Note');
$note->id = 'https://example.com/custom-notes/1';
$note->set('id', 'https://example.com/custom-notes/1');
$note->setId('https://example.com/custom-notes/1');Whenever you assign a value, the format of this value is checked.
This action is made by a validator. If rules are not respected an Exception is thrown.
With Type factory, you can instanciate a type and set several properties.
use ActivityPub\Type;
$note = Type::create('Note', [
'id' => 'https://example.com/custom-notes/1',
'name' => 'An important note',
]);use ActivityPub\Type;
$note = Type::create('Note');
echo $note->has('id'); // true
echo $note->has('anotherProperty'); // falseAll core and extended types are used with a classic instanciation.
use ActivityPub\Type\Extended\Object\Note;
$note = new Note();Same way with Type factory:
use ActivityPub\Type;
$note = Type::create('Note');If you need some custom attributes, you can extend predefined types.
- Create your custom type:
use ActivityPub\Type\Extended\Object\Note;
class MyNote extends Note
{
// Override basic type
protected $type = 'CustomNote';
// Custom property
protected $myProperty;
}There are 2 ways to instanciate a type:
- A classic PHP call:
$note = new MyNote();
$note->id = 'https://example.com/custom-notes/1';
$note->myProperty = 'Custom Value';
echo $note->getMyProperty(); // Custom Value- With the Type factory:
use ActivityPub\Type;
$note = Type::create('MyNote', [
'id' => 'https://example.com/custom-notes/1',
'myProperty' => 'Custom Value'
]);Extending types preserves benefits of getters, setters and their validators.
Use a custom property validator when you define custom attributes or when you want to override ActivityPub attribute default validation.
Regarding to previous example with a custom attribute $myProperty, if
you try to set this property, it would be done without any check on
values you're providing.
You can easily cope with that implementing a custom validator using
Validator.
use ActivityPub\Type\ValidatorInterface;
use ActivityPub\Type\Validator;
// Create a custom validator that implements ValidatorInterface
class MyPropertyValidator implements ValidatorInterface
{
// A public validate() method is mandatory
public function validate($value, $container)
{
return true;
}
}
// Attach this custom validator to a property
Validator::add('myProperty', MyPropertyValidator::class);
// Now all values are checked with the validate() method
// 'myProperty' is passed to the first argument
// $note is passed to the second one.
$note->myProperty = 'Custom Value';An equivalent way is to use Type factory and addValidator() method:
use ActivityPub\Type;
// Attach this custom validator to a property
Type::addValidator('myProperty', MyPropertyValidator::class);