DataTransformerInterface.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\TransformationFailedException;
  12. /**
  13. * Transforms a value between different representations.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. interface DataTransformerInterface
  18. {
  19. /**
  20. * Transforms a value from the original representation to a transformed representation.
  21. *
  22. * This method is called on two occasions inside a form field:
  23. *
  24. * 1. When the form field is initialized with the data attached from the datasource (object or array).
  25. * 2. When data from a request is submitted using {@link Form::submit()} to transform the new input data
  26. * back into the renderable format. For example if you have a date field and submit '2009-10-10'
  27. * you might accept this value because its easily parsed, but the transformer still writes back
  28. * "2009/10/10" onto the form field (for further displaying or other purposes).
  29. *
  30. * This method must be able to deal with empty values. Usually this will
  31. * be NULL, but depending on your implementation other empty values are
  32. * possible as well (such as empty strings). The reasoning behind this is
  33. * that value transformers must be chainable. If the transform() method
  34. * of the first value transformer outputs NULL, the second value transformer
  35. * must be able to process that value.
  36. *
  37. * By convention, transform() should return an empty string if NULL is
  38. * passed.
  39. *
  40. * @param mixed $value The value in the original representation
  41. *
  42. * @return mixed The value in the transformed representation
  43. *
  44. * @throws TransformationFailedException when the transformation fails
  45. */
  46. public function transform($value);
  47. /**
  48. * Transforms a value from the transformed representation to its original
  49. * representation.
  50. *
  51. * This method is called when {@link Form::submit()} is called to transform the requests tainted data
  52. * into an acceptable format for your data processing/model layer.
  53. *
  54. * This method must be able to deal with empty values. Usually this will
  55. * be an empty string, but depending on your implementation other empty
  56. * values are possible as well (such as NULL). The reasoning behind
  57. * this is that value transformers must be chainable. If the
  58. * reverseTransform() method of the first value transformer outputs an
  59. * empty string, the second value transformer must be able to process that
  60. * value.
  61. *
  62. * By convention, reverseTransform() should return NULL if an empty string
  63. * is passed.
  64. *
  65. * @param mixed $value The value in the transformed representation
  66. *
  67. * @return mixed The value in the original representation
  68. *
  69. * @throws TransformationFailedException when the transformation fails
  70. */
  71. public function reverseTransform($value);
  72. }