<?php
/**
 * Validator for ProductsFormHandler.
 *
 * @package Ilabs\Firevert\Modules\SpectatorViews\Services\Form\Validator
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\SpectatorViews\Services\Form\Validator;

use Ilabs\Firevert\Core\Form\Validation\FormValidator;

/**
 * Class ProductsFormValidator
 *
 * Validates form fields defined in ProductsFormHandler.
 */
class ProductsFormValidator extends FormValidator {

	/**
	 * ProductsFormValidator constructor.
	 *
	 * @param array<string, string> $field_types Field definitions passed from FormHandler.
	 */
	public function __construct( array $field_types ) {
		$this->set_field_types( $field_types );
		$this->set_custom_rules(
			array(
				'firevert_spectator_views_selected_products' => array(
					function ( $value ) {
						if ( ! empty( $value ) && ! is_array( $value ) ) {
							return __( 'Selected products must be an array.', 'firevert' );
						}
						return null;
					},
				),

				'firevert_spectator_views_product_categories' => array(
					function ( $value ) {
						if ( ! empty( $value ) && ! is_array( $value ) ) {
							return __( 'Product categories must be an array.', 'firevert' );
						}
						return null;
					},
				),

				'firevert_spectator_views_excluded_products' => array(
					function ( $value ) {
						if ( ! empty( $value ) && ! is_array( $value ) ) {
							return __( 'Excluded products must be an array.', 'firevert' );
						}
						return null;
					},
				),

				'firevert_spectator_views_excluded_categories' => array(
					function ( $value ) {
						if ( ! empty( $value ) && ! is_array( $value ) ) {
							return __( 'Excluded categories must be an array.', 'firevert' );
						}
						return null;
					},
				),
			)
		);
	}
}
