1. Go to this page and download the library: Download freema/n8n-bundle 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/ */
freema / n8n-bundle example snippets
use Freema\N8nBundle\Contract\N8nPayloadInterface;
use Freema\N8nBundle\Contract\N8nResponseHandlerInterface;
use Freema\N8nBundle\Enum\RequestMethod;
class ForumPost implements N8nPayloadInterface
{
public function toN8nPayload(): array
{
return [
'text' => $this->content,
'author_id' => $this->authorId,
'created_at' => $this->createdAt->format(DATE_ATOM)
];
}
public function getN8nContext(): array
{
return [
'entity_type' => 'forum_post',
'entity_id' => $this->id,
'action' => 'moderate'
];
}
// Optional: define HTTP method and content type
public function getN8nRequestMethod(): RequestMethod
{
return RequestMethod::POST_FORM; // or POST_JSON, GET, etc.
}
// Optional: custom response handler
public function getN8nResponseHandler(): ?N8nResponseHandlerInterface
{
return new ModerationResponseHandler();
}
// Optional: response entity mapping
public function getN8nResponseClass(): ?string
{
return ModerationResponse::class;
}
}
class MyResponseHandler implements N8nResponseHandlerInterface
{
public function handleN8nResponse(array $responseData, string $requestUuid): void
{
// Process response from n8n
}
public function getHandlerId(): string
{
return 'my_handler';
}
}
$uuid = $n8nClient->sendWithCallback($payload, 'workflow-id', new MyResponseHandler());
use Freema\N8nBundle\Enum\RequestMethod;
class MyPayload implements N8nPayloadInterface
{
public function getN8nRequestMethod(): RequestMethod
{
return RequestMethod::POST_FORM; // Form data (application/x-www-form-urlencoded)
// return RequestMethod::POST_JSON; // JSON body (application/json)
// return RequestMethod::GET; // GET parameters
// return RequestMethod::PUT_JSON; // PUT with JSON
// return RequestMethod::PATCH_FORM; // PATCH with form data
}
}
// 1. Create response entity
class ModerationResponse
{
public function __construct(
public readonly bool $allowed,
public readonly ?string $reason = null,
public readonly ?string $confidence = null
) {}
}
// 2. Specify class in payload
class ForumPost implements N8nPayloadInterface
{
public function getN8nResponseClass(): ?string
{
return ModerationResponse::class;
}
}
// 3. Use mapped object
$result = $n8nClient->send($post, 'workflow-id');
$mappedResponse = $result['mapped_response']; // Instance of ModerationResponse
$isAllowed = $mappedResponse->allowed; // Type-safe access
// Event listener
class N8nMonitoringListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
N8nRequestSentEvent::NAME => 'onRequestSent',
N8nResponseReceivedEvent::NAME => 'onResponseReceived',
N8nRequestFailedEvent::NAME => 'onRequestFailed',
N8nRetryEvent::NAME => 'onRetry',
];
}
public function onRequestSent(N8nRequestSentEvent $event): void
{
// Log, metrics, monitoring...
}
}
// 1. Post implements N8nPayloadInterface
class ForumPost implements N8nPayloadInterface
{
public function toN8nPayload(): array
{
return [
'text' => $this->content,
'author_id' => $this->authorId,
'thread_id' => $this->threadId
];
}
}
// 2. Handler for processing results
class ForumPostModerationHandler implements N8nResponseHandlerInterface
{
public function handleN8nResponse(array $responseData, string $requestUuid): void
{
$status = $responseData['status']; // 'ok', 'suspicious', 'blocked'
$spamScore = $responseData['spam_score'];
$flags = $responseData['flags'];
// Based on result: publish/block/send for manual review
match($responseData['suggested_action']) {
'approve' => $this->approvePost($postId),
'manual_review' => $this->queueForManualReview($postId, $flags),
'block' => $this->blockPost($postId, $flags)
};
}
}
// 3. Usage
$post = new ForumPost(/*...*/);
$handler = new ForumPostModerationHandler();
$uuid = $n8nClient->sendWithCallback($post, 'moderation-workflow-id', $handler);
use Freema\N8nBundle\Testing\MockN8nClient;
class MyServiceTest extends TestCase
{
private MockN8nClient $mockN8n;
protected function setUp(): void
{
$this->mockN8n = new MockN8nClient();
}
public function testForumPostModeration(): void
{
// Configure the mock response
$this->mockN8n->willReturn([
'status' => 'approved',
'spam_score' => 0.1,
'confidence' => 'high'
]);
// Test your service
$service = new ModerationService($this->mockN8n);
$result = $service->moderatePost($forumPost);
// Verify the request was sent
$this->mockN8n->assertSent('moderation-workflow-id');
$this->mockN8n->assertSentCount(1);
// Verify payload content
$this->mockN8n->assertSentWithPayload('moderation-workflow-id', [
'text' => 'Forum post content'
]);
}
}
// Assert request was sent
$mockClient->assertSent('workflow-id');
// Assert with custom callback
$mockClient->assertSent('workflow-id', function (array $request) {
return $request['payload']->getSomeValue() === 'expected';
});
// Assert request was not sent
$mockClient->assertNotSent('workflow-id');
// Assert number of requests
$mockClient->assertSentCount(3);
// Assert nothing was sent
$mockClient->assertNothingSent();
// Assert payload data
$mockClient->assertSentWithPayload('workflow-id', [
'key' => 'expected-value'
]);
// Get all requests
$requests = $mockClient->getRequests();
// Get requests for specific workflow
$requests = $mockClient->getRequestsFor('workflow-id');
// Reset state between tests
$mockClient->reset();
// Custom client ID
$mockClient->withClientId('test-client');
// Health status
$mockClient->withHealthStatus(false);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.