| 1 |
<?php |
| 2 |
|
| 3 |
namespace JewelTheme\AdminBarEditor\Inc; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 |
|
| 7 |
abstract class Base_Data { |
| 8 |
|
| 9 |
private $data; |
| 10 |
|
| 11 |
final public function get( $data = null ) { |
| 12 |
$this->ensure_data(); |
| 13 |
|
| 14 |
return self::get_items( $this->data, $data ); |
| 15 |
} |
| 16 |
|
| 17 |
final public function set( $key, $value = null ) { |
| 18 |
if ( is_array( $key ) ) { |
| 19 |
$this->data = array_merge( $this->get_defaults(), $key ); |
| 20 |
} else { |
| 21 |
$this->ensure_data(); |
| 22 |
$this->data[ $key ] = $value; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public function delete( $key = null ) { |
| 27 |
if ( $key ) { |
| 28 |
unset( $this->data[ $key ] ); |
| 29 |
} else { |
| 30 |
$this->data = []; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
final public function set_recursive( $key, array $value ) { |
| 35 |
$this->ensure_data(); |
| 36 |
|
| 37 |
$data = &$this->data[ $key ]; |
| 38 |
|
| 39 |
$data = $this->merge_properties( $data, $value ); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
final public function merge_properties( array $default_props, array $custom_props, array $allowed_props_keys = [] ) { |
| 44 |
$props = array_replace_recursive( $default_props, $custom_props ); |
| 45 |
|
| 46 |
if ( $allowed_props_keys ) { |
| 47 |
$props = array_intersect_key( $props, array_flip( $allowed_props_keys ) ); |
| 48 |
} |
| 49 |
|
| 50 |
return $props; |
| 51 |
} |
| 52 |
|
| 53 |
final protected static function get_items( array $haystack, $needle = null ) { |
| 54 |
if ( $needle ) { |
| 55 |
return isset( $haystack[ $needle ] ) ? $haystack[ $needle ] : null; |
| 56 |
} |
| 57 |
|
| 58 |
return $haystack; |
| 59 |
} |
| 60 |
|
| 61 |
protected function get_defaults() { |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
private function ensure_data() { |
| 66 |
if ( null === $this->data ) { |
| 67 |
$this->data = $this->get_defaults(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function has_own_method( $method_name, $base_class_name = null ) { |
| 72 |
try { |
| 73 |
$reflection_method = new \ReflectionMethod( $this, $method_name ); |
| 74 |
$declaring_class = $reflection_method->getDeclaringClass(); |
| 75 |
} catch ( \Exception $e ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
if ( $base_class_name ) { |
| 80 |
return $base_class_name !== $declaring_class->name; |
| 81 |
} |
| 82 |
|
| 83 |
return get_called_class() === $declaring_class->name; |
| 84 |
} |
| 85 |
|
| 86 |
} |