|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +### Development |
| 8 | +```bash |
| 9 | +# Install dependencies |
| 10 | +composer install |
| 11 | + |
| 12 | +# Run static analysis |
| 13 | +composer phpstan |
| 14 | + |
| 15 | +# Generate PHPStan baseline for new violations |
| 16 | +composer phpstan:baseline |
| 17 | + |
| 18 | +# Run tests locally (requires vendor directory) |
| 19 | +vendor/bin/codecept run unit |
| 20 | + |
| 21 | +# Run specific test |
| 22 | +vendor/bin/codecept run unit ValidatorTest.php |
| 23 | + |
| 24 | +# Run tests with SLIC (if available) |
| 25 | +slic use validation |
| 26 | +slic composer install --ignore-platform-reqs |
| 27 | +slic run unit --ext DotReporter |
| 28 | +``` |
| 29 | + |
| 30 | +## Architecture Overview |
| 31 | + |
| 32 | +### Core Components |
| 33 | + |
| 34 | +1. **Config** (`src/Config.php`): Static configuration manager |
| 35 | + - Must call `Config::setServiceContainer()` before use |
| 36 | + - Initialize with `Config::initialize()` to register rules |
| 37 | + |
| 38 | +2. **Validator** (`src/Validator.php`): Main validation engine |
| 39 | + - Takes rules array, values array, and labels array |
| 40 | + - Key methods: `passes()`, `fails()`, `validated()`, `errors()` |
| 41 | + - Returns sanitized data via `validated()` |
| 42 | + |
| 43 | +3. **ValidationRulesRegistrar**: Rule registry accessed via service container |
| 44 | + - Prevents duplicate rule registration |
| 45 | + - Maps rule IDs to class implementations |
| 46 | + |
| 47 | +### Design Patterns |
| 48 | + |
| 49 | +- **Strategy Pattern**: Each rule implements `ValidationRule` interface |
| 50 | +- **Command Pattern**: Rules can return `ExcludeValue` or `SkipValidationRules` commands |
| 51 | +- **Abstract Factory**: Rules created from strings via `fromString()` method |
| 52 | +- **Registry Pattern**: Rules registered in `ValidationRulesRegistrar` |
| 53 | +- **Trait**: `HasValidationRules` adds validation to value objects |
| 54 | + |
| 55 | +### Rule System |
| 56 | + |
| 57 | +Rules can be specified as: |
| 58 | +- Strings: `'required'`, `'max:255'`, `'email'` |
| 59 | +- Instances: `new Max(255)`, `new Required()` |
| 60 | +- Closures: Custom validation logic |
| 61 | + |
| 62 | +Built-in rule categories: |
| 63 | +- Basic: Required, Optional, Nullable, Exclude |
| 64 | +- Type: Boolean, Integer, Numeric, Email, Currency, DateTime |
| 65 | +- Comparison: Min, Max, Size, In, InStrict |
| 66 | +- Conditional: ExcludeIf, NullableIf, OptionalIf (with Unless variants) |
| 67 | + |
| 68 | +### Key Interfaces |
| 69 | + |
| 70 | +- `ValidationRule`: Core rule interface with `__invoke()` method |
| 71 | +- `Sanitizer`: Rules that modify values during validation |
| 72 | +- `ValidatesOnFrontEnd`: Rules that can serialize to JSON for client-side validation |
| 73 | +- `ConditionalRule`: Abstract base for conditional validation rules |
| 74 | + |
| 75 | +### WordPress Integration |
| 76 | + |
| 77 | +- Uses `__()` for internationalization with `%TEXTDOMAIN%` placeholder |
| 78 | +- Hook system via configurable prefix: `{prefix}register_validation_rules` |
| 79 | +- Compatible with WordPress coding standards and PHPStan WordPress rules |
| 80 | + |
| 81 | +### Testing |
| 82 | + |
| 83 | +Tests use custom assertions in `TestCase`: |
| 84 | +- `assertValidationRulePassed($rule, $value, $values = [])` |
| 85 | +- `assertValidationRuleFailed($rule, $value, $values = [])` |
| 86 | + |
| 87 | +Mock rules available for testing validators (e.g., `MockRequiredRule`). |
| 88 | + |
| 89 | +### Extension Points |
| 90 | + |
| 91 | +1. Create custom rules by implementing `ValidationRule` |
| 92 | +2. Register rules via WordPress action hook |
| 93 | +3. Custom exceptions via `Config::setValidationExceptionClass()` |
| 94 | +4. Extend `ConditionalRule` for conditional validation logic |
0 commit comments