DataTransformerInterface.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 when the form field is initialized with its default data, on
  23. * two occasions for two types of transformers:
  24. *
  25. * 1. Model transformers which normalize the model data.
  26. * This is mainly useful when the same form type (the same configuration)
  27. * has to handle different kind of underlying data, e.g The DateType can
  28. * deal with strings or \DateTime objects as input.
  29. *
  30. * 2. View transformers which adapt the normalized data to the view format.
  31. * a/ When the form is simple, the value returned by convention is used
  32. * directly in the view and thus can only be a string or an array. In
  33. * this case the data class should be null.
  34. *
  35. * b/ When the form is compound the returned value should be an array or
  36. * an object to be mapped to the children. Each property of the compound
  37. * data will be used as model data by each child and will be transformed
  38. * too. In this case data class should be the class of the object, or null
  39. * when it is an array.
  40. *
  41. * All transformers are called in a configured order from model data to view value.
  42. * At the end of this chain the view data will be validated against the data class
  43. * setting.
  44. *
  45. * This method must be able to deal with empty values. Usually this will
  46. * be NULL, but depending on your implementation other empty values are
  47. * possible as well (such as empty strings). The reasoning behind this is
  48. * that data transformers must be chainable. If the transform() method
  49. * of the first data transformer outputs NULL, the second must be able to
  50. * process that value.
  51. *
  52. * @param mixed $value The value in the original representation
  53. *
  54. * @return mixed The value in the transformed representation
  55. *
  56. * @throws TransformationFailedException when the transformation fails
  57. */
  58. public function transform($value);
  59. /**
  60. * Transforms a value from the transformed representation to its original
  61. * representation.
  62. *
  63. * This method is called when {@link Form::submit()} is called to transform the requests tainted data
  64. * into an acceptable format.
  65. *
  66. * The same transformers are called in the reverse order so the responsibility is to
  67. * return one of the types that would be expected as input of transform().
  68. *
  69. * This method must be able to deal with empty values. Usually this will
  70. * be an empty string, but depending on your implementation other empty
  71. * values are possible as well (such as NULL). The reasoning behind
  72. * this is that value transformers must be chainable. If the
  73. * reverseTransform() method of the first value transformer outputs an
  74. * empty string, the second value transformer must be able to process that
  75. * value.
  76. *
  77. * By convention, reverseTransform() should return NULL if an empty string
  78. * is passed.
  79. *
  80. * @param mixed $value The value in the transformed representation
  81. *
  82. * @return mixed The value in the original representation
  83. *
  84. * @throws TransformationFailedException when the transformation fails
  85. */
  86. public function reverseTransform($value);
  87. }