<?php
/**
 * File Path: includes/api/class-mbs-onix-parser-31.php
 *
 * ONIX 3.1 Product Parser -- My Book Showroom
 *
 * Thin subclass of MBS_Onix_Parser_30.
 *
 * ONIX 3.1 uses the same element names and short tags as 3.0 for all elements
 * retained in 3.1. Since MBS_Onix_Exporter_30 / MBS_Onix_Exporter_31 already
 * avoids all deprecated 3.0 elements, a 3.1 import file can be parsed by the
 * 3.0 parser with only a version-appropriate error message override.
 *
 * Spec: ONIX-31-Implementation-Spec-2026-05-04.md
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Api
 * @since      1.6.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Onix_Parser_31 extends MBS_Onix_Parser_30 { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Override: customize the "no records" error message for ONIX 3.1 files.
	 *
	 * @param SimpleXMLElement $xml   Parsed root element.
	 * @param array            &$stats Import stats; errors appended on failure.
	 * @return array[] Array of normalised product arrays.
	 */
	public function parse_products( SimpleXMLElement $xml, array &$stats ): array {
		$products = parent::parse_products( $xml, $stats );

		// Replace the 3.0 "no records" error string with a 3.1-specific one.
		$stats['errors'] = array_map(
			static function ( string $msg ): string {
				return str_replace(
					__( 'No valid product records found in the ONIX 3.0 file.', 'my-book-showroom' ),
					__( 'No valid product records found in the ONIX 3.1 file.', 'my-book-showroom' ),
					$msg
				);
			},
			$stats['errors']
		);

		return $products;
	}
}
