ACF\AI\GEO

Schema::find_minimal_type_setprivate staticACF 6.8.0

Find minimal set of types to cover all properties

Uses a greedy algorithm to find the smallest set of types that collectively support all given properties.

Method of the class: Schema{}

No Hooks.

Returns

Array. Array of type names

Usage

$result = Schema::find_minimal_type_set( $properties );
$properties(array) (required)
Array of property names.

Changelog

Since 6.8.0 Introduced.

Schema::find_minimal_type_set() code ACF 6.8.8

private static function find_minimal_type_set( $properties ) {
	$uncovered_properties = $properties;
	$selected_types       = array();

	$type_hierarchy   = SchemaData::get_type_hierarchy();
	$property_domains = SchemaData::get_property_domains();

	while ( ! empty( $uncovered_properties ) ) {
		$best_type     = null;
		$best_coverage = 0;

		// Find the type that covers the most uncovered properties
		foreach ( $type_hierarchy as $type => $parent ) {
			$coverage = 0;
			foreach ( $uncovered_properties as $property ) {
				if ( isset( $property_domains[ $property ] ) ) {
					$valid_types = $property_domains[ $property ];
					// Check if this type or any of its parents support the property
					$type_chain = array_merge( array( $type ), self::get_type_parents( $type ) );
					if ( ! empty( array_intersect( $type_chain, $valid_types ) ) ) {
						++$coverage;
					}
				}
			}

			if ( $coverage > $best_coverage ) {
				$best_type     = $type;
				$best_coverage = $coverage;
			}
		}

		if ( null === $best_type ) {
			break; // No type covers remaining properties
		}

		$selected_types[] = $best_type;

		// Remove covered properties
		$type_chain           = array_merge( array( $best_type ), self::get_type_parents( $best_type ) );
		$uncovered_properties = array_filter(
			$uncovered_properties,
			function ( $property ) use ( $type_chain, $property_domains ) {
				if ( ! isset( $property_domains[ $property ] ) ) {
					return true;
				}
				$valid_types = $property_domains[ $property ];
				return empty( array_intersect( $type_chain, $valid_types ) );
			}
		);
	}

	return $selected_types;
}