PHP code example of portable-content / portable-content-php

1. Go to this page and download the library: Download portable-content/portable-content-php 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/ */

    

portable-content / portable-content-php example snippets




ortableContent\Block\Markdown\MarkdownBlock;
use PortableContent\ContentItem;
use PortableContent\Tests\Support\Repository\RepositoryFactory;

// Create a markdown block
$block = MarkdownBlock::create('# Hello World\n\nThis is my first note!');

// Create content with the block
$content = ContentItem::create('note', 'My First Note', 'A simple example', [$block]);

// Set up repository (in-memory for this example)
$repository = RepositoryFactory::createInMemoryRepository();

// Save content
$repository->save($content);

// Retrieve content
$retrieved = $repository->findById($content->id);
echo "Retrieved: {$retrieved->title}\n";

use PortableContent\Validation\ContentValidationService;
use PortableContent\Validation\ContentSanitizer;
use PortableContent\Validation\BlockSanitizerManager;
use PortableContent\Validation\Adapters\SymfonyValidatorAdapter;
use PortableContent\Block\Markdown\MarkdownBlockSanitizer;
use Symfony\Component\Validator\Validation;

// Set up validation service
$blockSanitizerManager = new BlockSanitizerManager([new MarkdownBlockSanitizer()]);
$contentSanitizer = new ContentSanitizer($blockSanitizerManager);
$symfonyValidator = Validation::createValidator();
$contentValidator = new SymfonyValidatorAdapter($symfonyValidator);
$validationService = new ContentValidationService($contentSanitizer, $contentValidator);

// Raw input data (as from API/form)
$rawData = [
    'type' => '  note  ',  // Will be sanitized
    'title' => 'My Note',
    'blocks' => [
        [
            'kind' => 'markdown',
            'source' => '# Hello World\n\nContent here.'
        ]
    ]
];

// Validate and sanitize
$result = $validationService->validateContentCreation($rawData);

if ($result->isValid()) {
    $sanitizedData = $result->getData();
    // Create domain objects from validated data...
} else {
    $errors = $result->getErrors();
    // Handle validation errors...
}