alexanevsky/input-manager-bundle
Installation:
composer require alexanevsky/input-manager-bundle
Add the bundle to config/bundles.php:
return [
// ...
Alexanevsky\InputManagerBundle\InputManagerBundle::class => ['all' => true],
];
First Use Case:
Inject InputManager into a controller or service:
use Alexanevsky\InputManagerBundle\InputManager;
public function __construct(private InputManager $inputManager) {}
Basic Deserialization:
Create an InputInterface class (e.g., UserInput) with public properties matching your request data.
class UserInput implements InputInterface {
public string $firstName;
public string $lastName;
}
Deserialize JSON input:
$json = '{"firstName": "John", "last_name": "Doe"}';
$input = $this->inputManager->deserializeInput($json, UserInput::class);
Request Handling:
deserializeInput() to convert raw request data (JSON, form, etc.) into an InputInterface object.$input = $this->inputManager->deserializeInput(
$request->getContent(),
UserInput::class
);
Validation:
@Assert\NotBlank) to InputInterface properties.$errors = $this->inputManager->validate($input);
InputValidatorInterface:
$errors = $this->inputManager->validate($input, CustomValidator::class);
Mapping to Models:
map() method to transform validated input into a model/entity:
$user = $this->inputManager->map($input, User::class);
Nested and Collections:
InputInterface classes for each level.AbstractInputCollection:
class CategoryInputCollection extends AbstractInputCollection {
public function getClass(): string { return CategoryInput::class; }
}
Entity Resolution:
@EntityFromId to resolve entities by ID:
#[EntityFromId(Category::class)]
public Category $category;
InputInterface as a DTO for form data binding.InputManager to test validation and mapping logic in isolation.Property Naming:
last_name → lastName). Override with explicit setters if needed.Type Conversion:
"") are converted to false for boolean properties. Use @Assert\NotBlank to enforce non-empty values.Entity Resolution:
id) matches the expected key in the input (e.g., category_id). Customize with @EntityFromId attributes.Validation Order:
Circular References:
UserInput referencing ArticleInput which references UserInput).$errors for TranslatableMessage objects. Use $errors['property']->getMessage() to extract messages.InputInterface classes. Enable debug mode for detailed exceptions.Custom Modifiers:
InputModifiableInterface to modify input data post-deserialization:
public function modify(): void {
$this->title = strtoupper($this->title);
}
Custom Validators:
AbstractInputValidator for reusable validation logic:
class CustomValidator extends AbstractInputValidator {
public function validate(): array {
return ['field' => new TranslatableMessage('Custom error')];
}
}
Type Casting:
InputInterface setters.Configuration:
InputManager services.How can I help you explore Laravel packages today?