This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a PHP OpenRouter Client library that provides an easy-to-use interface for integrating OpenRouter (a unified interface for LLMs) into PHP applications. The library follows modern PHP practices with PHP 8.2+ requirements and uses Guzzle HTTP for API communication.
# Run all tests
composer test
# OR
phpunit
# Run specific test
phpunit tests/DTO/ChatTest.php# Run PHPStan (included in dev dependencies)
vendor/bin/phpstan analyze# Install dependencies
composer install
# Update dependencies
composer updateMain Client Classes:
OpenRouter(src/OpenRouter.php): Main client class with resource properties (chat,models)OpenRouterConfig(src/OpenRouterConfig.php): Configuration class handling API keys, timeouts, retry logic with Guzzle
Resource Pattern:
Resources/AbstractResource.php: Base class for API resourcesResources/ChatResource.php: Handles chat completionsResources/ModelsResource.php: Handles model information
DTO Architecture: The library heavily uses DTOs (Data Transfer Objects) for structured data:
ChatRequest.php: Main request object supporting both prompt strings and Chat objectsChat.php: Contains array of Message objects for conversation continuityMessage.php: Individual messages with role, content, stamps metadataChatResponse.php: API response wrapper
Content System:
Contracts/ContentInterface.php: Interface for message contentTextContent.php&ImageContent.php: Different content typesContracts/StampInterface.php: Metadata stamps on messages
Configuration DTOs:
ResponseFormat.php,Schema.php,Property.php: For structured outputToolCall.php,FunctionData.php: For function callingProviderPreferences.php: OpenRouter-specific settings
- Resource Pattern: API endpoints are organized as resource classes accessed via the main client
- DTO Pattern: Heavy use of DTOs for type safety and serialization/deserialization
- Factory Pattern: OpenRouterConfig creates configured Guzzle clients with retry middleware
- Conversation Continuity: Chat objects maintain message history and can be serialized/deserialized
Tests are organized by DTO classes in tests/DTO/ directory. Each major DTO class has comprehensive unit tests covering serialization, validation, and behavior.
Runtime:
guzzlehttp/guzzle ^7.9: HTTP clientcaseyamcl/guzzle_retry_middleware ^2.12: Retry logic for API calls
Development:
phpunit/phpunit ^11.5: Testing frameworkphpstan/phpstan ^2.1: Static analysismockery/mockery ^1.6: Mocking for tests
The library supports environment variables for configuration:
OPENROUTER_API_KEY: API key for OpenRouterOPENROUTER_API_ENDPOINT: Custom API endpoint (defaults to https://openrouter.ai/api/v1/)
These can be set in phpunit.xml for testing or passed directly to OpenRouterConfig constructor.