<?php

namespace App\Http\Traits;

trait FinancialTrait
{
    /**
     * 환율정보 처리
     * User: 김국현
     * Date: 2019-02-04
     * Time: 오후 11:22
     *
     * https://www.data.go.kr/dataset/3068846/openapi.do
     * https://www.koreaexim.go.kr/site/program/openapi/openApiView?menuid=001003002002001&apino=2&viewtype=C
     */

    /*
     * 모든 환율목록 조회
     */
    function exchangeList($searchDate = "")
    {
        if(!$searchDate) {
            date('Y-m-d', time());
        }
        $key = KOREA_OPEN_API_KEY;
        $url = "site/program/financial/exchangeJSON?authkey={$key}&searchdate={$searchDate}&data=AP01";
        $result = json_decode(file_get_contents(KOREA_OPEN_API_HOST . $url), true);

        return $result;
    }

    /*
     * 유로화 조회
     */
    function getEuRate()
    {
        $exchangeList = $this->exchangeList();
        $result = [];
        foreach ($exchangeList as $exchange) {
            if ($exchange['cur_unit'] == "EUR") {
                $result = $exchange;
                break;
            }
        }
        return $result;
    }
}
