PHP code example of particle-academy / last-word

1. Go to this page and download the library: Download particle-academy/last-word 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/ */

    

particle-academy / last-word example snippets


use LastWord\Agent;

$doc = [
    'title' => 'Quarterly Notes',
    'blocks' => [
        ['type' => 'heading', 'level' => 1, 'runs' => [['text' => 'Summary']]],
        ['type' => 'paragraph', 'runs' => [
            ['text' => 'Revenue was '],
            ['text' => 'up 12%', 'bold' => true],
            ['text' => ' — details in the '],
            ['text' => 'appendix', 'link' => 'https://example.com/appendix'],
            ['text' => '.'],
        ]],
        ['type' => 'list', 'items' => [
            ['runs' => [['text' => 'Ship the docx engine']], 'children' => [
                ['runs' => [['text' => 'Reader + writer']]],
            ]],
            ['runs' => [['text' => 'Wire the Editor bridge']]],
        ]],
    ],
];

// Validate before writing — catches malformed agent output
$errors = Agent::validate($doc);   // [] when valid: [{path, message}, …] otherwise

// Write to disk (synchronous)
$result = Agent::write($doc, storage_path('app/notes.docx'));
// ['path' => …, 'bytes' => 4151, 'blocks' => 3]

// Or keep it in memory
$bytes = Agent::toBytes($doc);

// And read any .docx back into the same model — including Word-authored files
$model = Agent::read($bytes);      // or Agent::read('/path/to/file.docx')

use LastWord\Agent;

// Inbound: a Word file arrives → markdown for the Editor
$doc = Agent::read($uploadedBytes);
$markdown = Agent::toMarkdown($doc);

// … the Editor (or an agent) edits the markdown …

// Outbound: markdown → model → a real .docx
$doc = Agent::fromMarkdown($markdown);
$bytes = Agent::toBytes($doc);

$images = [
    'logo' => ['data' => file_get_contents('logo.png'), 'widthPx' => 150, 'heightPx' => 50, 'alt' => 'Acme Ltd'],
    'seal' => 'data:image/jpeg;base64,...',
];

Agent::guide(['images' => $images]);      // the guide, plus a section listing image:logo and image:seal
Agent::jsonSchema(['images' => $images]); // the image src takes exactly those names, or a data URL
Agent::toBytes([
    'page' => ['header' => ['blocks' => [['type' => 'image', 'src' => 'image:logo']]]],
    'blocks' => $blocks,
], ['images' => $images]);

$cat = Agent::catalog($docx);                       // headings + tables, each with its selector
$x   = Agent::extract($docx, ['underHeading' => 'Results', 'nth' => 0]);
// {ok: true, content: {fancyContent: 1, ...}, report}  or  {ok: false, refused: {reason}}

$doc['blocks'][] = ['type' => 'portable', 'content' => $sheetRange];  // e.g. from holy-sheet extract()
Agent::toBytes(Agent::validateAndRepair($doc)['schema']);

use LastWord\Exchange\ExportPolicy;

$policy = new ExportPolicy(['allow' => ['markdown', 'html']]);
Agent::export($bytes, 'html', ['selector' => ['table' => 0]], $policy);
// {ok: true, target, mime, text|bytes, report}  or  {ok: false, refused: {reason, target, message, ...}}
Agent::exportToolDefinition($policy);   // the tool an agent sees: only enabled targets

$l = Agent::layout($doc);                       // or Agent::layout($doc, $writeOptions)
// MOIC's letterhead memo (tests/fixtures/layout/viewer-agreement.json, "memo-letterhead"):
$l['pages'];                                    // 2
$l['pageStarts'];                               // [0, 9]: block 9 (the table) starts page 2
$l['pageList'][1];                              // ['page' => 2, 'blocks' => [9, ..., 20], 'continued' => true,
                                                //  'footnotes' => [], 'used' => 588.9, 'capacity' => 648, 'fill' => 0.909]
                                                // continued: the table split, its header row repeated
$l['approximate'];                              // [] = exact; else e.g. ['font:Garamond', 'code']

Agent::toBytes($doc, ['template' => $dotxBytesOrPath]);
Agent::write($doc, $path, ['template' => '/templates/house.dotx']);

Agent::toBytes($doc, [
    'template' => $letterheadDocxOrDotx,
    'templateParts' => ['styles', 'headers', 'footers', 'page'],
]);

Agent::toBytes($doc, [
    'template' => $letterhead,
    'templateParts' => ['styles', 'headers', 'footers', 'page'],
    'templateFirstPageOnly' => true,
]);

try {
    Agent::toBytes($probeDoc, ['template' => $uploaded]);
} catch (TemplateException $e) {
    // reject the upload, with $e->getMessage() explaining why
}

$old = Agent::read($currentBytes);
$ops = Agent::diff($edited, $old);   // store these with the version

Agent::reduce($edited, $ops);        // equals $old

Agent::read('letterhead.dotx');            // [..., 'sourceKind' => 'template']
Agent::convertTemplate('letterhead.dotx'); // .docx bytes ('document' | 'template' | null = flip)