<commit_before><?php

namespace Fadion\Fixerio;

use Fadion\Fixerio\Exceptions\ResponseException;
use Fadion\Fixerio\Exceptions\ConnectionException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\TransferException;

class Exchange
{
    /**
     * Guzzle client
     * @var GuzzleHttp\Client
     */
    private $guzzle;

    /**
     * URL of fixer.io
     * @var string
     */
    private $url = "api.fixer.io";

    /**
     * Date when an historical call is made
     * @var string
     */
    private $date;

    /**
     * Http or Https
     * @var string
     */
    private $protocol = 'http';

    /**
     * Base currency
     * @var string
     */
    private $base = 'EUR';

    /**
     * List of currencies to return
     * @var array
     */
    private $symbols = [];

    /**
     * Holds whether the response should be
     * an object or not
     * @var array
     */
    private $asObject = false;

    /**
     * @param $guzzle Guzzle client
     */
    public function __construct($guzzle = null)
    {
        if (isset($guzzle)) {
            $this->guzzle = $guzzle;
        }
        else {
            $this->guzzle = new GuzzleClient();
        }
    }

    /**
     * Sets the protocol to https
     * 
     * @return Exchange
     */
    public function secure()
    {
        $this->protocol = 'https';

        return $this;
    }

    /**
     * Sets the base currency
     * 
     * @param  string $currency
     * @return Exchange
     */
    public function base($currency)
    {
        $this->base = $currency;

        return $this;
    }

    /**
     * Sets the currencies to return.
     * Expects either a list of arguments or
     * a single argument as array
     * 
     * @param  array $currencies
     * @return Exchange
     */
    public function symbols($currencies = null)
    {
        if (func_num_args() and !is_array(func_get_args()[0])) {
            $currencies = func_get_args();
        }

        $this->symbols = $currencies;

        return $this;
    }

    /**
     * Defines that the api call should be
     * historical, meaning it will return rates
     * for any day since the selected date
     * 
     * @param  string $date
     * @return Exchange
     */
    public function historical($date)
    {
        $this->date = date('Y-m-d', strtotime($date));

        return $this;
    }

    /**
     * Sets the returned response to be an object,
     * as opposed to an array
     * 
     * @return Exchange
     */
    public function asObject()
    {
        $this->asObject = true;

        return $this;
    }

    /**
     * Returns the correctly formatted url
     * 
     * @return string
     */
    public function getUrl()
    {
        return $this->buildUrl($this->url);
    }

    /**
     * Makes the request and returns the response
     * with the rates.
     * 
     * @throws ConnectionException if the request is incorrect or times out
     * @throws ResponseException if the response is malformed
     * @return array
     */
    public function get()
    {
        $url = $this->buildUrl($this->url);

        try {
            $response = $this->makeRequest($url);

            return $this->prepareResponse($response);
        }
        // The client needs to know only one exception, no
        // matter what exception is thrown by Guzzle
        catch (TransferException $e) {
            throw new ConnectionException($e->getMessage());
        }
    }

    /**
     * Forms the correct url from the different parts
     * <commit_msg>Simplify getting the response as object<commit_after><?php

namespace Fadion\Fixerio;

use Fadion\Fixerio\Exceptions\ResponseException;
use Fadion\Fixerio\Exceptions\ConnectionException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\TransferException;

class Exchange
{
    /**
     * Guzzle client
     * @var GuzzleHttp\Client
     */
    private $guzzle;

    /**
     * URL of fixer.io
     * @var string
     */
    private $url = "api.fixer.io";

    /**
     * Date when an historical call is made
     * @var string
     */
    private $date;

    /**
     * Http or Https
     * @var string
     */
    private $protocol = 'http';

    /**
     * Base currency
     * @var string
     */
    private $base = 'EUR';

    /**
     * List of currencies to return
     * @var array
     */
    private $symbols = [];

    /**
     * Holds whether the response should be
     * an object or not
     * @var array
     */
    private $asObject = false;

    /**
     * @param $guzzle Guzzle client
     */
    public function __construct($guzzle = null)
    {
        if (isset($guzzle)) {
            $this->guzzle = $guzzle;
        }
        else {
            $this->guzzle = new GuzzleClient();
        }
    }

    /**
     * Sets the protocol to https
     * 
     * @return Exchange
     */
    public function secure()
    {
        $this->protocol = 'https';

        return $this;
    }

    /**
     * Sets the base currency
     * 
     * @param  string $currency
     * @return Exchange
     */
    public function base($currency)
    {
        $this->base = $currency;

        return $this;
    }

    /**
     * Sets the currencies to return.
     * Expects either a list of arguments or
     * a single argument as array
     * 
     * @param  array $currencies
     * @return Exchange
     */
    public function symbols($currencies = null)
    {
        if (func_num_args() and !is_array(func_get_args()[0])) {
            $currencies = func_get_args();
        }

        $this->symbols = $currencies;

        return $this;
    }

    /**
     * Defines that the api call should be
     * historical, meaning it will return rates
     * for any day since the selected date
     * 
     * @param  string $date
     * @return Exchange
     */
    public function historical($date)
    {
        $this->date = date('Y-m-d', strtotime($date));

        return $this;
    }

    /**
     * Returns the correctly formatted url
     * 
     * @return string
     */
    public function getUrl()
    {
        return $this->buildUrl($this->url);
    }

    /**
     * Makes the request and returns the response
     * with the rates.
     * 
     * @throws ConnectionException if the request is incorrect or times out
     * @throws ResponseException if the response is malformed
     * @return array
     */
    public function get()
    {
        $url = $this->buildUrl($this->url);

        try {
            $response = $this->makeRequest($url);

            return $this->prepareResponse($response);
        }
        // The client needs to know only one exception, no
        // matter what exception is thrown by Guzzle
        catch (TransferException $e) {
            throw new ConnectionException($e->getMessage());
        }
    }

    /**
     * Alias of get() but returns an object
     * response.
     * 
     * @throws ConnectionException if the request is incorrect or times out
     * @throws ResponseException if the response is malformed
     * @return object
     */
    public function getAsObject()
    {
        $this->asObject = true;

        return $this->get();
    }

    /**
     * Forms the correct url from the different parts
     * 
{'fraction_non_alphanumeric': 0.10921125206839492, 'fraction_numerical': 0.0002757859900717044, 'mean_word_length': 1.7452687358062073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '182836', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2054, 'n_words': 611}