PHP code example of alexander89 / actyx

1. Go to this page and download the library: Download alexander89/actyx library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexander89 / actyx example snippets




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");

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";