<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 2/26/2019
 * Time: 7:49 AM
 */

class WebServiceSystem
{

    protected $DEBUG_SOAP = 1;
    protected $limit = 1200;

    public function WebServiceSystem()
    {
        set_time_limit($this->limit);
        ini_set('max_execution_time', $this->limit);
        ini_set('soap.wsdl_cache_enabled', 0);
        ini_set('soap.wsdl_cache_ttl', 0);
    }

//    public function __construct()
//    {
//        set_time_limit($this->limit);
//        ini_set('max_execution_time', $this->limit);
//    }


    //region Bus Services
    protected $BUS_API_SOAP = null;

// public function BusApi($URL = "http://188.75.80.34/bus_api/BusServices.asmx?wsdl")
    public function BusApi($URL = "http://81.12.87.20:80/bus_api/BusServices.asmx?wsdl")
    {
//        $URL = "http://81.12.87.20:80/bus_api/BusServices.asmx?wsdl";
        $this->BUS_API_SOAP = new SoapClient(
            $URL, array(
                'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
                'classmap' => array('LogInInfo' => 'LogInInfo',
                    'trace' => $this->DEBUG_SOAP,
                    'debug' => $this->DEBUG_SOAP
                )
            )
        );
        return $this;
    }

    function json($function, $soap_request = array(), $pars = true)
    {
        $error = "no error";
        $data = "";
        try {
            if ($soap_request == null) {
                $soap_request = array();
            }
            $data = $this->BUS_API_SOAP->__soapCall($function, [$soap_request]);
            if (is_object($data)) {
                $data = get_object_vars($data);
            }
            if ($pars)
                $data = json_decode($data[key((array)$data)], true);
        } catch (SoapFault $exception) {
            $error = $exception->getMessage();
        }
        return $this->Output($data, $function, $error);
    }


    private function Output($data, $request, $error)
    {
        $time = time();
        $return = array(
            'id' => $time . '-' . uniqid(),
            'server-time' => date("h:i:s", $time),
            'type' => $_SERVER['REQUEST_METHOD'],
            'request' => $request,
            'error' => $error,
            'response' => $data
        );
        return ("[" . json_encode($return, JSON_UNESCAPED_UNICODE) . "]");
    }
    //endregion
}

/*$system = new WebServiceSystem();
$data = $system->BusApi('http://localhost:10254/BusServices.asmx?wsdl')->json(
    "RefNoServiceNafis",
    array(
        "User" => "romouzi",
        "Pass" => "rmz_13111363",
        "RefNo" => 711163809570,
        "GroupCode" => 3131
    )
);*/

//print_r($data);
//echo "<hr>";
//ServiceReport report = new ServiceReport();
//TransReportIn transReportIn = new TransReportIn();
//AuIn auIn = new AuIn()
//{
//UserName = "romouzi",
//Password = "rmz_13111363"
//};
//transReportIn.AuIn = auIn;
//transReportIn.GroupCode = "3131";
//transReportIn.FromDate = "1397/11/10";
//transReportIn.ToDate = "1397/11/11";
//transReportIn.MerchantCode = "71116898";
//
//return report.Report(transReportIn);


/*$soap = new SoapClient("http://nafispublic.fanavacard.com/extsrv-svcbo/RefNoService.svc?WSDL",
    array(
        'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
        'classmap' => array('LogInInfo' => 'LogInInfo',
            'trace' => true,
            'debug' => true
        )
    ));
$types = $soap->__getTypes();
foreach ($types as $type) {
    $type = preg_replace(
        array('/(\w+) ([a-zA-Z0-9]+)/', '/\n /'),
        array('<font color="green">${1}</font> <font color="blue">${2}</font>', "\n\t"),
        $type
    );
    echo $type;
    echo "\n\n";
}
echo '</pre>';*/