patbator/activitystreams

活动流是一个简单的规范,用于描述网络中的社交动作。http://activitystrea.ms

1.0.4 2019-01-14 09:58 UTC

This package is auto-updated.

Last update: 2024-09-22 10:44:23 UTC


README

Activity Streams 2.0的PHP实现

此库旨在从JSON提供AS2词汇作为PHP对象,并将它们序列化为JSON。它不旨在成为完整的JSON-LD实现。

安装

使用Composer

$ composer require patbator/activitystreams

手动

克隆或下载仓库。

需要提供的PSR-4自动加载器来使用ActivityStreams对象

require_once '[path where you cloned or downloaded this library]/autoload.php';

用法

让我们从第一个https://www.w3.org/TR/activitystreams-core/示例开始。

对象到JSON

use Patbator\ActivityStreams\Model\Note;
use Patbator\ActivityStreams\Stream;

$note = new Note;
$note->content('My dog has fleas.');
$note->summary('A note');

$stream = new Stream($item);

echo $stream->render();

将输出

{
    "@context": "https://www.w3.org/ns/activitystreams",
    "content": "My dog has fleas.",
    "summary": "A note",
    "type": "Note"
}

JSON到对象

use Patbator\ActivityStreams\Stream;

$note = Stream::fromJson('{
  "@context": "https://www.w3.org/ns/activitystreams",
  "summary": "A note",
  "type": "Note",
  "content": "My dog has fleas."
}')->getRoot();

echo $note->content();

将输出

My dog has fleas.

查看测试以获取更多使用示例。

扩展

你可能需要为一种类型提供自己的类,例如,以实现https://www.w3.org/wiki/SocialCG/ActivityPub/Authentication_Authorization

假设你希望一个服务有一个`publicKey`属性。

use \Patbator\ActivityStreams\Model\Service;


class My_ActivityPub_Service extends Service 
{
  public function __construct() 
  {
    parent::__construct();
    $this->_actor_attribs[] = 'publicKey';
  }


  public function type()
  {
    return 'Service';
  }
}

你可以在类型工厂中声明它

use Patbator\ActivityStreams\Model\Base;
use Patbator\ActivityStreams\Model\Factory;

Base::setFactory((new Factory)
                 ->mapTypeToClass('Service', 'My_ActivityPub_Service'));

之后,工厂将为任何具有"类型":"服务"的JSON对象创建一个新的`My_ActiviyPub_Service`,而不是`Patbator\ActivityStreams\Model\Service`。

测试

$ composer test

许可证

MIT许可证(MIT)。请参阅LICENSE.md以获取更多信息。