      1: <?php
      2:
      3: /**
      4:  * Class Sidecar_Settings_Base
      5:  */
      6: class Sidecar_Settings_Base extends ArrayObject {
      7:
      8:   /**
      9:    * @var Sidecar_Plugin_Base|Sidecar_Settings_Base
     10:    */
     11:   protected $_parent;
     12:
     13:   /**
     14:    * @var bool
     15:    */
     16:   protected $_is_dirty = false;
     17:
     18:   /**
     19:    * @param Sidecar_Plugin_Base|Sidecar_Settings_Base $parent
     20:    */
     21:   function __construct( $parent ) {
     22:     $this->_parent = $parent;
     23:   }
     24:
     25:   /**
     26:    * Register a setting
     27:    * @param string $setting_name
     28:    */
     29:   function register_setting( $setting_name ) {
     30:     $this->offsetSet( $setting_name, false );
     31:   }
     32:
     33:   /**
     34:    * @param string $setting_name
     35:    *
     36:    * @return bool
     37:    */
     38:   function has_setting( $setting_name ) {
     39:     return $this->offsetExists( $setting_name );
     40:   }
     41:
     42:   /**
     43:    * @param string $setting_name
     44:    *
     45:    * @return bool|mixed|Sidecar_Settings_Base
     46:    */
     47:   function get_setting( $setting_name ) {
     48:     return $this->offsetGet( $setting_name );
     49:   }
     50:
     51:   /**
     52:    * @param string $setting_name
     53:    * @param mixed $setting_value
     54:    *
     55:    */
     56:   function set_setting( $setting_name, $setting_value ) {
     57:     $this->offsetSet( $setting_name, $setting_value );
     58:   }
     59:
     60:   /**
     61:    * @return array
     62:    */
     63:   function get_values() {
     64:     return $this->getArrayCopy();
     65:   }
     66:
     67:   /**
     68:    * @param string $setting_name
     69:    * @return array
     70:    */
     71:   function get_value( $setting_name ) {
     72:     return $this->offsetGet( $setting_name );
     73:   }
     74:
     75:   /**
     76:    * @return array
     77:    */
     78:   function get_values_deep() {
     79:     $values = array();
     80:     /**
     81:      * @var mixed|Sidecar_Settings_Base $value
     82:      */
     83:     foreach( $this->getArrayCopy() as $name => $value ) {
     84:       $values[$name] = method_exists( $value, 'get_values' ) ? $value->get_values() : $value;
     85:     }
     86:     return $values;
     87:   }
     88:
     89:   /**
     90:    * @param $is_dirty
     91:    */
     92:   function set_dirty( $is_dirty ) {
     93:     $this->_parent->set_dirty( $is_dirty );
     94:   }
     95:
     96:   /**
     97:    * @param array $settings_values
     98:    */
     99:   function set_values( $settings_values ) {
    100:
    101:     if ( ! is_array( $settings_values ) )
    102:       if ( empty( $settings_values ) ) {
    103:         $settings_values = array();
    104:         $this->set_dirty( true );
    105:       } else {
    106:         $settings_values = (array)$settings_values;
    107:       }
    108:
    109:     if ( $this->getArrayCopy() !== $settings_values ) {
    110:       $this->exchangeArray( $settings_values );
    111:       $this->set_dirty( true );
    112:     }
    113:
    114:   }
    115:
    116:   /**
    117:    * @param string $setting_name
    118:    * @param mixed $setting_value
    119:    * @param bool $set_dirty
    120:    * @return array
    121:    */
    122:   function update_settings_value( $setting_name, $setting_value, $set_dirty = true ) {
    123:     $this->offsetSet( $setting_name, $setting_value );
    124:     if ( $set_dirty )
    125:       $this->set_dirty( true );
    126:   }
    127:
    128:   /**
    129:    */
    130:   function update_settings() {
    131:     $this->set_dirty( true );
    132:   }
    133:
    134:   /**
    135:    * @return bool
    136:    */
    137:   function is_dirty() {
    138:     return $this->_is_dirty;
    139:   }
    140:
    141:   /**
    142:    * @todo verify this is needed in base class
    143:    * Get a representation of the encryption status
    144:    * @return bool
    145:    */
    146:   function is_encrypted() {
    147:     return $this->_parent->is_encrypted();
    148:   }
    149:
    150:   /**
    151:    * @todo verify this is needed in base class
    152:    * @param bool $is_encrypted
    153:    */
    154:   function set_encrypted( $is_encrypted ) {
    155:     $this->_parent->set_encrypted( $is_encrypted );
    156:   }
    157:
    158:   /**
    159:    * @param int|string $offset
    160:    *
    161:    * @return Sidecar_Settings_Base|string|null
    162:    */
    163:   function offsetGet( $offset ) {
    164:     return $this->offsetExists( $offset ) ? parent::offsetGet( $offset ) : false;
    165:     }
    166:
    167: //  /**
    168: //   * @return ArrayIterator
    169: //   */
    170: //  function getIterator() {
    171: //      return new ArrayIterator( $this->_settings_values );
    172: //  }
    173: //
    174: //  /**
    175: //   * @param int|string $offset
    176: //   *
    177: //   * @return bool
    178: //   */
    179: //  function offsetExists( $offset ) {
    180: //      return isset( $this->_settings_values[$offset] );
    181: //  }
    182: //
    183: //  /**
    184: //   * @param int|string $offset
    185: //   * @param Sidecar_Settings_Base|string|null $value
    186: //   */
    187: //  function offsetSet( $offset , $value ) {
    188: //    if ( is_null( $offset ) ) {
    189: //        $this->_settings_values[] = $value;
    190: //    } else {
    191: //        $this->_settings_values[$offset] = $value;
    192: //    }
    193: //  }
    194: //
    195: //  /**
    196: //   * @param int|string $offset
    197: //   */
    198: //  function offsetUnset( $offset ) {
    199: //      unset( $this->_settings_values[$offset] );
    200: //  }
    201: }
    202: