ClientTransformer.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace WebserviceBundle\Form\Transformer;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\Form\DataTransformerInterface;
  5. use Symfony\Component\Form\Exception\TransformationFailedException;
  6. class ClientTransformer implements DataTransformerInterface
  7. {
  8. private $em;
  9. public function __construct(EntityManagerInterface $em)
  10. {
  11. $this->em = $em;
  12. }
  13. /**
  14. * Transforms an object (issue) to a string (number).
  15. *
  16. * @param Int | null $client
  17. * @return string
  18. */
  19. public function transform($client)
  20. {
  21. if (null === $client) {
  22. return '';
  23. }
  24. return $client;
  25. }
  26. /**
  27. * Transforms a string (query pattern) to an client id (using the web service).
  28. *
  29. * @param string $client_query_param
  30. * @return Issue|null
  31. * @throws TransformationFailedException if object (issue) is not found.
  32. */
  33. public function reverseTransform($client_query_param)
  34. {
  35. // no issue number? It's optional, so that's ok
  36. if (!$issueNumber) {
  37. return;
  38. }
  39. $issue = $this->em
  40. ->getRepository(Issue::class)
  41. // query for the issue with this id
  42. ->find($issueNumber)
  43. ;
  44. if (null === $issue) {
  45. // causes a validation error
  46. // this message is not shown to the user
  47. // see the invalid_message option
  48. throw new TransformationFailedException(sprintf(
  49. 'An Client could not be found for the query "%s"!',
  50. $issueNumber
  51. ));
  52. }
  53. return $issue;
  54. }
  55. }