InputException.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Exception;
  7. use Magento\Framework\Phrase;
  8. /**
  9. * Exception to be thrown when there is an issue with the Input to a function call.
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class InputException extends AbstractAggregateException
  15. {
  16. /**
  17. * @deprecated
  18. */
  19. const DEFAULT_MESSAGE = 'One or more input exceptions have occurred.';
  20. /**
  21. * @deprecated
  22. */
  23. const INVALID_FIELD_RANGE = 'The %fieldName value of "%value" must be between %minValue and %maxValue';
  24. /**
  25. * @deprecated
  26. */
  27. const INVALID_FIELD_MIN_VALUE = 'The %fieldName value of "%value" must be greater than or equal to %minValue.';
  28. /**
  29. * @deprecated
  30. */
  31. const INVALID_FIELD_MAX_VALUE = 'The %fieldName value of "%value" must be less than or equal to %maxValue.';
  32. /**
  33. * @deprecated
  34. */
  35. const INVALID_FIELD_VALUE = 'Invalid value of "%value" provided for the %fieldName field.';
  36. /**
  37. * @deprecated
  38. */
  39. const REQUIRED_FIELD = '"%fieldName" is required. Enter and try again.';
  40. /**
  41. * Initialize the input exception.
  42. *
  43. * @param \Magento\Framework\Phrase $phrase
  44. * @param \Exception $cause
  45. * @param int $code
  46. */
  47. public function __construct(Phrase $phrase = null, \Exception $cause = null, $code = 0)
  48. {
  49. if ($phrase === null) {
  50. $phrase = new Phrase('One or more input exceptions have occurred.');
  51. }
  52. parent::__construct($phrase, $cause, $code);
  53. }
  54. /**
  55. * Creates an InputException for when a specific field was provided with an invalid value.
  56. *
  57. * @param string $fieldName Name of the field which had an invalid value provided.
  58. * @param string $fieldValue The invalid value that was provided for the field.
  59. * @param \Exception $cause Cause of the InputException
  60. * @return \Magento\Framework\Exception\InputException
  61. */
  62. public static function invalidFieldValue($fieldName, $fieldValue, \Exception $cause = null)
  63. {
  64. return new self(
  65. new Phrase(
  66. 'Invalid value of "%value" provided for the %fieldName field.',
  67. ['fieldName' => $fieldName, 'value' => $fieldValue]
  68. ),
  69. $cause
  70. );
  71. }
  72. /**
  73. * Creates an InputException for a missing required field.
  74. *
  75. * @param string $fieldName Name of the missing required field.
  76. * @return \Magento\Framework\Exception\InputException
  77. */
  78. public static function requiredField($fieldName)
  79. {
  80. return new self(
  81. new Phrase('"%fieldName" is required. Enter and try again.', ['fieldName' => $fieldName])
  82. );
  83. }
  84. }