<?php
namespace API\BVD\Traits;

trait MatchBVD
{

    public function matchingBVD($post)
    {
        $xmlRequest = '<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://bvdep.com/webservices/">
                    <soap:Header/>
                  <soap:Body>
                    <web:Match>
                      <web:sessionHandle>'.$this->token.'</web:sessionHandle>
                      <web:criteria>
                        <web:Name>'.($post['RS']??'').'</web:Name>
                        <web:Address>'.($post['ADR']??'').'</web:Address>
                        <web:PostCode>'.($post['CPOSTAL']??'').'</web:PostCode>
                        <web:City>'.($post['VILLE']??'').'</web:City>
                        <web:Country>'.($post['PAYS']??'').'</web:Country>
                        <web:PhoneOrFax></web:PhoneOrFax>
                        <web:EMailOrWebsite></web:EMailOrWebsite>
                        <web:NationalId>'.($post['NO_SIRET']??'').'</web:NationalId>
                        <web:Ticker></web:Ticker>
                        <web:Isin></web:Isin>
                        <web:State></web:State>
                      </web:criteria>
                      <web:exclusionFlags>None</web:exclusionFlags>
                    </web:Match>
                  </soap:Body>
                </soap:Envelope>
        ';
        
        $options = [
            'body'    => $xmlRequest,
            'headers' => [
                "Content-Type" => "text/xml"
            ]
        ];

        try {
            $xml = $this->generateXML($options);
            $this->setData($xml);
            /** @phpstan-ignore-next-line */
            return $this->code;
        } catch (\Exception $e) {
            echo 'Matching : '.$e->getMessage();
        }
    }
}
