PHP code example of golded-dev / laravel-ftn

1. Go to this page and download the library: Download golded-dev/laravel-ftn 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/ */

    

golded-dev / laravel-ftn example snippets




declare(strict_types=1);

use Golded\Ftn\Contracts\MessageBaseReader;
use Golded\Ftn\ParsedMessage;
use Golded\Ftn\ReaderOptions;

final class ExampleReader implements MessageBaseReader
{
    /**
     * @return iterable<ParsedMessage>
     */
    public function read(string $path, ?ReaderOptions $options = null): iterable
    {
        $options ??= new ReaderOptions();

        yield new ParsedMessage(
            msgno: 1,
            fromName: 'Sysop',
            toName: 'All',
            subject: 'Hello',
            bodyText: 'Message body',
            attributesRaw: 0,
            externalId: 'example:1',
            areaCode: 'GENERAL',
            areaName: 'General',
        );
    }
}



declare(strict_types=1);

use Golded\Ftn\Contracts\MessageSourceCatalog;
use Golded\Ftn\MessageSource;
use Golded\Ftn\ReaderOptions;

final class ExampleCatalog implements MessageSourceCatalog
{
    /**
     * @return iterable<MessageSource>
     */
    public function sources(string $path, ?ReaderOptions $options = null): iterable
    {
        yield new MessageSource(
            sourceType: 'example',
            path: $path.'/general',
            code: 'GENERAL',
            name: 'General',
            sortOrder: 10,
            metaKey: 'example:general',
        );
    }
}

use Golded\Ftn\ReaderOptions;

$options = new ReaderOptions(
    fallbackCharset: 'CP850',
);

use Golded\Ftn\FtnAddress;

$address = FtnAddress::fromString('2:236/77.1@fidonet');

$address->zone; // 2
$address->net; // 236
$address->node; // 77
$address->point; // 1
$address->domain; // fidonet
$address->toString(); // 2:236/77.1@fidonet

use Golded\Ftn\ParsedMessage;

$message = new ParsedMessage(
    msgno: 42,
    fromName: 'Alice',
    toName: 'Bob',
    subject: 'Re: routing',
    bodyText: "Seen-by lines removed elsewhere\n",
    attributesRaw: 0,
    externalId: '2:203/0 12345678',
    fromAddress: '2:203/0',
    toAddress: '2:203/1',
    areaCode: 'NETMAIL',
    areaName: 'Netmail',
);



declare(strict_types=1);

use Golded\Ftn\Contracts\MessageWriter;
use Golded\Ftn\OutgoingMessage;
use Golded\Ftn\WriterOptions;

final class ExampleWriter implements MessageWriter
{
    /**
     * @param iterable<OutgoingMessage> $messages
     */
    public function write(string $path, iterable $messages, ?WriterOptions $options = null): int
    {
        $written = 0;

        foreach ($messages as $message) {
            $written++;
        }

        return $written;
    }
}

use Golded\Ftn\Support\CharsetDetector;

$charset = CharsetDetector::detect("\x01CHRS: LATIN-1 2\nBody");

// ISO-8859-1

$charset = CharsetDetector::detect("\x01CHRS: MYSTERY\nBody", 'CP437');

// CP437

use Golded\Ftn\Support\MojibakeRepairer;

$result = MojibakeRepairer::repair('Bruger m°de');

$result->text; // Bruger møde
$result->changed; // true
$result->confidence; // 0.0-1.0

use Golded\Ftn\Support\Text;

$name = Text::readNullPaddedField($rawHeader, offset: 0, length: 36);
$body = Text::parseBody($rawBody);
$utf8 = Text::toUtf8($rawSubject, 'CP850');
$id = Text::syntheticId($from, $to, $subject, $date, $body);

use Golded\Ftn\Support\ControlLines;

$msgid = ControlLines::extractMsgid($rawBody);

$controlLines = ControlLines::parseMessage($rawBody);

$controlLines->msgid;
$controlLines->reply;
$controlLines->charset;
$controlLines->seenBy;
$controlLines->path;
$controlLines->tearline;
$controlLines->origin;
$controlLines->originAddress?->toString();