ValidationException.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Validation;
  7. use Magento\Framework\Exception\AggregateExceptionInterface;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Phrase;
  10. /**
  11. * Validation exception with possibility to set several error messages
  12. *
  13. * ValidationException exists to be compatible with the Web-API (SOAP and REST) implementation which currently
  14. * uses Magento\Framework\Exception\AggregateExceptionInterface returned as a result of ServiceContracts call
  15. * to support Multi-Error response.
  16. *
  17. * @api
  18. * @since 101.0.7
  19. */
  20. class ValidationException extends LocalizedException implements AggregateExceptionInterface
  21. {
  22. /**
  23. * @var ValidationResult|null
  24. */
  25. private $validationResult;
  26. /**
  27. * @param Phrase $phrase
  28. * @param \Exception $cause
  29. * @param int $code
  30. * @param ValidationResult|null $validationResult
  31. */
  32. public function __construct(
  33. Phrase $phrase,
  34. \Exception $cause = null,
  35. $code = 0,
  36. ValidationResult $validationResult = null
  37. ) {
  38. parent::__construct($phrase, $cause, $code);
  39. $this->validationResult = $validationResult;
  40. }
  41. /**
  42. * @inheritdoc
  43. * @since 101.0.7
  44. */
  45. public function getErrors(): array
  46. {
  47. $localizedErrors = [];
  48. if (null !== $this->validationResult) {
  49. foreach ($this->validationResult->getErrors() as $error) {
  50. $localizedErrors[] = new LocalizedException($error);
  51. }
  52. }
  53. return $localizedErrors;
  54. }
  55. }