<?php
/**
 * Link model.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Shift4_APM\Models;

use Flo_Checkout\Gateways\Shift4_APM\Enums\Request_Method;
use JsonSerializable;

defined( 'ABSPATH' ) || exit;

/**
 * Class Link_Model
 */
class Link_Model implements JsonSerializable {

	/**
	 * The href.
	 *
	 * @var string
	 */
	public string $href;

	/**
	 * The rel.
	 *
	 * @var string
	 */
	public string $rel;

	/**
	 * The method.
	 *
	 * @var string
	 */
	public string $method;

	/**
	 * Constructor.
	 *
	 * @param array $link_data The link data.
	 */
	public function __construct( array $link_data ) {
		$this->href   = $link_data['href'];
		$this->rel    = $link_data['rel'];
		$this->method = $link_data['method'];
	}

	/**
	 * Get request method.
	 *
	 * @return Request_Method|null
	 */
	public function get_request_method(): ?Request_Method {
		return Request_Method::tryFrom( strtoupper( $this->method ) );
	}

	/**
	 * {@inheritDoc}
	 */
	public function jsonSerialize(): array {
		return array(
			'href'   => $this->href,
			'rel'    => $this->rel,
			'method' => $this->method,
		);
	}
}
