PHP code example of wundii / data-mapper-symfony-bundle

1. Go to this page and download the library: Download wundii/data-mapper-symfony-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wundii / data-mapper-symfony-bundle example snippets


return [
    // ...
    Wundii\DataMapper\SymfonyBundle\DataMapperBundle::class => ['all' => true],
];



declare(strict_types=1);

namespace App\Controller;

use App\Dto\TestClass;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Wundii\DataMapper\SymfonyBundle\DataMapper;

final class YourController extends AbstractController
{
    public function __construct(
        private readonly DataMapper $dataMapper,
    ) {
    }

    #[Route('/do-something/', name: 'app_do-something')]
    public function doSomething(Request $request): Response
    {
        // Automatic recognition of the format based on the content type of the request
        // returns an instance of TestClass or an Exception
        $testClass = $this->dataMapper->request($request, TestClass::class);

        // or you can use tryRequest to avoid exceptions, null will be returned instead
        $testClass = $this->dataMapper->tryRequest($request, TestClass::class);
        $this->dataMapper->getMapStatusEnum();
        $this->dataMapper->getErrorMessage();

        // Do something with $testClass

        return $this->json(...);
    }
}



declare(strict_types=1);

namespace App\Controller;

use App\Dto\TestClass;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Wundii\DataMapper\DataMapper;

final class YourController extends AbstractController
{
    public function __construct(
        private readonly DataMapper $dataMapper,
    ) {
    }

    #[Route('/do-something/', name: 'app_do-something')]
    public function doSomething(Request $request): Response
    {
        // you can use the native DataMapper methods directly
        $testClass = $this->dataMapper->json($request->getContent(), TestClass::class);

        // Do something with $testClass

        return $this->json(...);
    }
}