column index, columns: [{index, header, field, state}], unmapped: int[], ignored: int[], key_ok: bool} */ public static function map( array $header ): array { $map = array(); $columns = array(); $unmapped = array(); $ignored = array(); $generic = array(); // Field => position in $columns of a column matched by a further alias ("Price"). foreach ( $header as $i => $cell ) { $text = trim( (string) $cell ); if ( 0 === strpos( $text, self::IGNORED_PREFIX ) ) { $ignored[] = $i; $columns[] = array( 'index' => $i, 'header' => $text, 'field' => null, 'state' => 'ignored', ); continue; } $field = FieldRegistry::resolve( $text ); if ( null !== $field && isset( $generic[ $field ] ) && FieldRegistry::isCanonical( $text, $field ) ) { // The field's own name wins over a generic alias met earlier, whatever the column order (FR-002 AC1). $pos = $generic[ $field ]; $unmapped[] = $columns[ $pos ]['index']; $columns[ $pos ]['field'] = null; $columns[ $pos ]['state'] = 'duplicate'; unset( $map[ $field ], $generic[ $field ] ); } if ( null !== $field && ! isset( $map[ $field ] ) ) { if ( ! FieldRegistry::isCanonical( $text, $field ) ) { $generic[ $field ] = count( $columns ); } $map[ $field ] = $i; $columns[] = array( 'index' => $i, 'header' => $text, 'field' => $field, 'state' => 'mapped', ); continue; } $unmapped[] = $i; $columns[] = array( 'index' => $i, 'header' => $text, 'field' => null, 'state' => null === $field ? 'unmapped' : 'duplicate', ); } sort( $unmapped ); return array( 'map' => $map, 'columns' => $columns, 'unmapped' => $unmapped, 'ignored' => $ignored, 'key_ok' => self::keyOk( $map ), ); } /** * Whether the mapping has a match key (ID or SKU, P09-FR-002 AC4). * * @param array $map Field => column index. * @return bool */ public static function keyOk( array $map ): bool { return isset( $map[ FieldRegistry::KEY_ID ] ) || isset( $map[ FieldRegistry::KEY_SKU ] ); } /** * Apply a saved template (field => header text) to a new file. * * @param string[] $header Header cells of the new file. * @param array $template Template: map (field => header text), nth (field => occurrence of that header, when not 1). * @return array {map, missing: [{field, header}], key_ok} */ public static function applyTemplate( array $header, array $template ): array { $by_key = array(); foreach ( $header as $i => $cell ) { $k = HeaderNormalizer::key( (string) $cell ); if ( '' !== $k ) { $by_key[ $k ][] = $i; } } $map = array(); $missing = array(); $nth = (array) ( $template['nth'] ?? array() ); foreach ( (array) ( $template['map'] ?? array() ) as $field => $text ) { $k = HeaderNormalizer::key( (string) $text ); $n = max( 1, (int) ( $nth[ $field ] ?? 1 ) ); // Which of the columns of that name (two "Price" columns). if ( isset( $by_key[ $k ][ $n - 1 ] ) ) { $map[ (string) $field ] = $by_key[ $k ][ $n - 1 ]; } else { $missing[] = array( 'field' => (string) $field, 'header' => (string) $text, ); } } return array( 'map' => $map, 'missing' => $missing, 'key_ok' => self::keyOk( $map ), ); } }