FixedDataTransformer.php 825 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Symfony\Tests\Component\Form\Fixtures;
  3. use Symfony\Component\Form\DataTransformerInterface;
  4. class FixedDataTransformer implements DataTransformerInterface
  5. {
  6. private $mapping;
  7. public function __construct(array $mapping)
  8. {
  9. $this->mapping = $mapping;
  10. }
  11. public function transform($value)
  12. {
  13. if (!array_key_exists($value, $this->mapping)) {
  14. throw new \RuntimeException(sprintf('No mapping for value "%s"', $value));
  15. }
  16. return $this->mapping[$value];
  17. }
  18. public function reverseTransform($value)
  19. {
  20. $result = array_search($value, $this->mapping, true);
  21. if ($result === false) {
  22. throw new \RuntimeException(sprintf('No reverse mapping for value "%s"', $value));
  23. }
  24. return $result;
  25. }
  26. }