diff --git a/src/DataView/FieldTypeRegistry.php b/src/DataView/FieldTypeRegistry.php index 867084c..baddcdd 100644 --- a/src/DataView/FieldTypeRegistry.php +++ b/src/DataView/FieldTypeRegistry.php @@ -14,17 +14,17 @@ class FieldTypeRegistry { * * @var array */ - protected array $types = []; + protected static array $types = []; public function __construct() { - $this->register_default_types(); + if ( empty( static::$types ) ) $this->register_default_types(); } /** * Register the default field types. */ protected function register_default_types(): void { - $this->types = [ + static::$types = [ 'string' => [ 'dataset' => DataSet::TYPE_STRING, 'sanitizer' => 'sanitize_text_field', @@ -91,7 +91,7 @@ protected function register_default_types(): void { */ public function get_dataset_type( string $type ): string { $this->validate_type( $type ); - return $this->types[ $type ]['dataset']; + return static::$types[ $type ]['dataset']; } /** @@ -103,7 +103,7 @@ public function get_dataset_type( string $type ): string { */ public function get_sanitizer( string $type ): callable { $this->validate_type( $type ); - return $this->types[ $type ]['sanitizer']; + return static::$types[ $type ]['sanitizer']; } /** @@ -115,7 +115,7 @@ public function get_sanitizer( string $type ): callable { */ public function get_schema( string $type ): array { $this->validate_type( $type ); - return $this->types[ $type ]['schema']; + return static::$types[ $type ]['schema']; } /** @@ -127,7 +127,7 @@ public function get_schema( string $type ): array { */ public function get_input_type( string $type ): string { $this->validate_type( $type ); - return $this->types[ $type ]['input']; + return static::$types[ $type ]['input']; } /** @@ -137,7 +137,7 @@ public function get_input_type( string $type ): string { * @return bool True if type is registered. */ public function has_type( string $type ): bool { - return isset( $this->types[ $type ] ); + return isset( static::$types[ $type ] ); } /** @@ -155,7 +155,7 @@ public function register_type( string $name, array $config ): void { ); } } - $this->types[ $name ] = $config; + static::$types[ $name ] = $config; } /** @@ -167,7 +167,7 @@ public function register_type( string $name, array $config ): void { protected function validate_type( string $type ): void { if ( ! $this->has_type( $type ) ) { throw new \InvalidArgumentException( - sprintf( 'Unknown field type "%s". Available types: %s', $type, implode( ', ', array_keys( $this->types ) ) ) + sprintf( 'Unknown field type "%s". Available types: %s', $type, implode( ', ', array_keys( static::$types ) ) ) ); } } diff --git a/tests/phpunit/data-view.php b/tests/phpunit/data-view.php index 4926629..120a89d 100644 --- a/tests/phpunit/data-view.php +++ b/tests/phpunit/data-view.php @@ -54,6 +54,43 @@ public function test_field_type_registry_has_default_types(): void { $this->assertTrue( $registry->has_type( 'datetime' ) ); } + public function test_field_type_registry_has_shared_custom_types(): void { + $registry_1 = new FieldTypeRegistry(); + $registry_2 = new FieldTypeRegistry(); + + $registry_1->register_type( 'custom_type_1', [ + 'dataset' => DataSet::TYPE_STRING, + 'sanitizer' => fn( $value ) => ( $value ), + 'schema' => [], + 'input' => 'hidden', + ] ); + + $registry_2->register_type( 'custom_type_2', [ + 'dataset' => DataSet::TYPE_INTEGER, + 'sanitizer' => fn( $value ) => ( $value ), + 'schema' => [], + 'input' => 'hidden', + ] ); + + foreach( [ + $registry_1, + $registry_2 + ] as $registry ) { + $this->assertTrue( $registry->has_type( 'custom_type_1' ) ); + $this->assertTrue( $registry->has_type( 'custom_type_2' ) ); + + // Check defaults are not erased by custom + $this->assertTrue( $registry->has_type( 'string' ) ); + $this->assertTrue( $registry->has_type( 'text' ) ); + $this->assertTrue( $registry->has_type( 'email' ) ); + $this->assertTrue( $registry->has_type( 'url' ) ); + $this->assertTrue( $registry->has_type( 'integer' ) ); + $this->assertTrue( $registry->has_type( 'boolean' ) ); + $this->assertTrue( $registry->has_type( 'date' ) ); + $this->assertTrue( $registry->has_type( 'datetime' ) ); + } + } + public function test_field_type_registry_maps_to_dataset_types(): void { $registry = new FieldTypeRegistry();