false, 'default' => '', 'onLoadCallback' => null, 'onSaveCallback' => null, 'possibleValues' => array(), 'type' => 'string', //equivalent to self::TYPE_STRING 'value' => '', ); public static $defaults_for_types = array( 'array' => array( 'default' => 'a:0:{}', 'onLoadCallback' => 'unserialize', 'onSaveCallback' => 'serialize', ), 'boolean' => array( 'default' => 0, 'onSaveCallback' => 'intval', 'possibleValues' => array(0, 1), ), 'integer' => array( 'default' => 0, 'onSaveCallback' => 'intval', ), ); public static $create_option_callback = 'add_option'; public static $delete_option_callback = 'delete_option'; public static $get_option_callback = 'get_option'; public static $set_option_callback = 'update_option'; protected $id, $options, $prefix; private $is_loaded = false, $is_new = false; /** * Constructs the object * * @author oncletom * @since 2.0 * @version 1.0 * @param $id string * @param $options array [optional] */ public function __construct($id, array $options = array()) { $this->id = $id; $this->options = self::$defaults; $this->fromArray($options); } /** * Computes the option value * * @author oncletom * @since 2.0 * @version 1.0 * @return mixed */ public function __toString() { return $this->getValue(); } /** * Removes an option from database * * @author oncletom * * @return */ public function delete() { call_user_func(self::$delete_option_callback, $this->getId()); $this->fromArray(self::$defaults); } /** * Builds the option from an array * * @author oncletom * @since 2.0 * @version 1.0 */ public function fromArray(array $options) { $valid_options = array_intersect_key($options, self::$defaults); $this->is_loaded = false; /* * If we have a type defaults, we override non-defined fields */ if (isset($options['type']) && isset(self::$defaults_for_types[$options['type']])) { $this->options = array_merge($this->options, self::$defaults_for_types[$options['type']]); } /* * Finally setup given options */ $this->options = array_merge($this->options, $valid_options); } /** * Returns the default value for the option * * @author oncletom * @since 2.0 * @version 1.0 * @return mixed */ public function getDefaultValue() { return $this->options['default']; } /** * * @author oncletom * @since 2.0 * @version 1.0 * @return mixed */ public function getId() { return sprintf('%s%s', $this->prefix, $this->id); } /** * * @author oncletom * @since 2.0 * @version 1.0 * @return mixed */ public function getPossibleValues() { return $this->options['possibleValues']; } /** * * @author oncletom * @since 2.0 * @version 1.0 * @return mixed */ public function getValue() { if (!$this->is_loaded) { $value = call_user_func(self::$get_option_callback, $this->getId(), $this->getDefaultValue()); if ($value === false) { $this->is_new = true; } if ($this->options['onLoadCallback'] && is_callable($this->options['onLoadCallback'])) { $value = call_user_func($this->options['onLoadCallback'], $value); } $this->options['value'] = $value; $this->is_loaded = true; } return $this->options['value']; } /** * Returns if the option is an autoload or not * * @author oncletom * @since 2.0 * @version 1.0 * @return boolean */ public function isAutoload() { return !!$this->options['autoload']; } /** * Saves a value to the persistent storage * * @author oncletom * @since 2.0 * @version 1.0 */ public function save() { $value = $this->options['value']; if ($this->options['onSaveCallback'] && is_callable($this->options['onSaveCallback'])) { $value = call_user_func($this->options['onSaveCallback'], $value); } if ($this->is_new) { call_user_func(self::$create_option_callback, $this->getId(), $value, '', $this->isAutoload() ? 'yes' : 'no'); } else { call_user_func(self::$set_option_callback, $this->getId(), $value); } $this->is_new = false; } /** * Sets the database prefix of the object * * @author oncletom * @since 2.0 * @version 1.0 * @param string $prefix */ public function setPrefix($prefix) { $this->prefix = $prefix; } /** * Saves a value and stores it through the remote API * * @author oncletom * @since 2.0 * @version 1.0 * @param mixed $value */ public function setValue($value) { $this->options['value'] = $value; $this->save(); } }