portable-content / portable-content-php
PHP implementation of portable content system - Mutable Architecture with AbstractBlock v0.4.0
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.9
- ramsey/uuid: ^4.7
- symfony/validator: ^7.3
Requires (Dev)
- ergebnis/composer-normalize: ^2.42
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.4
- phpunit/phpunit: ^11.0
- roave/security-advisories: dev-master
README
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
- Installation
- Database Setup
- Quick Start
- Documentation
- Development
- Project Structure
- Features
- Requirements
- License
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:
- Getting Started Guide - Complete setup and basic usage
- API Reference - Detailed API documentation
- Validation System - Input validation and sanitization
- Repository Pattern - Data persistence and retrieval
- Architecture Overview - System design and components
- Examples - Common usage patterns and recipes
- Future Features - Planned features and roadmap
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