Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/DataView/FieldTypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class FieldTypeRegistry {
*
* @var array<string, array{dataset: string, sanitizer: callable|string, schema: array, input: string}>
*/
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',
Expand Down Expand Up @@ -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'];
}

/**
Expand All @@ -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'];
}

/**
Expand All @@ -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'];
}

/**
Expand All @@ -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'];
}

/**
Expand All @@ -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 ] );
}

/**
Expand All @@ -155,7 +155,7 @@ public function register_type( string $name, array $config ): void {
);
}
}
$this->types[ $name ] = $config;
static::$types[ $name ] = $config;
}

/**
Expand All @@ -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 ) ) )
);
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/data-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down