Language code => description. */ public array $description = array(); /** @var int 4 or 6. */ public int $ip_version = 0; /** @var string[] */ public array $languages = array(); /** @var int Bytes per node: record_size * 2 / 8. */ public int $node_byte_size = 0; /** @var int Number of nodes in the search tree. */ public int $node_count = 0; /** @var int 24, 28 or 32 bits per record. */ public int $record_size = 0; /** @var int Bytes in the search tree: node_count * node_byte_size. */ public int $search_tree_size = 0; /** * Read and check a decoded metadata map. * * @param array $map The decoded metadata block. * @throws InvalidDatabaseException When a required field is missing or the format is unsupported. */ public static function from_map( array $map ): self { $meta = new self(); $meta->node_count = self::int_field( $map, 'node_count' ); if ( $meta->node_count < 1 ) { throw new InvalidDatabaseException( 'The MaxMind DB metadata node_count must be at least 1.' ); } $meta->record_size = self::int_field( $map, 'record_size' ); if ( ! in_array( $meta->record_size, array( 24, 28, 32 ), true ) ) { throw new InvalidDatabaseException( 'The MaxMind DB metadata record_size must be 24, 28, or 32.' ); } $meta->ip_version = self::int_field( $map, 'ip_version' ); if ( ! in_array( $meta->ip_version, array( 4, 6 ), true ) ) { throw new InvalidDatabaseException( 'The MaxMind DB metadata ip_version must be 4 or 6.' ); } $meta->binary_format_major_version = self::int_field( $map, 'binary_format_major_version' ); if ( self::FORMAT_MAJOR_VERSION !== $meta->binary_format_major_version ) { throw new InvalidDatabaseException( 'The MaxMind DB binary format version is not supported.' ); } $meta->binary_format_minor_version = self::int_field( $map, 'binary_format_minor_version' ); $epoch = $map['build_epoch'] ?? null; if ( ! is_int( $epoch ) && ! ( is_string( $epoch ) && 1 === preg_match( '/^\d+$/', $epoch ) ) ) { throw new InvalidDatabaseException( 'The MaxMind DB metadata build_epoch must be an unsigned integer.' ); } $meta->build_epoch = $epoch; $type = $map['database_type'] ?? null; if ( ! is_string( $type ) ) { throw new InvalidDatabaseException( 'The MaxMind DB metadata database_type must be a string.' ); } $meta->database_type = $type; $meta->description = self::string_map( $map, 'description' ); $meta->languages = array_values( self::string_list( $map, 'languages' ) ); $meta->node_byte_size = intdiv( $meta->record_size * 2, 8 ); $meta->search_tree_size = $meta->node_count * $meta->node_byte_size; return $meta; } /** * @param array $map * @throws InvalidDatabaseException When the field is missing or not an integer. */ private static function int_field( array $map, string $key ): int { $value = $map[ $key ] ?? null; if ( ! is_int( $value ) ) { throw new InvalidDatabaseException( "The MaxMind DB metadata $key must be an integer." ); } return $value; } /** * Optional: a map of strings. Absent means empty. * * @param array $map * @return array * @throws InvalidDatabaseException When present but not a map of strings. */ private static function string_map( array $map, string $key ): array { if ( ! isset( $map[ $key ] ) ) { return array(); } $value = $map[ $key ]; if ( ! is_array( $value ) ) { throw new InvalidDatabaseException( "The MaxMind DB metadata $key must be a map." ); } foreach ( $value as $item ) { if ( ! is_string( $item ) ) { throw new InvalidDatabaseException( "The MaxMind DB metadata $key must contain only strings." ); } } return $value; } /** * Optional: a list of strings. Absent means empty. * * @param array $map * @return string[] * @throws InvalidDatabaseException When present but not a list of strings. */ private static function string_list( array $map, string $key ): array { if ( ! isset( $map[ $key ] ) ) { return array(); } $value = $map[ $key ]; if ( ! is_array( $value ) ) { throw new InvalidDatabaseException( "The MaxMind DB metadata $key must be an array of strings." ); } foreach ( $value as $item ) { if ( ! is_string( $item ) ) { throw new InvalidDatabaseException( "The MaxMind DB metadata $key must contain only strings." ); } } return $value; } }