<?php
namespace nur;

use nur\b\CurlException;

class curl {
  private static function _generic(string $url, ?array $curlOptions=null, ?array &$headers=null): string {
    if ($curlOptions === null) $curlOptions = [];
    $curlOptions = $curlOptions + [CURLOPT_URL => $url];
    if (!isset($curlOptions[CURLOPT_RETURNTRANSFER])) {
      $curlOptions[CURLOPT_RETURNTRANSFER] = true;
    }
    $extract_headers = isset($curlOptions[CURLOPT_HEADER]) && $curlOptions[CURLOPT_HEADER];
    $ch = curl_init();
    if ($ch === false) throw new CurlException(null, "init");
    curl_setopt_array($ch, $curlOptions);
    try {
      $result = curl_exec($ch);
      if ($result === false) throw new CurlException($ch);
      if ($extract_headers) {
        $info = curl_getinfo($ch);
        $headers_size = $info["header_size"];
        $headers = substr($result, 0, $headers_size);
        $headers = explode("\r\n", $headers);
        $result = substr($result, $headers_size);
      }
      return strval($result);
    } finally {
      curl_close($ch);
    }
  }

  static final function get(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
    if ($params !== null) {
      $query = http_build_query($params);
      if (strpos($url, "?") !== false) {
        $url = "$url&$query";
      } else {
        $url = "$url?$query";
      }
    }
    return self::_generic($url, $curlOptions, $headers);
  }

  private static function _custom(string $method, string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
    if ($curlOptions === null) $curlOptions = [];
    $curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
    if ($params !== null) {
      $curlOptions[CURLOPT_POSTFIELDS] = http_build_query($params);
    }
    return self::_generic($url, $curlOptions, $headers);
  }

  static final function post(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
    return self::_custom("POST", $url, $params, $curlOptions, $headers);
  }

  static final function put(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
    return self::_custom("PUT", $url, $params, $curlOptions, $headers);
  }

  static final function delete(string $url, ?array $params=null, ?array $curlOptions=null, ?array &$headers=null): string {
    return self::_custom("DELETE", $url, $params, $curlOptions, $headers);
  }
}
