/Base.php

https://github.com/khallmark/LBHToolkit-Library · PHP · 235 lines · 102 code · 40 blank · 93 comment · 9 complexity · 89a712718607fa3affeb4ebb285ceab6 MD5 · raw file

  1. <?php
  2. /**
  3. * Base.php
  4. * LBHToolkit_Base
  5. *
  6. * This file handles the most basic setup for many LBHToolkit Classes.
  7. *
  8. * LICENSE
  9. *
  10. * This file is subject to the New BSD License that is bundled with this package.
  11. * It is available in the LICENSE file.
  12. *
  13. * It is also available online at http://www.littleblackhat.com/lbhtoolkit
  14. *
  15. * @author Kevin Hallmark <khallmark@avectra.com>
  16. * @since 2011-10-19
  17. * @package LBHToolkit
  18. * @subpackage LBHToolkit
  19. * @copyright Little Black Hat, 2011
  20. * @license http://www.littleblackhat.com/lbhtoolkit New BSD License
  21. */
  22. abstract class LBHToolkit_Base implements Serializable
  23. {
  24. /**
  25. * Parameters used by this object
  26. *
  27. * @var array
  28. */
  29. protected $_params = array();
  30. /**
  31. * Constructor
  32. *
  33. * @param string $object
  34. * @param string $params
  35. * @author Kevin Hallmark
  36. */
  37. public function __construct($params = NULL)
  38. {
  39. if (method_exists($this, 'setDefaultParams'))
  40. {
  41. $this->setDefaultParams();
  42. }
  43. if ($params !== NULL)
  44. {
  45. $this->setParams($params, TRUE);
  46. }
  47. if (method_exists($this, 'validateParams'))
  48. {
  49. $this->validateParams($params);
  50. }
  51. }
  52. /**
  53. * Get a value from the params array
  54. *
  55. * @param string $key
  56. * @return void
  57. * @author Kevin Hallmark
  58. */
  59. public function __get($key)
  60. {
  61. return $this->getParam($key);
  62. }
  63. public function getParam($key)
  64. {
  65. if (array_key_exists($key, $this->_params))
  66. {
  67. return $this->_params[$key];
  68. }
  69. return NULL;
  70. return $this->_params[$key];
  71. }
  72. public function setParam($key, $value)
  73. {
  74. $validate_method = 'validate' . ucfirst($key);
  75. if (method_exists($this, $validate_method))
  76. {
  77. $valid = $this->$validate_method($value);
  78. if ($valid !== TRUE)
  79. {
  80. $message = sprintf('Validation failed for %s with reason %s.', $key, $valid);
  81. throw new LBHToolkit_Exception($message);
  82. }
  83. }
  84. $this->_params[$key] = $value;
  85. }
  86. /**
  87. * Set to params
  88. *
  89. * @param string $key
  90. * @param string $value
  91. * @return void
  92. * @author Kevin Hallmark
  93. */
  94. public function __set($key, $value)
  95. {
  96. $this->setParam($key, $value);
  97. }
  98. /**
  99. * PHP Magic Method::__isset()
  100. *
  101. * @param string $key
  102. * @return void
  103. * @author Kevin Hallmark
  104. */
  105. public function __isset($key)
  106. {
  107. return isset($this->_params[$key]);
  108. }
  109. /**
  110. * Get the parameters in mass
  111. *
  112. * @return void
  113. * @author Kevin Hallmark
  114. */
  115. public function getParams()
  116. {
  117. return $this->_params;
  118. }
  119. /**
  120. * Set the parameters in mass
  121. *
  122. * @param string $new_params
  123. * @return void
  124. * @author Kevin Hallmark
  125. */
  126. public function setParams($new_params, $merge = FALSE)
  127. {
  128. if (!is_array($new_params))
  129. {
  130. throw new LBHToolkit_Exception('Params must be an array.');
  131. }
  132. if ($merge)
  133. {
  134. $this->_params = array_merge($this->_params, $new_params);
  135. }
  136. else
  137. {
  138. $this->_params = $new_params;
  139. }
  140. }
  141. /**
  142. * Serialize
  143. *
  144. * @return void
  145. * @author Kevin Hallmark
  146. */
  147. public function serialize()
  148. {
  149. $params = $this->toArray();
  150. return serialize($params);
  151. }
  152. public function toArray()
  153. {
  154. $params = $this->_params;
  155. if($custom_fields = $this->customSerializeFields())
  156. {
  157. $params = array_merge($params, $custom_fields);
  158. }
  159. return $params;
  160. }
  161. /**
  162. * Custom Unserialize
  163. *
  164. * @param string $data
  165. * @return void
  166. * @author Kevin Hallmark
  167. */
  168. public function unserialize($data)
  169. {
  170. $data = unserialize($data);
  171. $data = $this->customUnserializeFields($data);
  172. $this->setParams($data);
  173. }
  174. /**
  175. * Lets you return custom fields to serialize
  176. *
  177. * @return void
  178. * @author Kevin Hallmark
  179. */
  180. public function customSerializeFields()
  181. {
  182. return array();
  183. }
  184. /**
  185. * Lets you add custom field processing on unserialize
  186. *
  187. * @param string $params
  188. * @return array Modified Parameters array
  189. * @author Kevin Hallmark
  190. */
  191. public function customUnserializeFields($params)
  192. {
  193. return $params;
  194. }
  195. }