NoSuchEntityException.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * No such entity service exception
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Exception;
  9. use Magento\Framework\Phrase;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class NoSuchEntityException extends LocalizedException
  15. {
  16. /**
  17. * @deprecated
  18. */
  19. const MESSAGE_SINGLE_FIELD = 'No such entity with %fieldName = %fieldValue';
  20. /**
  21. * @deprecated
  22. */
  23. const MESSAGE_DOUBLE_FIELDS = 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value';
  24. /**
  25. * @param \Magento\Framework\Phrase $phrase
  26. * @param \Exception $cause
  27. * @param int $code
  28. */
  29. public function __construct(Phrase $phrase = null, \Exception $cause = null, $code = 0)
  30. {
  31. if ($phrase === null) {
  32. $phrase = new Phrase('No such entity.');
  33. }
  34. parent::__construct($phrase, $cause, $code);
  35. }
  36. /**
  37. * Helper function for creating an exception when a single field is responsible for finding an entity.
  38. *
  39. * @param string $fieldName
  40. * @param string|int $fieldValue
  41. * @return \Magento\Framework\Exception\NoSuchEntityException
  42. */
  43. public static function singleField($fieldName, $fieldValue)
  44. {
  45. return new self(
  46. new Phrase(
  47. 'No such entity with %fieldName = %fieldValue',
  48. [
  49. 'fieldName' => $fieldName,
  50. 'fieldValue' => $fieldValue
  51. ]
  52. )
  53. );
  54. }
  55. /**
  56. * Helper function for creating an exception when two fields are responsible for finding an entity.
  57. *
  58. * @param string $fieldName
  59. * @param string|int $fieldValue
  60. * @param string $secondFieldName
  61. * @param string|int $secondFieldValue
  62. * @return \Magento\Framework\Exception\NoSuchEntityException
  63. */
  64. public static function doubleField($fieldName, $fieldValue, $secondFieldName, $secondFieldValue)
  65. {
  66. return new self(
  67. new Phrase(
  68. 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
  69. [
  70. 'fieldName' => $fieldName,
  71. 'fieldValue' => $fieldValue,
  72. 'field2Name' => $secondFieldName,
  73. 'field2Value' => $secondFieldValue,
  74. ]
  75. )
  76. );
  77. }
  78. }