Skip to content
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

EntityCaster: Enhanced Data Casting into Entities for spiral/filters component #87

Merged
merged 3 commits into from
Jan 8, 2024

Conversation

butschster
Copy link
Member

The Spiral\Cycle\Filter\EntityCaster extends the capabilities of spiral/filters, allowing developers to easily convert incoming data into entity objects. This is particularly useful when dealing with various data sources such as HTTP requests, gRPC requests, console commands, and more.

To get started and learn more about input casting in Spiral, check out our comprehensive documentation: Spiral Filters input casting.

Example

Consider a scenario where you need to transform POST data into an entity object. This is particularly useful when dealing with forms or API endpoints. Here's how you can achieve it:

namespace Spiral\App\Controller\Filter;

use Spiral\App\Entities\Role;
use Spiral\Filters\Attribute\Input\Post;
use Spiral\Filters\Model\Filter;

final class RoleFilter extends Filter
{
    #[Post]
    public Role $role;
}

And the corresponding controller

namespace Spiral\App\Controller;

use Spiral\App\Controller\Filter\RoleFilter;
use Spiral\Router\Annotation\Route;

final class RoleController
{
    #[Route(route: "/role", methods: ["POST"])]
    public function create(RoleFilter $filter): array
    {
        // Your logic here
    }
}

In this example, when a POST request is made to /role with data like [role: 1], the EntityCaster will automatically fetch the role object with ID 1 from the database and convert it into a Role object.

Registering the caster

To use the caster, you need to register it in your application's bootloader. Here's how you can do it:

<?php

declare(strict_types=1);

namespace Spiral\App\Bootloader;

use Spiral\Bootloader\DomainBootloader;
use Spiral\Bootloader\Security\FiltersBootloader;
use Spiral\Cycle\Filter\EntityCaster;
use Spiral\Filters\Model\Mapper\CasterRegistryInterface;

final class AppBootloader extends DomainBootloader
{
    public function defineDependencies(): array
    {
        return [
            FiltersBootloader::class,
        ];
    }

    public function init(CasterRegistryInterface $casterRegistry, EntityCaster $caster): void
    {
        $casterRegistry->register($caster);
    }
}

fixes #75

@butschster butschster added the enhancement New feature or request label Jan 5, 2024
@butschster butschster added this to the v2.x milestone Jan 5, 2024
@butschster butschster self-assigned this Jan 5, 2024
Copy link

codecov bot commented Jan 5, 2024

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (09cac59) 80.50% compared to head (c077539) 80.49%.

Files Patch % Lines
src/Filter/EntityCaster.php 84.00% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                2.x      #87      +/-   ##
============================================
- Coverage     80.50%   80.49%   -0.02%     
- Complexity      401      412      +11     
============================================
  Files            46       47       +1     
  Lines          1216     1241      +25     
============================================
+ Hits            979      999      +20     
- Misses          237      242       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@butschster butschster merged commit 6673bae into 2.x Jan 8, 2024
7 checks passed
@butschster butschster deleted the feature/issue-75 branch January 8, 2024 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[filters] Feature request: cycle entities interception
2 participants