与actyx协同工作的sdk。

0.2.0 2021-08-11 00:47 UTC

This package is not auto-updated.

Last update: 2024-10-03 13:37:28 UTC


README

WebSocket实现 https://developer.actyx.com/docs/reference/events-api

示例

<?php

namespace Example;

use Actyx;

// create a AppManifest
$manifest = new Actyx\AppManifest('com.example.php.01', 'php test', '0.0.1');
// connect to actyx
$actyx = new Actyx\Client($manifest);

// request some node data
$actyx->nodeId();
$actyx->manifest();
$actyx->preset();

// publish an event
$actyx->publish(
  array("alex.php.test"),
  array('eventType' => 'phpHello', 'sender' => 'alex')
);

// query all events
$events = $actyx->query("FROM allEvents");

事件类型

您可以输入事件以将它们获取到事件类中。您可以在流中解析一个您期望的类数组,不匹配的事件将被过滤掉。

事件类必须在构造函数中消费rawEvent。事件类应尝试使用数据构造实例。如果失败/不匹配,它必须抛出错误,并使用下一个类进行下一次尝试。如果没有任何事件与事件匹配,则它将被过滤掉。

示例

class AddEvent
{
  public string $eventType;
  public int $amount;
  function __construct($rawEvent)
  {
    // throws an exception if it don't matches
    Actyx\expect($rawEvent->eventType, 'add');
    $this->eventType = $rawEvent->eventType;
    $this->amount = $rawEvent->amount;
  }
}
class RemoveEvent
{
  public string $eventType;
  public int $amount;
  function __construct($rawEvent)
  {
    // mor complex expect
    Actyx\expect($rawEvent->eventType, function ($v) { return $v ==='remove' });
    $this->eventType = $rawEvent->eventType;
    $this->amount = $rawEvent->amount;
  }
}
// query typed events
$actyxEvents = $actyx->query(
  // aql query
  "FROM 'ax.demo.add' | 'ax.demo.remove'",
  // event order
  'asc',
  // lowerBound
  null,
  // upperBound
  null,
  // classes to merge the events in
  array(
    AddEvent::class,
    RemoveEvent::class
  )
);

$res = 0;
foreach ($actyxEvents as &$actyxEvent) {
  $event = $actyxEvent->payload;
  switch (get_class($event)) {
    case AddEvent::class:
      $res += $event->amount;
      break;
    case RemoveEvent::class:
      $res -= $event->amount;
      break;
  }
}
echo "The result is {$res} \n";