Demonstrates all available approaches for mapping HTTP request parameters to PHP objects in WebFiori HTTP.
- 5 Different Object Mapping Approaches
- Parameter validation and type safety
- Custom setter method mapping
- Clean separation of data and logic
- Modern attribute-based mapping (NEW)
User.php- Data model class with validationTraditionalMappingService.php- Traditional parameter mappingManualMappingService.php- Manual object mappingGetObjectMappingService.php- getObject() mappingMapEntityMappingService.php- MapEntity attribute mappingMapEntityCustomMappingService.php- MapEntity with custom settersindex.php- Main application entry point
php -S localhost:8080Method signature with individual parameters
#[RequestParam('name', 'string', false)]
#[RequestParam('email', 'string', false)]
#[RequestParam('age', 'int', false)]
public function create(string $name, string $email, int $age): arrayManual parameter extraction and object creation
public function create(): array {
$inputs = $this->getInputs();
$user = new User();
if ($inputs->hasKey('name')) $user->setName($inputs->get('name'));
// ... manual mapping
}Framework-assisted object mapping
public function create(): array {
$user = $this->getObject(User::class);
// Object automatically mapped from request
}Clean attribute-based mapping (NEW)
#[MapEntity(User::class)]
public function create(User $user): array {
// $user automatically mapped and injected
}Flexible parameter naming with custom setters (NEW)
#[MapEntity(User::class, setters: ['full-name' => 'setFullName', 'email-address' => 'setEmailAddress'])]
public function create(User $user): array {
// Custom parameter mapping handled automatically
}# 1. Traditional Parameters
curl -X POST "http://localhost:8080?service=traditional" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com", "age": 30}'
# 2. Manual Mapping
curl -X POST "http://localhost:8080?service=manual" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com", "age": 30}'
# 3. getObject() Mapping
curl -X POST "http://localhost:8080?service=getobject" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com", "age": 30}'
# 4. MapEntity Basic
curl -X POST "http://localhost:8080?service=mapentity" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com", "age": 30}'
# 5. MapEntity Custom Setters
curl -X POST "http://localhost:8080?service=mapentity-custom" \
-H "Content-Type: application/json" \
-d '{"full-name": "Jane Smith", "email-address": "jane@example.com", "user-age": 25}'| Approach | Pros | Cons | Best For |
|---|---|---|---|
| Traditional Parameters | ✅ Explicit, IDE support | ❌ Verbose for many params | Simple endpoints |
| Manual Mapping | ✅ Full control, flexible | ❌ Boilerplate code | Complex validation |
| getObject() Mapping | ✅ Automatic, less code | ❌ Silent error handling | Standard mapping |
| MapEntity Basic | ✅ Clean, type-safe | ❌ Less flexible | Modern development |
| MapEntity Custom | ✅ Flexible + clean | ❌ Setup complexity | Legacy API integration |
All approaches produce the same result structure:
{
"message": "User created with [approach name]",
"user": {
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"phone": null,
"address": null
},
"method": "[approach_identifier]"
}1. Traditional Approach - Explicit parameter handling
public function create(string $name, string $email, int $age): array {
$user = new User();
$user->setName($name);
$user->setEmail($email);
$user->setAge($age);
}2. Manual Mapping - Direct input processing
public function create(): array {
$inputs = $this->getInputs();
$user = new User();
if ($inputs->hasKey('name')) $user->setName($inputs->get('name'));
}3. Framework Mapping - Automated object creation
public function create(): array {
$user = $this->getObject(User::class);
// Framework handles mapping automatically
}4. Modern Attribute Mapping - Clean and type-safe
#[MapEntity(User::class)]
public function create(User $user): array {
// $user is automatically mapped and validated
}- Traditional: Simple endpoints with few parameters
- Manual: Complex validation or transformation logic
- getObject(): Standard mapping with framework control
- MapEntity: Modern development with type safety (RECOMMENDED)