Abstracting the WC_Meta_Data Properties with __set/__get

This commit is contained in:
Cesar Rodas 2017-05-23 19:15:13 -04:00
parent b158ba2c47
commit dafc795d7c
2 changed files with 42 additions and 19 deletions

View File

@ -1,5 +1,9 @@
<?php <?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/** /**
* Wraps an array (meta data for now) and tells if there was any changes. * Wraps an array (meta data for now) and tells if there was any changes.
* *
@ -12,13 +16,21 @@
* @author crodas * @author crodas
*/ */
class WC_Meta_Data { class WC_Meta_Data {
public $id; /**
public $key; * Current data for metadata
public $value; *
* @since 3.1.0
* @var arrray
*/
protected $current_data;
protected $previous_value = array(); /**
* Metadata data
protected $properties = array( 'id', 'key', 'value' ); *
* @since 3.1.0
* @var arrray
*/
protected $data;
/** /**
* Default constructor * Default constructor
@ -26,11 +38,7 @@ class WC_Meta_Data {
* @param Array meta data to wrap behind this function * @param Array meta data to wrap behind this function
*/ */
public function __construct( Array $meta ) { public function __construct( Array $meta ) {
foreach ( $meta as $key => $value ) { $this->current_data = $meta;
if ( in_array( $key, $this->properties ) ) {
$this->$key = $value;
}
}
$this->apply_changes(); $this->apply_changes();
} }
@ -38,9 +46,22 @@ class WC_Meta_Data {
* Merge changes with data and clear. * Merge changes with data and clear.
*/ */
public function apply_changes() { public function apply_changes() {
foreach ( $this->properties as $property ) { $this->data = $this->current_data;
$this->previous_value[ $property ] = $this->$property;
} }
public function __set( $key, $value ) {
$this->current_data[ $key ] = $value;
}
public function __isset( $key ) {
return array_key_exists( $key, $this->current_data );
}
public function __get( $key ) {
if ( array_key_exists( $key, $this->current_data )) {
return $this->current_data[ $key ];
}
return null;
} }
/** /**
@ -49,11 +70,11 @@ class WC_Meta_Data {
* @return array * @return array
*/ */
public function get_changes() { public function get_changes() {
$changes = array(); $changes = array();
foreach ( array( 'id', 'key', 'value' ) as $property ) { foreach ( $this->current_data as $id => $value) {
if ( array_key_exists( $property, $this->previous_value ) && if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) {
$this->previous_value[ $property ] !== $this->$property ) { $changes[ $id ] = $value;
$changes[ $property ] = $this->$property;
} }
} }

View File

@ -139,7 +139,7 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
*/ */
function test_get_meta_data_cache_invalidation() { function test_get_meta_data_cache_invalidation() {
$object = new WC_Order; $object = new WC_Order;
$object->add_meta_data( 'test_meta_key', 'val1', true ); $object->add_meta_data( 'test_meta_key', array( 'val1' ), true );
$object->add_meta_data( 'test_multi_meta_key', 'val2' ); $object->add_meta_data( 'test_multi_meta_key', 'val2' );
$object->add_meta_data( 'test_multi_meta_key', 'val3' ); $object->add_meta_data( 'test_multi_meta_key', 'val3' );
$object->save(); $object->save();
@ -242,7 +242,9 @@ class WC_Tests_CRUD_Data extends WC_Unit_Test_Case {
$object->set_meta_data( $metadata ); $object->set_meta_data( $metadata );
foreach ( $object->get_meta_data() as $id => $meta ) { foreach ( $object->get_meta_data() as $id => $meta ) {
$this->assertEquals( get_object_vars( $metadata[ $id ] ), get_object_vars( $meta ) ); $this->assertEquals( $metadata[ $id ]->id, $meta->id);
$this->assertEquals( $metadata[ $id ]->key, $meta->key);
$this->assertEquals( $metadata[ $id ]->value, $meta->value);
} }
} }