SubjectReader.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Gateway\Helper;
  7. use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
  8. use Magento\Framework\DataObject;
  9. /**
  10. * This class encapsulates implicit interfaces (array structures) used in payments implementation.
  11. * This class was introduced for backward compatibility with legacy implementation.
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class SubjectReader
  17. {
  18. /**
  19. * Reads payment from subject
  20. *
  21. * @param array $subject
  22. * @return PaymentDataObjectInterface
  23. */
  24. public static function readPayment(array $subject)
  25. {
  26. if (!isset($subject['payment'])
  27. || !$subject['payment'] instanceof PaymentDataObjectInterface
  28. ) {
  29. throw new \InvalidArgumentException('Payment data object should be provided');
  30. }
  31. return $subject['payment'];
  32. }
  33. /**
  34. * Reads amount from subject
  35. *
  36. * @param array $subject
  37. * @return mixed
  38. */
  39. public static function readAmount(array $subject)
  40. {
  41. if (!isset($subject['amount']) || !is_numeric($subject['amount'])) {
  42. throw new \InvalidArgumentException('Amount should be provided');
  43. }
  44. return $subject['amount'];
  45. }
  46. /**
  47. * Reads field from subject
  48. *
  49. * @param array $subject
  50. * @return string
  51. */
  52. public static function readField(array $subject)
  53. {
  54. if (!isset($subject['field']) || !is_string($subject['field'])) {
  55. throw new \InvalidArgumentException('Field does not exist');
  56. }
  57. return $subject['field'];
  58. }
  59. /**
  60. * Reads response NVP from subject
  61. *
  62. * @param array $subject
  63. * @return array
  64. */
  65. public static function readResponse(array $subject)
  66. {
  67. if (!isset($subject['response']) || !is_array($subject['response'])) {
  68. throw new \InvalidArgumentException('Response does not exist');
  69. }
  70. return $subject['response'];
  71. }
  72. /**
  73. * Read state object from subject
  74. *
  75. * @param array $subject
  76. * @return DataObject
  77. */
  78. public static function readStateObject(array $subject)
  79. {
  80. if (!isset($subject['stateObject']) || !$subject['stateObject'] instanceof DataObject) {
  81. throw new \InvalidArgumentException('State object does not exist');
  82. }
  83. return $subject['stateObject'];
  84. }
  85. }