WIP: Implement feedback functionality

This commit is contained in:
Björn Fromme committed 2023-11-05 18:19:58 +01:00
1 parent 47f2aa17be
commit 908b904b67
15 files changed
+346 -75

No files matched your search

@@ -1,38 +0,0 @@
<?php
namespace App\Form\DataTransformer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class EntityToIdTransformer implements DataTransformerInterface
{
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly string $class)
{
}
public function transform($value)
{
if (null === $value) {
return null;
}
return $value->getId();
}
public function reverseTransform($value)
{
if (empty($value)) {
return null;
}
$entity = $this->entityManager->getRepository($this->class)->find($value);
if (null === $entity) {
throw new TransformationFailedException(sprintf('No %s with id %d found', $this->class, $value));
}
return $entity;
}
}
@@ -0,0 +1,54 @@
<?php
namespace App\Form\DataTransformer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class EntityToIdentifierTransformer implements DataTransformerInterface
{
private string $identifier;
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly string $class,
string $identifier = 'id'
) {
if (false === in_array($identifier, ['id', 'uuid'])) {
throw new \InvalidArgumentException(sprintf('Identifier must be one of "id" or "uuid" got "%s"', $identifier));
}
$this->identifier = $identifier;
}
public function transform($value)
{
if (null === $value) {
return null;
}
$getter = 'get'.ucfirst($this->identifier);
return call_user_func([$value, $getter]);
}
public function reverseTransform($value)
{
if (empty($value)) {
return null;
}
$entity = $this
->entityManager
->getRepository($this->class)
->findOneBy([$this->identifier => $value])
;
if (null === $entity) {
throw new TransformationFailedException(sprintf('No %s with %s %s found', $this->class, $this->identifier, $value));
}
return $entity;
}
}
@@ -0,0 +1,33 @@
<?php
namespace App\Form\DataTransformer;
use App\Entity\FeedbackSet;
use Symfony\Component\Form\DataTransformerInterface;
class FeedbackToRatingsTransformer implements DataTransformerInterface
{
public function __construct(private readonly FeedbackSet $feedbackSet)
{
}
public function transform(mixed $value)
{
// Not in use
return $value;
}
public function reverseTransform(mixed $value)
{
$feedbackRatings = [];
foreach ($this->feedbackSet->getRatings() as $index => $rating) {
$feedbackRatings[] = [
'rating' => $rating,
'mark' => $value['rating_'.$index],
];
}
return $feedbackRatings;
}
}