portable-content/portable-content-php

PHP implementation of portable content system - Mutable Architecture with AbstractBlock v0.4.0

0.4.0 2025-08-24 07:07 UTC

This package is not auto-updated.

Last update: 2025-08-24 10:19:58 UTC


README

Version codecov PHP Version PHPStan Level Tests License

A robust PHP library for managing portable content with comprehensive validation, sanitization, and flexible storage backends.

โœจ Key Features

  • ๐Ÿ—๏ธ Mutable Domain Objects - Flexible ContentItem and MarkdownBlock entities with proper encapsulation
  • ๐Ÿงฑ AbstractBlock Architecture - Extensible base class for all block types
  • ๐Ÿ”’ Type-Safe Validation - Comprehensive input validation and sanitization
  • ๐Ÿ’พ Repository Pattern - Clean abstraction with capability discovery system
  • ๐Ÿงช Comprehensive Testing - 315 tests with 1,669 assertions
  • ๐Ÿ“š Complete Documentation - 7 detailed guides covering all aspects
  • โšก Production Ready - PHPStan Level 9, zero static analysis errors
  • ๐Ÿงน Clean Architecture - Focused, maintainable codebase with extensible design

Requirements

  • PHP 8.3 or higher
  • Composer
  • SQLite (included with PHP)

Installation

git clone https://github.com/portable-content/portable-content-php.git
cd portable-content-php
composer install

Database Setup

Initialize Database

# Create database with migrations
composer migrate

# Or specify custom path
php bin/migrate.php --path=storage/custom.db

# Test with in-memory database
composer migrate-test

Database Schema

The system uses SQLite with two main tables:

  • content_items: Stores ContentItem metadata (id, type, title, summary, timestamps)
  • markdown_blocks: Stores MarkdownBlock content (id, content_id, source, timestamp)

Foreign key constraints ensure data integrity between content and blocks.

Quick Start

Basic Usage

<?php

require_once 'vendor/autoload.php';

use PortableContent\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";

With Validation

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...
}

Table of Contents

Features

โœ… Immutable Domain Objects - Thread-safe, predictable content management โœ… Type-Safe API - Full PHP 8.3+ type hints and strict typing โœ… Input Validation - Comprehensive sanitization and validation pipeline โœ… Repository Pattern - Clean data access abstraction โœ… Transaction Safety - ACID-compliant database operations โœ… Extensible Blocks - Plugin system for different content types โœ… Production Ready - 315+ tests, PHPStan Level 9, CI/CD pipeline โœ… Developer Friendly - Comprehensive documentation and examples

Documentation

For complete documentation, see the docs/ directory:

For AI/LLM Developers

  • llms.txt - Essential guide for Large Language Models working with this library

Development

# Run tests
composer test

# Run tests with coverage
composer test-coverage

# Run specific test suite
./vendor/bin/phpunit --testsuite=Unit

# Code quality checks
composer cs-check          # Check code style
composer cs-fix            # Fix code style
composer phpstan           # Run static analysis
composer security-audit    # Check for security issues

# Composer maintenance
composer composer-normalize # Check composer.json format
composer composer-normalize-fix # Fix composer.json format

Project Structure

src/                          # Source code
โ”œโ”€โ”€ Block/                   # Block implementations (MarkdownBlock)
โ”œโ”€โ”€ Contracts/               # Interfaces and contracts
โ”œโ”€โ”€ Exception/               # Exception classes
โ”œโ”€โ”€ Validation/              # Validation and sanitization system
โ””โ”€โ”€ ContentItem.php          # Main domain object
tests/                        # Test files (315 tests, 1,283 assertions)
โ”œโ”€โ”€ Support/                  # Test support utilities
โ”‚   โ”œโ”€โ”€ Database/            # SQLite database helpers
โ”‚   โ””โ”€โ”€ Repository/          # Repository factory for testing
โ”œโ”€โ”€ Unit/                    # Unit tests
โ””โ”€โ”€ Integration/             # Integration tests (end-to-end workflows)
docs/                        # Complete documentation
โ”œโ”€โ”€ getting-started.md       # Setup and basic usage
โ”œโ”€โ”€ api-reference.md         # Complete API documentation
โ”œโ”€โ”€ validation.md            # Validation system guide
โ”œโ”€โ”€ examples.md              # Usage examples and patterns
โ”œโ”€โ”€ architecture.md          # System design and components
โ”œโ”€โ”€ repository.md            # Repository pattern details
โ””โ”€โ”€ future-features.md       # Roadmap and planned features
storage/                     # SQLite database files
bin/                         # CLI tools
โ””โ”€โ”€ migrate.php              # Database migration tool
migrations/                  # Database schema migrations
llms.txt                     # Guide for AI/LLM developers

Development Status

Phase 1A: COMPLETE โœ…

This library represents the completed Phase 1A implementation with all goals achieved:

  • โœ… Basic content entity storage (ContentItem, MarkdownBlock)
  • โœ… SQLite database with proper schema and migrations
  • โœ… Repository pattern for data access
  • โœ… Comprehensive input validation and sanitization
  • โœ… Extensive testing (315+ tests, 1,283+ assertions)
  • โœ… Production-ready code quality (PHPStan Level 9, CS-Fixer)

Next Phase: GraphQL API

Phase 1B will add a GraphQL API layer on top of this solid foundation. See docs/future-features.md for the complete roadmap.

๐Ÿ“ฆ Installation

composer require portable-content/portable-content-php

Or clone the repository:

git clone https://github.com/portable-content/portable-content-php.git
cd portable-content-php
composer install

Contributing

This library follows strict quality standards:

  • PHP 8.3+ with strict typing
  • PHPStan Level 9 static analysis
  • PHP-CS-Fixer code style compliance
  • Comprehensive testing with unit and integration tests
  • Mutable entity design with proper encapsulation
  • Clean architecture with clear separation of concerns

License

Apache 2.0 License