From 0dc7555ed1084c413bd686305d0a69633ce08868 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 18 Aug 2025 12:30:30 +0200 Subject: [PATCH] Use the new mapper component --- composer.json | 1 + composer.lock | 75 +++++++++++++++++++++++++- src/Controller/Api/ApiController.php | 4 +- src/Dto/Condition/ConditionNotNull.php | 14 +++++ src/Dto/SnipPostRequest.php | 19 +++---- 5 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 src/Dto/Condition/ConditionNotNull.php diff --git a/composer.json b/composer.json index 4af37b4..afed29a 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "symfony/form": "*", "symfony/framework-bundle": "*", "symfony/monolog-bundle": "^3.0", + "symfony/object-mapper": "7.3.*", "symfony/property-access": "*", "symfony/property-info": "*", "symfony/runtime": "*", diff --git a/composer.lock b/composer.lock index f85644f..6dec8eb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b4276957397352833f33cac4f3316f34", + "content-hash": "405d2a2709cb499d92b64659d2f59b2e", "packages": [ { "name": "dflydev/dot-access-data", @@ -4018,6 +4018,79 @@ ], "time": "2023-11-06T17:08:13+00:00" }, + { + "name": "symfony/object-mapper", + "version": "v7.3.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/object-mapper.git", + "reference": "782ee28ec5b1d3f6dbcf93a6b60288df68d774eb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/object-mapper/zipball/782ee28ec5b1d3f6dbcf93a6b60288df68d774eb", + "reference": "782ee28ec5b1d3f6dbcf93a6b60288df68d774eb", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "psr/container": "^2.0" + }, + "conflict": { + "symfony/property-access": "<7.2" + }, + "require-dev": { + "symfony/property-access": "^7.2", + "symfony/var-exporter": "^7.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\ObjectMapper\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides a way to map an object to another object", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/object-mapper/tree/v7.3.2" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-07-25T06:21:40+00:00" + }, { "name": "symfony/options-resolver", "version": "v7.3.0", diff --git a/src/Controller/Api/ApiController.php b/src/Controller/Api/ApiController.php index 3be9573..8594841 100644 --- a/src/Controller/Api/ApiController.php +++ b/src/Controller/Api/ApiController.php @@ -10,6 +10,7 @@ use App\Security\Voter\SnipVoter; use App\Service\SnipContent\SnipContentService; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; +use Symfony\Component\ObjectMapper\ObjectMapperInterface; use Symfony\Component\Routing\Attribute\Route; class ApiController extends AbstractApiController @@ -49,6 +50,7 @@ class ApiController extends AbstractApiController #[MapRequestPayload] SnipPostRequest $request, SnipContentService $cs, SnipRepository $repo, + ObjectMapperInterface $mapper, ): Response { $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); @@ -57,7 +59,7 @@ class ApiController extends AbstractApiController return $this->errorResponse('Snip is not the latest version'); } - $request->pushToSnip($snip); + $mapper->map($request, $snip); $repo->save($snip); if ($request->content !== null) { $cs->update($snip, $request->content, $request->contentName); diff --git a/src/Dto/Condition/ConditionNotNull.php b/src/Dto/Condition/ConditionNotNull.php new file mode 100644 index 0000000..9ada7c9 --- /dev/null +++ b/src/Dto/Condition/ConditionNotNull.php @@ -0,0 +1,14 @@ +name !== null) { - $snip->name = $this->name; - } - if ($this->public !== null) { - $snip->public = $this->public; - } - if ($this->visible !== null) { - $snip->visible = $this->visible; - } - } }