PHP code example of alexfn / nano-service

1. Go to this page and download the library: Download alexfn/nano-service 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/ */

    

alexfn / nano-service example snippets


$message = new NanoServiceMessage(
    // Body data
    [
        'key' => 'Value',
    ],
    // Message property (Optional)
    [
        'content_type' => 'text/json',
        'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
    ]
);

$message->addPayload([
    'key1' => 'Value 1',
    'key2' => 'Value 2',
]);

$message = (new NanoServiceMessage())
    ->addPayload([
        'key' => 'Value',
    ]);

(new NanoPublisher())
    ->setMessage($message)
    ->publish('event-name');

$consumer = new NanoConsumer();
$consumer
    ->events('event-one', 'event-two')
    ->consume(function (NanoServiceMessage $message) {
        $payload = $message->getPayload();// array
    });

$message = (new NanoServiceMessage())
    ->addPayload([
        'key' => 'Value',
    ]);

$message = (new NanoServiceMessage())
    ->addMeta([
        'key' => 'Value',
    ]);

$message->getPayload();
$message->getPayloadAttribute('key');
$message->getPayloadAttribute('key', 'default_value');
$message->addPayload([]);
$message->getPayloadAttribute('attribute', []);

$message->getMeta();
$message->getMetaAttribute('key');
$message->getMetaAttribute('key', 'default_value');
$message->addMeta([]);
$message->getMetaAttribute('attribute', []);

$message->getStatusCode(); // Default 'unknown'
$message->setStatusCode('success');
$message->getStatusData(); // Default []
$message->setStatusData([]);

// Encrypting a message with a private key
$message = (new NanoServiceMessage())
    ->setEncryptedAttribute('attribute', 'My secret data');

// Decrypting with the public key
$message->getEncryptedAttribute('attribute'); // My secret data

$message = (new NanoServiceMessage())
    ->addPayload([
        'key1' => 'Value 1',
        'key2' => 'Value 2',
    ])
    ->addPayload([
        'key1' => 'New value 1',
        'key3' => 'New value 3',
    ]);

// Result: {"key1":"Value 1","key2":"Value 2","key3":"New value 3"}

$message = (new NanoServiceMessage())
    ->addPayload([
        'key1' => 'Value 1',
        'key2' => 'Value 2',
    ])
    ->addPayload(
        [
            'key1' => 'New value 1',
            'key3' => 'New value 3',
        ],
        true
    );

// Result: {"key1":"New value 1","key2":"Value 2","key3":"New value 3"}

$message = (new NanoServiceMessage())
    ->setStatusData([
        'key1' => 'Value 1',
        'key2' => 'Value 2',
    ])
    ->setStatusData(
        [
            'key1' => 'New value 1',
            'key3' => 'New value 3',
        ],
        true
    );

// Result: {"key1":"New value 1","key3":"New value 3"}

$consumer
    ->events('event-name')
    ->consume($callback, function (NanoServiceMessage $message) {
        // debugCallback (Optional)
    });