Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Data.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Data |
| 5 | * |
| 6 | * A value object implementing the ArrayAccess interface. |
| 7 | * |
| 8 | * Example usage: |
| 9 | * $my_data = array( 'lorem' => 'dolor' ); |
| 10 | * |
| 11 | * // by default return 'nope' when a value is not set in the data |
| 12 | * $data = new Tribe__Data( $my_data, 'nope' ); |
| 13 | * |
| 14 | * // set some values in the data |
| 15 | * $data['foo'] = 'bar'; |
| 16 | * $data['bar'] = 23; |
| 17 | * |
| 18 | * // fetch some values |
| 19 | * $var_1 = $data['foo']; // "bar" |
| 20 | * $var_2 = $data['bar']; // 23 |
| 21 | * $var_3 = $data['lorem']; // "dolor" |
| 22 | * $var_4 = $data['woo']; // "nope" |
| 23 | * |
| 24 | * $data->set_default( 'not found' ); |
| 25 | * |
| 26 | * $var_4 = $data['woo']; // "not found" |
| 27 | * |
| 28 | */ |
| 29 | class Tribe__Data implements ArrayAccess, Iterator { |
| 30 | /** |
| 31 | * @var int |
| 32 | */ |
| 33 | protected $index = 0; |
| 34 | |
| 35 | /** |
| 36 | * @var array The data managed by this object. |
| 37 | */ |
| 38 | protected $data; |
| 39 | |
| 40 | /** |
| 41 | * @var mixed The default value that will be returned when trying to get the value |
| 42 | * of a non set key. |
| 43 | */ |
| 44 | protected $default; |
| 45 | |
| 46 | /** |
| 47 | * Tribe__Data constructor. |
| 48 | * |
| 49 | * @param array|object $data An array or object of data. |
| 50 | * @param mixed $default The default value that should be returned if a key is not set |
| 51 | */ |
| 52 | public function __construct( $data = [], $default = false ) { |
| 53 | $this->data = (array) $data; |
| 54 | $this->default = $default; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Whether a offset exists |
| 59 | * |
| 60 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 61 | * @param mixed $offset <p> |
| 62 | * An offset to check for. |
| 63 | * </p> |
| 64 | * @return boolean true on success or false on failure. |
| 65 | * </p> |
| 66 | * <p> |
| 67 | * The return value will be casted to boolean if non-boolean was returned. |
| 68 | * @since 4.11.0 |
| 69 | */ |
| 70 | public function offsetExists( $offset ) { |
| 71 | return isset( $this->data[ $offset ] ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Offset to retrieve |
| 76 | * |
| 77 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 78 | * @param mixed $offset <p> |
| 79 | * The offset to retrieve. |
| 80 | * </p> |
| 81 | * @return mixed Can return all value types. |
| 82 | * @since 4.11.0 |
| 83 | */ |
| 84 | public function offsetGet( $offset ) { |
| 85 | return isset( $this->data[ $offset ] ) |
| 86 | ? $this->data[ $offset ] |
| 87 | : $this->default; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Offset to set |
| 92 | * |
| 93 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 94 | * @param mixed $offset <p> |
| 95 | * The offset to assign the value to. |
| 96 | * </p> |
| 97 | * @param mixed $value <p> |
| 98 | * The value to set. |
| 99 | * </p> |
| 100 | * @return void |
| 101 | * @since 4.11.0 |
| 102 | */ |
| 103 | public function offsetSet( $offset, $value ) { |
| 104 | $this->data[ $offset ] = $value; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Offset to unset |
| 109 | * |
| 110 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 111 | * @param mixed $offset <p> |
| 112 | * The offset to unset. |
| 113 | * </p> |
| 114 | * @return void |
| 115 | * @since 4.11.0 |
| 116 | */ |
| 117 | public function offsetUnset( $offset ) { |
| 118 | unset( $this->data[ $offset ] ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Gets the data this object manages. |
| 123 | * |
| 124 | * @return array |
| 125 | */ |
| 126 | public function get_data() { |
| 127 | return $this->data; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the data this object will manage. |
| 132 | * |
| 133 | * @param array $data |
| 134 | */ |
| 135 | public function set_data( array $data ) { |
| 136 | $this->data = $data; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets the default value that will be returned when a key is not set. |
| 141 | * |
| 142 | * @return mixed |
| 143 | */ |
| 144 | public function get_default() { |
| 145 | return $this->default; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets the default value that should be returned when a key is not set. |
| 150 | * |
| 151 | * @param mixed $default |
| 152 | */ |
| 153 | public function set_default( $default ) { |
| 154 | $this->default = $default; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Return the current element |
| 159 | * |
| 160 | * @link http://php.net/manual/en/iterator.current.php |
| 161 | * @return mixed Can return any type. |
| 162 | * @since 4.11.0 |
| 163 | */ |
| 164 | public function current() { |
| 165 | $keys = array_keys( $this->data ); |
| 166 | |
| 167 | return $this->data[ $keys[ $this->index ] ]; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Move forward to next element |
| 172 | * |
| 173 | * @link http://php.net/manual/en/iterator.next.php |
| 174 | * @return void Any returned value is ignored. |
| 175 | * @since 4.11.0 |
| 176 | */ |
| 177 | public function next() { |
| 178 | $keys = array_keys( $this->data ); |
| 179 | |
| 180 | if ( isset( $keys[ ++ $this->index ] ) ) { |
| 181 | return $this->data[ $keys[ $this->index ] ]; |
| 182 | } |
| 183 | |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Return the key of the current element |
| 189 | * |
| 190 | * @link http://php.net/manual/en/iterator.key.php |
| 191 | * @return mixed scalar on success, or null on failure. |
| 192 | * @since 4.11.0 |
| 193 | */ |
| 194 | public function key() { |
| 195 | $keys = array_keys( $this->data ); |
| 196 | |
| 197 | return $keys[ $this->index ]; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Checks if current position is valid |
| 202 | * |
| 203 | * @link http://php.net/manual/en/iterator.valid.php |
| 204 | * @return boolean The return value will be casted to boolean and then evaluated. |
| 205 | * Returns true on success or false on failure. |
| 206 | * @since 4.11.0 |
| 207 | */ |
| 208 | public function valid() { |
| 209 | $keys = array_keys( $this->data ); |
| 210 | |
| 211 | return isset( $keys[ $this->index ] ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Rewind the Iterator to the first element |
| 216 | * |
| 217 | * @link http://php.net/manual/en/iterator.rewind.php |
| 218 | * @return void Any returned value is ignored. |
| 219 | * @since 4.11.0 |
| 220 | */ |
| 221 | public function rewind() { |
| 222 | $this->index = 0; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Converts the data object in an array. |
| 227 | * |
| 228 | * @return array |
| 229 | * |
| 230 | * @since 4.6 |
| 231 | */ |
| 232 | public function to_array() { |
| 233 | return $this->get_data(); |
| 234 | } |
| 235 | } |
| 236 |