Instance.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Braintree;
  3. /**
  4. * Braintree Class Instance template
  5. *
  6. * @abstract
  7. */
  8. abstract class Instance
  9. {
  10. protected $_attributes = [];
  11. /**
  12. *
  13. * @param array $attributes
  14. */
  15. public function __construct($attributes)
  16. {
  17. if (!empty($attributes)) {
  18. $this->_initializeFromArray($attributes);
  19. }
  20. }
  21. /**
  22. * returns private/nonexistent instance properties
  23. * @access public
  24. * @param string $name property name
  25. * @return mixed contents of instance properties
  26. */
  27. public function __get($name)
  28. {
  29. if (array_key_exists($name, $this->_attributes)) {
  30. return $this->_attributes[$name];
  31. } else {
  32. trigger_error('Undefined property on ' . get_class($this) . ': ' . $name, E_USER_NOTICE);
  33. return null;
  34. }
  35. }
  36. /**
  37. * used by isset() and empty()
  38. * @access public
  39. * @param string $name property name
  40. * @return boolean
  41. */
  42. public function __isset($name)
  43. {
  44. return array_key_exists($name, $this->_attributes);
  45. }
  46. /**
  47. * create a printable representation of the object as:
  48. * ClassName[property=value, property=value]
  49. * @return string
  50. */
  51. public function __toString()
  52. {
  53. $objOutput = Util::implodeAssociativeArray($this->_attributes);
  54. return get_class($this) .'[' . $objOutput . ']';
  55. }
  56. /**
  57. * initializes instance properties from the keys/values of an array
  58. * @ignore
  59. * @access protected
  60. * @param <type> $aAttribs array of properties to set - single level
  61. * @return void
  62. */
  63. private function _initializeFromArray($attributes)
  64. {
  65. $this->_attributes = $attributes;
  66. }
  67. }
  68. class_alias('Braintree\Instance', 'Braintree_Instance');