Model.php 3.2 KB

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