Skip to content

Issue #99: injectable input filters #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/book/v6/extended-features/injectable-input-filters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Injectable input filters

In the current version of 6.0, Dotkernel API has an Injectable Input Filter system into the constructors of our handlers.

When building APIs or backend applications in PHP, especially within frameworks that support dependency injection, input validation is a critical concern.
Many developers instinctively instantiate input filters or validators inside their handlers or controllers.
However, injecting input filters is a cleaner, more testable, and flexible approach.

The **previous** version that contained inline instantiation:

```php
public function handle(ServerRequestInterface $request): ResponseInterface
{
$inputFilter = (new CreateAdminInputFilter())->setData((array) $request->getParsedBody());
if (! $inputFilter->isValid()) {
throw (new BadRequestException())->setMessages($inputFilter->getMessages());
}

$admin = $this->adminService->createAdmin($inputFilter->getValues());

return $this->createdResponse($request, $admin);
}
```

While simple, this ties your handler directly to a concrete class. It’s harder to reuse logic across contexts and mock or replace the filter during testing.

Our **current** approach uses constructor injection:

```php
class PostAdminResourceHandler extends AbstractHandler
{
#[Inject(
AdminServiceInterface::class,
CreateAdminInputFilter::class,
)]
public function __construct(
protected AdminServiceInterface $adminService,
protected CreateAdminInputFilter $inputFilter,
) {
}

/**
* @throws BadRequestException
* @throws ConflictException
* @throws NotFoundException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->inputFilter->setData((array) $request->getParsedBody());
if (! $this->inputFilter->isValid()) {
throw (new BadRequestException())->setMessages($this->inputFilter->getMessages());
}

$admin = $this->adminService->createAdmin((array) $this->inputFilter->getValues());

return $this->createdResponse($request, $admin);
}
}
```

This new approach makes it trivial to mock the filters during tests:

```php
$mockFilter = $this->createMock(CreateAdminInputFilter::class);
$mockFilter->method('setData')->willReturnSelf();
$mockFilter->method('isValid')->willReturn(true);

$handler = new PostAdminResourceHandler($adminService, $mockFilter);
$response = $handler->handle($request);
```

You're no longer tied to the real filter logic in your handler tests.
1 change: 0 additions & 1 deletion docs/book/v6/introduction/file-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ This folder contains all application-related config files:
* `development.config.php.dist` - Activates debug mode; gets symlinked as `development.config.php` when enabling development mode
* `migrations.php` - Configuration for database migration, like migration file location and table to save the migration log
* `pipeline.php` - Contains a list of middlewares, in the order of their execution
* `twig-cs-fixer.php` - Configuration file for Twig code style checker/fixer

### `config/autoload` folder

Expand Down
1 change: 0 additions & 1 deletion docs/book/v6/introduction/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* `mezzio/mezzio-fastroute` - FastRoute integration for Mezzio
* `mezzio/mezzio-hal` - Hypertext Application Language implementation for PHP and PSR-15
* `mezzio/mezzio-problem-details` - Problem Details for PSR-15 HTTP APIs addressing the RFC 7807 standard
* `mezzio/mezzio-twigrenderer` - Twig integration for Mezzio
* `ramsey/uuid-doctrine` - Use ramsey/uuid as a Doctrine field type
* `roave/psr-container-doctrine` - Doctrine Factories for PSR-11 Containers
* `symfony/filesystem` - Provides basic utilities for the filesystem
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ nav:
- "New Handler Structure": v6/extended-features/handler-structure.md
- "Route Grouping": v6/extended-features/route-grouping.md
- "Problem Details": v6/extended-features/problem-details.md
- "Injectable Input Filters": v6/extended-features/injectable-input-filters.md
- Commands:
- "Create admin account": v6/commands/create-admin-account.md
- "Generate database migrations": v6/commands/generate-database-migrations.md
Expand Down