From f02893bb34cf24d55fece2e8e21542ac5e462908 Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Tue, 14 Nov 2017 16:44:04 -0200 Subject: [PATCH] improve filters and metadata structure --- .../entities/class-tainacan-filter.php | 41 ++++++++++--------- .../entities/class-tainacan-metadata.php | 31 +++++++------- .../repositories/class-tainacan-filters.php | 4 +- .../repositories/class-tainacan-metadatas.php | 4 +- tests/test-filters.php | 8 ++-- tests/test-metadata.php | 6 +-- 6 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index 3506dd7dc..6eaf94ee6 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -48,12 +48,12 @@ class Tainacan_Filter extends Tainacan_Entity { return new Tainacan_Metadata( $id ); } - function get_widget( $output = 'object' ){ - if( $output === 'object'){ - return unserialize( $this->get_mapped_property('option') ); - } else{ - return $this->get_mapped_property('widget'); - } + function get_filter_type_object(){ + return unserialize( $this->get_mapped_property('filter_type_object') ); + } + + function get_filter_type(){ + return $this->get_mapped_property('filter_type'); } // Setters @@ -82,21 +82,22 @@ class Tainacan_Filter extends Tainacan_Entity { return $this->set_mapped_property('metadata', $id); } - function set_widget($value){ - if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Filter_Type' ) && $this->get_metadata() ){ - $type = $this->get_metadata()->get_type(); - - //if filter matches the metadata type - if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){ - $this->set_option( $value ); - return $this->set_mapped_property('widget', get_class( $value ) ) ; - } - - } - return null; + function set_filter_type_object( Tainacan_Filter_Type $value ){ + // TODO: validate primitive type with filter + //if filter matches the metadata type + //if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){ + $this->set_filter_type( get_class( $value ) ); + return $this->set_mapped_property('filter_type_object',serialize($value) ) ; + //} } - function set_option($value){ - return $this->set_mapped_property('option', serialize($value) ) ; + /** + * este metodo eh privado pois eh setado automaticamente pelo metodo set_filter_type_object + * + * @param $value + * + */ + private function set_filter_type($value){ + return $this->set_mapped_property('filter_type', $value ); } } \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index 3b9f4e715..60a612924 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -75,12 +75,12 @@ class Tainacan_Metadata extends Tainacan_Entity { return $this->get_mapped_property('default_value'); } - function get_type( $output = 'object' ){ - if( $output === 'object'){ - return unserialize( $this->get_mapped_property('option') ); - }else{ - return $this->get_mapped_property('type'); - } + function get_field_type_object(){ + return unserialize( $this->get_mapped_property('field_type_object') ); + } + + function get_field_type(){ + return $this->get_mapped_property('field_type'); } // Setters @@ -128,16 +128,19 @@ class Tainacan_Metadata extends Tainacan_Entity { return $this->set_mapped_property('default_property', $value); } - function set_type($value){ - if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Field_Type' ) ){ - $this->set_option( $value ); - return $this->set_mapped_property('type', get_class( $value ) ) ; - } - return null; + function set_field_type_object(Tainacan_Field_Type $value){ + $this->set_field_type( get_class( $value ) ); + return $this->set_mapped_property('field_type_object', serialize($value) ) ; } - function set_option($value){ - return $this->set_mapped_property('option', serialize($value) ) ; + /** + * este metodo eh privado pois eh setado automaticamente pelo metodo set_field_type_object + * + * @param $value + * + */ + private function set_field_type($value){ + return $this->set_mapped_property('field_type', $value ) ; } // helpers diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 9b79c2f3a..e8cb4b22e 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -24,11 +24,11 @@ class Tainacan_Filters { 'map' => 'post_content', 'validation' => '' ], - 'widget' => [ + 'filter_type_object' => [ 'map' => 'meta', 'validation' => '' ], - 'option' => [ + 'filter_type' => [ 'map' => 'meta', 'validation' => '' ], diff --git a/src/classes/repositories/class-tainacan-metadatas.php b/src/classes/repositories/class-tainacan-metadatas.php index aed27bee4..515e9ed62 100644 --- a/src/classes/repositories/class-tainacan-metadatas.php +++ b/src/classes/repositories/class-tainacan-metadatas.php @@ -31,7 +31,7 @@ class Tainacan_Metadatas { 'map' => 'post_content', 'validation' => '' ], - 'type' => [ + 'field_type' => [ 'map' => 'meta', 'validation' => '' ], @@ -67,7 +67,7 @@ class Tainacan_Metadatas { 'map' => 'meta', 'validation' => '' ], - 'option' => [ + 'field_type_object' => [ 'map' => 'meta', 'validation' => '' ], diff --git a/tests/test-filters.php b/tests/test-filters.php index 9409e6b47..2e8077952 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -49,7 +49,7 @@ class Test_Filters extends WP_UnitTestCase { //setando os valores na classe do metadado $metadata->set_name('metadado'); $metadata->set_collection_id( $collection->get_id() ); - $metadata->set_type( $type ); + $metadata->set_field_type_object( $type ); //inserindo o metadado @@ -61,9 +61,9 @@ class Test_Filters extends WP_UnitTestCase { $filter->set_metadata( $metadata ); //nao devera permitir um filtro Range para o tipo string - $this->assertTrue( $filter->set_widget( $filter_range_type ) === null ); + $this->assertTrue( $filter->set_filter_type_object( $filter_range_type ) === null ); - $filter->set_widget( $filter_list_type ); + $filter->set_filter_type_object( $filter_list_type ); $filter = $Tainacan_Filters->insert( $filter ); @@ -72,7 +72,7 @@ class Test_Filters extends WP_UnitTestCase { $this->assertEquals( $test->get_name(), 'filtro'); $this->assertEquals( $test->get_collection_id(), $collection->get_id() ); $this->assertEquals( $test->get_metadata()->get_id(), $metadata->get_id() ); - $this->assertEquals( get_class( $test->get_widget() ), get_class( $filter_list_type ) ); + $this->assertEquals( get_class( $test->get_filter_type_object() ), get_class( $filter_list_type ) ); } diff --git a/tests/test-metadata.php b/tests/test-metadata.php index 60e3f91db..1fa43a05b 100644 --- a/tests/test-metadata.php +++ b/tests/test-metadata.php @@ -54,7 +54,7 @@ class Test_Metadata extends WP_UnitTestCase { //setando os valores na classe do metadado $metadata->set_name('metadado'); $metadata->set_collection_id( $collection->get_id() ); - $metadata->set_type( $type ); + $metadata->set_field_type_object( $type ); //inserindo o metadado @@ -64,7 +64,7 @@ class Test_Metadata extends WP_UnitTestCase { $this->assertEquals($test->get_name(), 'metadado'); $this->assertEquals($test->get_collection_id(), $collection->get_id()); - $this->assertEquals($test->get_type('name'), 'Tainacan_Text_Field_Type'); - $this->assertEquals($test->get_type(), $type); + $this->assertEquals($test->get_field_type(), 'Tainacan_Text_Field_Type'); + $this->assertEquals($test->get_field_type_object(), $type); } } \ No newline at end of file