PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / meta-data.php

meta-data.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/classes/meta-data.php

117 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Classes;
4
5 use JsonSerializable;
6
7 /**
8 * @property ?int $id
9 * @property string $key
10 * @property mixed $value
11 */
12 class MetaData implements JsonSerializable {
13
14 /**
15 * Current data for metadata
16 *
17 * @var array
18 */
19 protected array $current_data;
20
21 /**
22 * Metadata data
23 *
24 * @var array
25 */
26 protected array $data;
27
28 /**
29 * Constructor.
30 *
31 * @param array $meta Data to wrap behind this function.
32 */
33 public function __construct( array $meta = [] ) {
34 $this->current_data = $meta;
35 $this->apply_changes();
36 }
37
38 /**
39 * When converted to JSON.
40 *
41 * @return array
42 */
43 #[\ReturnTypeWillChange]
44 public function jsonSerialize(): array {
45 return $this->get_data();
46 }
47
48 /**
49 * Merge changes with data and clear.
50 */
51 public function apply_changes() {
52 $this->data = $this->current_data;
53 }
54
55 /**
56 * Creates or updates a property in the metadata object.
57 *
58 * @param string $key Key to set.
59 * @param mixed $value Value to set.
60 */
61 public function __set( string $key, $value ) {
62 $this->current_data[ $key ] = $value;
63 }
64
65 /**
66 * Checks if a given key exists in our data. This is called internally
67 * by `empty` and `isset`.
68 *
69 * @param string $key Key to check if set.
70 *
71 * @return bool
72 */
73 public function __isset( string $key ) {
74 return array_key_exists( $key, $this->current_data );
75 }
76
77 /**
78 * Returns the value of any property.
79 *
80 * @param string $key Key to get.
81 * @return mixed Property value or NULL if it does not exists
82 */
83 public function __get( string $key ) {
84 if ( array_key_exists( $key, $this->current_data ) ) {
85 return $this->current_data[ $key ];
86 }
87 return null;
88 }
89
90 /**
91 * Return data changes only.
92 *
93 * @return array
94 */
95 public function get_changes(): array {
96 $changes = [];
97 foreach ( $this->current_data as $id => $value ) {
98 if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) {
99 $changes[ $id ] = $value;
100 }
101 }
102
103 return $changes;
104 }
105
106 /**
107 * Return all data as an array.
108 *
109 * @return array
110 */
111 public function get_data(): array {
112 return $this->data;
113 }
114 }
115
116 // End of file meta-data.php
117