LegacyConstraintViolationBuilder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Validator\Violation;
  11. @trigger_error('The '.__NAMESPACE__.'\LegacyConstraintViolationBuilder class is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  12. use Symfony\Component\Validator\ExecutionContextInterface;
  13. /**
  14. * Backwards-compatible implementation of {@link ConstraintViolationBuilderInterface}.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal You should not instantiate or use this class. Code against
  19. * {@link ConstraintViolationBuilderInterface} instead.
  20. *
  21. * @deprecated since version 2.5.5, to be removed in 3.0.
  22. */
  23. class LegacyConstraintViolationBuilder implements ConstraintViolationBuilderInterface
  24. {
  25. /**
  26. * @var ExecutionContextInterface
  27. */
  28. private $context;
  29. /**
  30. * @var string
  31. */
  32. private $message;
  33. /**
  34. * @var array
  35. */
  36. private $parameters;
  37. /**
  38. * @var mixed
  39. */
  40. private $invalidValue;
  41. /**
  42. * @var string
  43. */
  44. private $propertyPath;
  45. /**
  46. * @var int|null
  47. */
  48. private $plural;
  49. /**
  50. * @var mixed
  51. */
  52. private $code;
  53. public function __construct(ExecutionContextInterface $context, $message, array $parameters)
  54. {
  55. $this->context = $context;
  56. $this->message = $message;
  57. $this->parameters = $parameters;
  58. $this->invalidValue = $context->getValue();
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function atPath($path)
  64. {
  65. $this->propertyPath = $path;
  66. return $this;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function setParameter($key, $value)
  72. {
  73. $this->parameters[$key] = $value;
  74. return $this;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function setParameters(array $parameters)
  80. {
  81. $this->parameters = $parameters;
  82. return $this;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function setTranslationDomain($translationDomain)
  88. {
  89. // can't be set in the old API
  90. return $this;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function setInvalidValue($invalidValue)
  96. {
  97. $this->invalidValue = $invalidValue;
  98. return $this;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function setPlural($number)
  104. {
  105. $this->plural = $number;
  106. return $this;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function setCode($code)
  112. {
  113. $this->code = $code;
  114. return $this;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function setCause($cause)
  120. {
  121. // do nothing - we can't save the cause through the old API
  122. return $this;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function addViolation()
  128. {
  129. if ($this->propertyPath) {
  130. $this->context->addViolationAt($this->propertyPath, $this->message, $this->parameters, $this->invalidValue, $this->plural, $this->code);
  131. return;
  132. }
  133. $this->context->addViolation($this->message, $this->parameters, $this->invalidValue, $this->plural, $this->code);
  134. }
  135. }