Accessor.php
234 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Session; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use stdClass; |
| 7 | |
| 8 | /** |
| 9 | * Class Access |
| 10 | * |
| 11 | * In legacy core session data load in array which contains multiple keys like give_purchase, receipt_access etc. |
| 12 | * This class helps to convert them into objects. Every subclass will treat a specific key as group of session data. |
| 13 | * |
| 14 | * @package Give\Session |
| 15 | */ |
| 16 | abstract class Accessor { |
| 17 | /** |
| 18 | * Session Id. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $sessionKey; |
| 23 | |
| 24 | /** |
| 25 | * Session data as array. |
| 26 | * We use this array internally to perform database related operation. |
| 27 | * |
| 28 | * @var mixed |
| 29 | */ |
| 30 | protected $data; |
| 31 | |
| 32 | /** |
| 33 | * Session data as object. |
| 34 | * Session data in object format will be return when query. |
| 35 | * |
| 36 | * @var stdClass |
| 37 | */ |
| 38 | protected $dataObj; |
| 39 | |
| 40 | /** |
| 41 | * Class constructor. |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | $this->get(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Convert session data to object. |
| 49 | * |
| 50 | * @param array $data |
| 51 | * |
| 52 | * @return stdClass |
| 53 | * @since 2.7.0 |
| 54 | */ |
| 55 | protected function convertToObject( $data ) { |
| 56 | $dataObj = new stdClass(); |
| 57 | $data = $this->renameArrayKeysToPropertyNames( $data ); |
| 58 | |
| 59 | foreach ( $data as $key => $value ) { |
| 60 | if ( is_array( $value ) ) { |
| 61 | $dataObj->{$key} = $this->convertToObject( $value ); |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | $dataObj->{$key} = $value; |
| 66 | } |
| 67 | |
| 68 | return $dataObj; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get data from session. |
| 73 | * |
| 74 | * @return stdClass |
| 75 | * @since 2.7.0 |
| 76 | */ |
| 77 | public function get() { |
| 78 | if ( $this->dataObj ) { |
| 79 | return $this->dataObj; |
| 80 | } |
| 81 | |
| 82 | $this->data = Give()->session->get( $this->sessionKey, $this->data ); |
| 83 | $this->dataObj = $this->convertToObject( $this->data ); |
| 84 | |
| 85 | return $this->dataObj; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get data from session. |
| 90 | * |
| 91 | * @param string $key |
| 92 | * |
| 93 | * @return stdClass |
| 94 | * @since 2.7.0 |
| 95 | */ |
| 96 | public function getByKey( $key ) { |
| 97 | $result = null; |
| 98 | |
| 99 | if ( null !== $this->dataObj ) { |
| 100 | $result = property_exists( $this->dataObj, $key ) ? $this->dataObj->{$key} : $result; |
| 101 | } |
| 102 | return $result; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Save/Replace/Remove data into session |
| 107 | * |
| 108 | * @param string $key |
| 109 | * @param mixed $data |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function store( $key, $data ) { |
| 114 | $this->validateData( $data ); |
| 115 | |
| 116 | if ( ! empty( $this->data[ $key ] ) ) { |
| 117 | // Merge data. |
| 118 | $this->data[ $key ] = array_merge( |
| 119 | $this->data[ $key ], |
| 120 | $data |
| 121 | ); |
| 122 | |
| 123 | } else { |
| 124 | $this->data[ $key ] = $data; |
| 125 | } |
| 126 | |
| 127 | return $this->set(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Store data into session. |
| 133 | * |
| 134 | * @return string |
| 135 | * @since 2.7.0 |
| 136 | */ |
| 137 | protected function set() { |
| 138 | $this->dataObj = $this->convertToObject( $this->data ); |
| 139 | |
| 140 | return Give()->session->set( $this->sessionKey, $this->data ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Replace session data. |
| 145 | * |
| 146 | * @param string $key |
| 147 | * @param mixed $data |
| 148 | * |
| 149 | * @return string |
| 150 | * @since 2.7.0 |
| 151 | */ |
| 152 | public function replace( $key, $data ) { |
| 153 | $this->validateData( $data ); |
| 154 | $this->data[ $key ] = $data; |
| 155 | |
| 156 | return $this->set(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Delete session data. |
| 161 | * |
| 162 | * @param string $key |
| 163 | * |
| 164 | * @return string |
| 165 | * @since 2.7.0 |
| 166 | */ |
| 167 | public function delete( $key ) { |
| 168 | if ( array_key_exists( $key, $this->data ) ) { |
| 169 | unset( $this->data[ $key ] ); |
| 170 | } |
| 171 | |
| 172 | return $this->set(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Return session data in array format. |
| 177 | * |
| 178 | * @return array |
| 179 | * @since 2.7.0 |
| 180 | */ |
| 181 | public function toArray() { |
| 182 | return $this->data; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Rename array key to property name |
| 187 | * |
| 188 | * @param array $data |
| 189 | * |
| 190 | * @return array |
| 191 | * @since 2.7.0 |
| 192 | */ |
| 193 | protected function renameArrayKeysToPropertyNames( $data ) { |
| 194 | |
| 195 | foreach ( $data as $key => $value ) { |
| 196 | // Convert array key string to property name. |
| 197 | // Remove other then char, dash, give related prefix and hyphen and prefix. |
| 198 | $newName = preg_replace( '/[^a-zA-Z0-9_\-]/', '', $key ); |
| 199 | $newName = preg_replace( '/(-|_)?give(-|_)?/', '', $newName ); |
| 200 | $keyParts = preg_split( '/(-|_)/', $newName ); |
| 201 | $keyParts = array_map( 'ucfirst', array_filter( $keyParts ) ); |
| 202 | $newName = lcfirst( implode( '', $keyParts ) ); |
| 203 | |
| 204 | // Remove old key/value pair if renamed. |
| 205 | if ( $key !== $newName ) { |
| 206 | unset( $data[ $key ] ); |
| 207 | } |
| 208 | |
| 209 | if ( is_array( $value ) ) { |
| 210 | // Process array. |
| 211 | $data[ $newName ] = $this->renameArrayKeysToPropertyNames( $value ); |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | $data[ $newName ] = $value; |
| 216 | } |
| 217 | |
| 218 | return $data; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Validate data. |
| 223 | * |
| 224 | * @param mixed $data |
| 225 | * |
| 226 | * @since 2.7.0 |
| 227 | */ |
| 228 | protected function validateData( $data ) { |
| 229 | if ( is_array( $data ) && isset( $data[0] ) ) { |
| 230 | throw new InvalidArgumentException( 'Invalid value. Please pass an associative array' ); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 |