<?php
/**
 * Nemtly Booking Exception
 *
 * Base exception class for all plugin exceptions
 *
 * @package NemtlyBooking
 * @since   PBOOKING_SINCE
 */

namespace NemtlyBooking\Exceptions;

/**
 * Nemtly Booking Exception class
 *
 * @since PBOOKING_SINCE
 */
class Nemtly_Booking_Exception extends \Exception {

    /**
     * Error code
     *
     * @var string
     */
    protected $error_code;

    /**
     * Error data
     *
     * @var array
     */
    protected $error_data;

    /**
     * HTTP status code
     *
     * @var int
     */
    protected $http_status;

    /**
     * Constructor
     *
     * @since PBOOKING_SINCE
     *
     * @param string $message     Exception message.
     * @param string $error_code  Error code.
     * @param array  $error_data  Error data.
     * @param int    $http_status HTTP status code.
     * @param int    $code        Exception code.
     * @param \Exception $previous Previous exception.
     */
    public function __construct( $message = '', $error_code = '', $error_data = [], $http_status = 500, $code = 0, \Exception $previous = null ) {
        parent::__construct( $message, $code, $previous );

        $this->error_code  = $error_code;
        $this->error_data  = $error_data;
        $this->http_status = $http_status;
    }

    /**
     * Get error code
     *
     * @since PBOOKING_SINCE
     *
     * @return string Error code.
     */
    public function get_error_code() {
        return $this->error_code;
    }

    /**
     * Get error data
     *
     * @since PBOOKING_SINCE
     *
     * @return array Error data.
     */
    public function get_error_data() {
        return $this->error_data;
    }

    /**
     * Get HTTP status code
     *
     * @since PBOOKING_SINCE
     *
     * @return int HTTP status code.
     */
    public function get_http_status() {
        return $this->http_status;
    }

    /**
     * Convert to WP_Error
     *
     * @since PBOOKING_SINCE
     *
     * @return \WP_Error WordPress error object.
     */
    public function to_wp_error() {
        return new \WP_Error(
            $this->error_code,
            $this->getMessage(),
            array_merge(
                $this->error_data,
                [
					'status' => $this->http_status,
				]
            )
        );
    }

    /**
     * Convert to array
     *
     * @since PBOOKING_SINCE
     *
     * @return array Exception data as array.
     */
    public function to_array() {
        return [
            'error_code'  => $this->error_code,
            'message'     => $this->getMessage(),
            'data'        => $this->error_data,
            'http_status' => $this->http_status,
            'file'        => $this->getFile(),
            'line'        => $this->getLine(),
        ];
    }
}
