| 1 |
<?php |
| 2 |
|
| 3 |
namespace OptimoleWP\PageProfiler\Storage; |
| 4 |
|
| 5 |
/** |
| 6 |
* Abstract base class for storage implementations. |
| 7 |
* |
| 8 |
* This class defines the interface for storage operations that concrete |
| 9 |
* implementations must provide. |
| 10 |
*/ |
| 11 |
abstract class Base { |
| 12 |
|
| 13 |
/** |
| 14 |
* Store data with the given key. |
| 15 |
* |
| 16 |
* @param string $key The unique identifier for the data. |
| 17 |
* @param array $data The data to store. |
| 18 |
*/ |
| 19 |
abstract public function store( string $key, array $data ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Retrieve data by key. |
| 23 |
* |
| 24 |
* @param string $key The unique identifier for the data to retrieve. |
| 25 |
* @return array<string, mixed>|false The stored data or false if not found. |
| 26 |
*/ |
| 27 |
abstract public function get( string $key ); |
| 28 |
|
| 29 |
/** |
| 30 |
* Coerce a stored profiler payload to an array. |
| 31 |
* |
| 32 |
* Object-cache backends that JSON-decode without associative arrays return stdClass. |
| 33 |
* Nested objects (e.g. `af`, `bg`, `lcp`) are converted recursively. |
| 34 |
* |
| 35 |
* @param mixed $value Raw storage value. |
| 36 |
* @return array<string|int, mixed>|false |
| 37 |
*/ |
| 38 |
public static function normalize_value( $value ) { |
| 39 |
if ( false === $value || null === $value ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if ( is_object( $value ) ) { |
| 44 |
$value = get_object_vars( $value ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! is_array( $value ) ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
foreach ( $value as $key => $item ) { |
| 52 |
if ( is_object( $item ) || is_array( $item ) ) { |
| 53 |
$normalized_item = self::normalize_value( $item ); |
| 54 |
$value[ $key ] = ( false !== $normalized_item ) ? $normalized_item : []; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $value; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Delete data by key. |
| 63 |
* |
| 64 |
* @param string $key The unique identifier for the data to delete. |
| 65 |
*/ |
| 66 |
abstract public function delete( string $key ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Delete all stored data. |
| 70 |
*/ |
| 71 |
abstract public function delete_all(); |
| 72 |
} |
| 73 |
|