Home > Back-end >  How to sanitize output with custom filter (API Platform)
How to sanitize output with custom filter (API Platform)

Time:10-05

I'm trying to add a distance filter on my entity using api-platform (2.6.5). Everything works fine, except for the response body :

"hydra:member": [
    {
        "0": {
            "@id": "/api/places/1",
            "@type": "Place",
            "id": 1,
            "name": "test",
            "description": "test",
            "slug": "test",
            "address": "test",
            "country": "Suisse",
            "latitude": "50.6300000",
            "longitude": "3.0620000",
            "tableCount": 10,
            "tablePlace": 6,
            "tableCapacity": 60
        },
        "distance": "8.887448516728334"
    },
    {
        "0": {
            "@id": "/api/places/8",
            ...
        },
        "distance": "10.273770606986691"
    },
    {...}

The calculated distance is not considered like an entity property and api platform return them separately (This is a problem for the frontend).

Here is my entity :

/**
* @ORM\Entity(repositoryClass=PlaceRepository::class)
 * @ApiResource(
 *     normalizationContext={"groups"={"place", "place:read"}, "swagger_definition_name": "Read"},
 *     denormalizationContext={"groups"={"place", "place:write"}, "swagger_definition_name": "Write"},
 * )
 * @ApiFilter(SearchFilter::class, properties={"name": "partial", "description": "partial", "zip":"start", "city": "exact"})
 * @ApiFilter(NearbyFilter::class, properties={"location"})
 * @Entity @HasLifecycleCallbacks
 */
class Place
{
....
/**
 * @ORM\Column(type="decimal", precision=10, scale=7, nullable=true)
 * @Groups({"place"})
 */
private $latitude;

/**
 * @ORM\Column(type="decimal", precision=10, scale=7, nullable=true)
 * @Groups({"place"})
 */
private $longitude;
/**
 * @var float The distance calculated
 * @Groups({"place:read"})
 */
private $distance;

And here is my filter :

final class NearbyFilter extends AbstractContextAwareFilter
{
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
        
        if ($property !== 'location') {
            return;
        }
        
        [$lat, $lng, $radius] = explode(',', $value);

        // add distance in DQL based on filter location point
        $queryBuilder
            ->addSelect('
                (6371.0 * acos (
                    cos ( radians(:latitude) )
                    * cos( radians(o.latitude) )
                    * cos( radians(o.longitude) - radians(:longitude) )
                      sin ( radians(:latitude) )
                    * sin( radians(o.latitude) )
                )) AS distance')
            ->having('distance <= :radius')
            ->setParameter('radius', $radius)
            ->setParameter('latitude', $lat)
            ->setParameter('longitude', $lng)
            ->orderBy('distance')
        ;

    }

As you can see, i tried to add unmapped property 'distance' but that have no effect :/ I also tried to use Dto and Denormalizer to fix this without success (But I'm new with it).

Can you have an idea to fix my problem ?

Thanks !

CodePudding user response:

Finally find a solution with DataProvider :

namespace App\DataProvider;

use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Place;

class PlaceDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
    private $collectionDataProvider;
    public function __construct(CollectionDataProviderInterface $collectionDataProvider)
    {
        $this->collectionDataProvider = $collectionDataProvider;
    }

    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
    {
        $places = $this->collectionDataProvider->getCollection($resourceClass, $operationName, $context);

        foreach ($places as &$place) {
            if(!$place instanceof Place && is_array($place)) {
                $sanitizedPlace = $place[0];

                if( !is_null( $place['distance'] ) ){
                    $sanitizedPlace = $this->setDistance($sanitizedPlace, $place['distance']);
                }

                $place = $sanitizedPlace;
            }
        }
        
        return $places;
    }

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return $resourceClass === Place::class;
    }

    public function setDistance(Place $place, $distance): Place
    {
        $place->setDistance($distance);
        
        return $place;
    }
}

Thanks

  • Related