Model.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. class ChargeBee_Model
  3. {
  4. protected $_values;
  5. protected $_subTypes;
  6. protected $_dependantTypes;
  7. protected $_data = array();
  8. protected $allowed = array();
  9. function __construct($values, $subTypes=array(), $dependantTypes=array())
  10. {
  11. $this->_values = $values;
  12. $this->_subTypes = $subTypes;
  13. $this->_dependantTypes=$dependantTypes;
  14. $this->_load();
  15. }
  16. public function param($k)
  17. {
  18. if(array_key_exists($k, $this->_values))
  19. {
  20. return $this->_values[$k];
  21. }
  22. else
  23. {
  24. throw new Exception("Unknown param $k in " . get_class($this));
  25. }
  26. }
  27. public function __set($k, $v)
  28. {
  29. $this->_data[$k] = $v;
  30. }
  31. public function __isset($k)
  32. {
  33. return isset($this->_data[$k]);
  34. }
  35. public function __unset($k)
  36. {
  37. unset($this->data[$k]);
  38. }
  39. public function __get($k)
  40. {
  41. if (isset($this->_data[$k]))
  42. {
  43. return $this->_data[$k];
  44. }
  45. else if(in_array($k, $this->allowed))
  46. {
  47. return null;
  48. }
  49. else if(substr($k, 0, 2) == "cf") //All the custom fields start with prefix cf.
  50. {
  51. return null;
  52. }
  53. else
  54. {
  55. throw new Exception("Unknown property $k in " . get_class($this));
  56. }
  57. }
  58. private function __getDependant($k)
  59. {
  60. if (isset($this->_dependantTypes[$k]))
  61. {
  62. return $this->_dependantTypes[$k];
  63. }
  64. else if(in_array($k, $this->allowed))
  65. {
  66. return null;
  67. }
  68. else
  69. {
  70. throw new Exception("Unknown property $k in " . get_class($this));
  71. }
  72. }
  73. private function isHash($array)
  74. {
  75. if (!is_array($array))
  76. return false;
  77. foreach (array_keys($array) as $k) {
  78. if (is_numeric($k))
  79. return false;
  80. }
  81. return true;
  82. }
  83. private function _load()
  84. {
  85. foreach($this->_values as $k => $v)
  86. {
  87. $setVal = null;
  88. if($this->isHash($v) && array_key_exists($k, $this->_dependantTypes))
  89. {
  90. continue;
  91. }
  92. if($this->isHash($v) && array_key_exists($k, $this->_subTypes))
  93. {
  94. $setVal = new $this->_subTypes[$k]($v);
  95. }
  96. else if(is_array($v) && array_key_exists($k, $this->_subTypes))
  97. {
  98. $setVal = array();
  99. foreach($v as $stV)
  100. {
  101. array_push($setVal, new $this->_subTypes[$k]($stV));
  102. }
  103. }
  104. if(is_null($setVal))
  105. {
  106. $setVal = $v;
  107. }
  108. $this->_data[ChargeBee_Util::toCamelCaseFromUnderscore($k)] = $setVal;
  109. }
  110. }
  111. public function _initDependant($obj, $type, $subTypes = array())
  112. {
  113. if(!array_key_exists($type, $obj))
  114. {
  115. return $this;
  116. }
  117. $class=$this->__getDependant($type);
  118. if($this->isHash($obj) && !is_null($class))
  119. {
  120. $dependantObj = new $class($obj[$type], $subTypes);
  121. $this->_data[ChargeBee_Util::toCamelCaseFromUnderscore($type)] = $dependantObj;
  122. }
  123. return $this;
  124. }
  125. public function _initDependantList($obj, $type, $subTypes = array())
  126. {
  127. if(!array_key_exists($type, $obj))
  128. {
  129. return $this;
  130. }
  131. if(!is_array($obj[$type])){
  132. return $this;
  133. }
  134. $class=$this->__getDependant($type);
  135. if(!is_null($class)){
  136. $setVal = array();
  137. foreach($obj[$type] as $dt)
  138. {
  139. array_push($setVal, new $class($dt, $subTypes));
  140. }
  141. $this->_data[ChargeBee_Util::toCamelCaseFromUnderscore($type)] = $setVal;
  142. }
  143. return $this;
  144. }
  145. }
  146. ?>