Successful.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Braintree\Result;
  3. use Braintree\Instance;
  4. use Braintree\Util;
  5. /**
  6. * Braintree Successful Result
  7. *
  8. * A Successful Result will be returned from gateway methods when
  9. * validations pass. It will provide access to the created resource.
  10. *
  11. * For example, when creating a customer, Successful will
  12. * respond to <b>customer</b> like so:
  13. *
  14. * <code>
  15. * $result = Customer::create(array('first_name' => "John"));
  16. * if ($result->success) {
  17. * // Successful
  18. * echo "Created customer {$result->customer->id}";
  19. * } else {
  20. * // Error
  21. * }
  22. * </code>
  23. *
  24. *
  25. * @package Braintree
  26. * @subpackage Result
  27. */
  28. class Successful extends Instance
  29. {
  30. /**
  31. *
  32. * @var boolean always true
  33. */
  34. public $success = true;
  35. /**
  36. *
  37. * @var string stores the internal name of the object providing access to
  38. */
  39. private $_returnObjectNames;
  40. /**
  41. * @ignore
  42. * @param array|null $objsToReturn
  43. * @param array|null $propertyNames
  44. */
  45. public function __construct($objsToReturn = [], $propertyNames = [])
  46. {
  47. // Sanitize arguments (preserves backwards compatibility)
  48. if (!is_array($objsToReturn)) { $objsToReturn = [$objsToReturn]; }
  49. if (!is_array($propertyNames)) { $propertyNames = [$propertyNames]; }
  50. $objects = $this->_mapPropertyNamesToObjsToReturn($propertyNames, $objsToReturn);
  51. $this->_attributes = [];
  52. $this->_returnObjectNames = [];
  53. foreach ($objects as $propertyName => $objToReturn) {
  54. // save the name for indirect access
  55. array_push($this->_returnObjectNames, $propertyName);
  56. // create the property!
  57. $this->$propertyName = $objToReturn;
  58. }
  59. }
  60. /**
  61. *
  62. * @ignore
  63. * @return string string representation of the object's structure
  64. */
  65. public function __toString()
  66. {
  67. $objects = [];
  68. foreach ($this->_returnObjectNames as $returnObjectName) {
  69. array_push($objects, $returnObjectName);
  70. }
  71. return __CLASS__ . '[' . implode(', ', $objects) . ']';
  72. }
  73. private function _mapPropertyNamesToObjsToReturn($propertyNames, $objsToReturn) {
  74. if(count($objsToReturn) != count($propertyNames)) {
  75. $propertyNames = [];
  76. foreach ($objsToReturn as $obj) {
  77. array_push($propertyNames, Util::cleanClassName(get_class($obj)));
  78. }
  79. }
  80. return array_combine($propertyNames, $objsToReturn);
  81. }
  82. }
  83. class_alias('Braintree\Result\Successful', 'Braintree_Result_Successful');