<?php
namespace SGC\eGiftCard\Http\Request;
use SGC\eGiftCard\Http\Request\Validator;

defined( 'ABSPATH' ) || exit; // Prevent direct file access.
/**
 * Class Request
 *
 * Represents an HTTP request, providing methods to access and validate request parameters.
 */
class Request {
    /**
     * Get specific request data.
     *
     * @param array $keys The keys to retrieve from the request.
     * @return array The filtered request data.
     */
    public function only(array $keys)
    {
        $data = [];
        
        foreach ($keys as $key) {
            //Nonce verification is not required here as the request is made using rest api and nonce already verified there
            if (isset($_REQUEST[$key])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
                $data[$key] = sanitize_text_field( wp_unslash( $_REQUEST[$key] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
            }
        }
        return $data;
    }

    /**
     * Retrieve a specific request parameter by its key.
     *
     * @param string $key The key of the request parameter.
     * @param mixed  $default The default value to return if the key is not found.
     *
     * @return mixed The value of the request parameter or the default value if not found.
     */
    public function get( $key, $default = null ) {
        //Nonce verification is not required here as the request is made using rest api and nonce already verified there
        return isset( $_REQUEST[ $key ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
    }

    /**
     * Magic method to retrieve a specific request parameter by its key.
     *
     * @param string $key The key of the request parameter.
     *
     * @return mixed The value of the request parameter or null if not found.
     */
    public function __get( $key ) {
        return $this->get( $key );
    }

    /**
     * Retrieve a specific request parameter safely by its key.
     *
     * @param string $key The key of the request parameter.
     *
     * @return mixed|null The sanitized value of the request parameter or null if not found.
     */
    public function getSafe( $key ) {
        //Nonce verification is not required here as the request is made using rest api and nonce already verified there
        return isset( $_REQUEST[ $key ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
    }

    /**
     * Validate request data using specified rules and custom error messages.
     *
     * @param array $data The data to validate.
     * @param array $rules The validation rules.
     * @param array $messages Custom error messages.
     *
     * @return array An array of validation results.
     */
    public function validate( array $data, array $rules, array $messages ) {
        $validator = new Validator( $data, $rules, $messages );

        return $validator->validate();
    }
}
