C_Photocrati_Settings_Manager_Base

Provides a base abstraction for a Settings Manager Class C_Settings_Manager_Base.

Defined (1)

The class is defined in the following location(s).

/non_pope/class.photocrati_settings_manager.php  
  1. abstract class C_Photocrati_Settings_Manager_Base implements ArrayAccess 
  2. static $option_name = 'pope_settings'; 
  3. protected $_options = array(); 
  4. protected $_defaults = array(); 
  5. protected $_option_handlers = array(); 
  6.  
  7. abstract function save(); 
  8. abstract function destroy(); 
  9. abstract function load(); 
  10.  
  11. protected function __construct() 
  12. $this->load(); 
  13.  
  14. /** 
  15. * Adds a class to handle dynamic options 
  16. * @param string $klass 
  17. * @param array $options 
  18. */ 
  19. function add_option_handler($klass, $options=array()) 
  20. if (!is_array($options)) $options = array($options); 
  21. foreach ($options as $option_name) { 
  22. $this->_option_handlers[$option_name] = $klass; 
  23.  
  24. /** 
  25. * Gets a handler used to provide a dynamic option 
  26. * @param string $option_name 
  27. * @return null|mixed 
  28. */ 
  29. protected function _get_option_handler($option_name, $method='get') 
  30. $retval = NULL; 
  31.  
  32. if (isset($this->_option_handlers[$option_name])) { 
  33. if (!is_object($this->_option_handlers[$option_name])) { 
  34. $klass = $this->_option_handlers[$option_name]; 
  35. $this->_option_handlers[$option_name] = new $klass; 
  36. $retval = $this->_option_handlers[$option_name]; 
  37. if (!method_exists($retval, $method)) $retval = NULL; 
  38. return $retval; 
  39.  
  40. /** 
  41. * Gets the value of a particular setting 
  42. * @param $key 
  43. * @param null $default 
  44. * @return null 
  45. */ 
  46. function get($key, $default=NULL) 
  47. $retval = $default; 
  48.  
  49. if (($handler = $this->_get_option_handler($key, 'get'))) { 
  50. $retval = $handler->get($key, $default); 
  51. else if (isset($this->_options[$key])) { 
  52. $retval = $this->_options[$key]; 
  53.  
  54. // In case a stdObject has been passed in as a value, we 
  55. // want to only return scalar values or arrays 
  56. if (is_object($retval)) $retval = (array) $retval; 
  57.  
  58. return $retval; 
  59.  
  60. /** 
  61. * Sets a setting to a particular value 
  62. * @param string $key 
  63. * @param mixed $value 
  64. * @return mixed 
  65. */ 
  66. function set($key, $value=NULL, $skip_handlers=FALSE) 
  67. if (is_object($value)) $value = (array) $value; 
  68.  
  69. if (is_array($key)) { 
  70. foreach ($key as $k=>$v) $this->set($k, $v); 
  71. elseif (!$skip_handlers && ($handler = $this->_get_option_handler($key, 'set'))) { 
  72. $handler->set($key, $value); 
  73. else $this->_options[$key] = $value; 
  74.  
  75. return $this; 
  76.  
  77. /** 
  78. * Deletes a setting 
  79. * @param string $key 
  80. */ 
  81. function delete($key) 
  82. if (($handler = $this->_get_option_handler($key, 'delete'))) { 
  83. $handler->delete($key); 
  84. else { 
  85. unset($this->_options[$key]); 
  86.  
  87. /** 
  88. * Determines if a setting exists or not 
  89. * @param $key 
  90. * @return bool 
  91. */ 
  92. function is_set($key) 
  93. return array_key_exists($key, $this->_options); 
  94.  
  95. /** 
  96. * Alias to is_set() 
  97. * @param $key 
  98. * @return bool 
  99. */ 
  100. function exists($key) 
  101. return $this->is_set($key); 
  102.  
  103. function does_not_exist($key) 
  104. return !$this->exists($key); 
  105.  
  106. function reset() 
  107. $this->_options = array(); 
  108. $this->_defaults = array(); 
  109.  
  110. /** 
  111. * This function does two things: 
  112. * a) If a value hasn't been set for the specified key, or it's been set to a previously set 
  113. * default value, then set this key to the value specified 
  114. * b) Sets a new default value for this key 
  115. */ 
  116. function set_default_value($key, $default) 
  117. if (!isset($this->_defaults[$key])) $this->_defaults[$key] = $default; 
  118. if (is_null($this->get($key, NULL)) OR $this->get($key) == $this->_defaults[$key]) { 
  119. $this->set($key, $default); 
  120. $this->_defaults[$key] = $default; 
  121. return $this->get($key); 
  122.  
  123. function offsetExists($key) 
  124. return $this->is_set($key); 
  125.  
  126. function offsetGet($key) 
  127. return $this->get($key); 
  128.  
  129. function offsetSet($key, $value) 
  130. return $this->set($key, $value); 
  131.  
  132. function offsetUnset($key) 
  133. return $this->delete($key); 
  134.  
  135. function __get($key) 
  136. return $this->get($key); 
  137.  
  138. function __set($key, $value) 
  139. return $this->set($key, $value); 
  140.  
  141. function __isset($key) 
  142. return $this->is_set($key); 
  143.  
  144. function __toString() 
  145. return json_encode($this->_options); 
  146.  
  147. function __toArray() 
  148. return $this->_options; 
  149.  
  150. function to_array() 
  151. return $this->__toArray(); 
  152.  
  153. function to_json() 
  154. return json_encode($this->_options); 
  155.  
  156. function from_json($json) 
  157. $this->_options = (array)json_decode($json);