<?php
namespace API\BVD\Traits;

trait MatchBVD
{
    abstract public function setData(\SimpleXMLElement $data);
    
    /**
     * @param array<mixed> $post
     */
    public function matchingBVD($post) :void
    {
        $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);
        } catch (\Exception $e) {
            echo 'Matching : '.$e->getMessage();
        }
    }
}
