<commit_before><?php

namespace Ups\Tests;

use Psr\Log\LoggerInterface;
use SimpleXMLElement;
use Ups\RequestInterface;
use Ups\ResponseInterface;

class RequestMock implements RequestInterface
{
    const RESPONSE_DIRECTORY = '/../_files/responses';
    const REQUEST_DIRECTORY = '/../_files/requests';

    /**
     * @var string
     */
    protected $access;

    /**
     * @var string
     */
    protected $request;

    /**
     * @var string
     */
    protected $endpointUrl;

    /**
     * @var string
     */
    private $responsePath;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param string|null $responsePath
     */
    public function __construct(LoggerInterface $logger = null, $responsePath = null)
    {
        $this->logger = $logger;
        $this->responsePath = $responsePath;
    }

    /**
     * @param string $request
     *
     * @return SimpleXMLElement
     */
    public function getExpectedRequestXml($request)
    {
        $args = func_get_args();
        if (isset($args[1]) && is_array($args[1])) {
            $args = $args[1];
        } else {
            $args = null;
        }

        $request = realpath(__DIR__.self::REQUEST_DIRECTORY.$request);
        if ($request && is_file($request)) {
            $request = file_get_contents($request);
            if (isset($args)) {
                $request = call_user_func_array('sprintf', array_merge([$request], $args));
            }

            return new SimpleXMLElement($request);
        }

        return;
    }

    /**
     * @return SimpleXMLElement
     */
    public function getRequestXml()
    {
        return new SimpleXMLElement($this->getRequest());
    }

    /**
     * @param string|null $access      The access request xml
     * @param string|null $request     The request xml
     * @param string|null $endpointUrl The UPS API Endpoint URL
     *
     * @return ResponseInterface
     */
    public function request($access = null, $request = null, $endpointUrl = null)
    {
        if (null !== $access) {
            $this->setAccess($access);
        }
        if (null !== $request) {
            $this->setRequest($request);
        }
        if (null !== $endpointUrl) {
            $this->setEndpointUrl($endpointUrl);
        }

        $response = realpath(__DIR__.self::RESPONSE_DIRECTORY.$this->responsePath);
        if ($response && is_file($response)) {
            $response = file_get_contents($response);
            if (!empty($response)) {
                $response = mb_convert_encoding($response, 'UTF-8', mb_detect_encoding($response));
                $response = new SimpleXMLElement($response);<commit_msg>Only use mb_encoding when function exists<commit_after><?php

namespace Ups\Tests;

use Psr\Log\LoggerInterface;
use SimpleXMLElement;
use Ups\RequestInterface;
use Ups\ResponseInterface;

class RequestMock implements RequestInterface
{
    const RESPONSE_DIRECTORY = '/../_files/responses';
    const REQUEST_DIRECTORY = '/../_files/requests';

    /**
     * @var string
     */
    protected $access;

    /**
     * @var string
     */
    protected $request;

    /**
     * @var string
     */
    protected $endpointUrl;

    /**
     * @var string
     */
    private $responsePath;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param string|null $responsePath
     */
    public function __construct(LoggerInterface $logger = null, $responsePath = null)
    {
        $this->logger = $logger;
        $this->responsePath = $responsePath;
    }

    /**
     * @param string $request
     *
     * @return SimpleXMLElement
     */
    public function getExpectedRequestXml($request)
    {
        $args = func_get_args();
        if (isset($args[1]) && is_array($args[1])) {
            $args = $args[1];
        } else {
            $args = null;
        }

        $request = realpath(__DIR__.self::REQUEST_DIRECTORY.$request);
        if ($request && is_file($request)) {
            $request = file_get_contents($request);
            if (isset($args)) {
                $request = call_user_func_array('sprintf', array_merge([$request], $args));
            }

            return new SimpleXMLElement($request);
        }

        return;
    }

    /**
     * @return SimpleXMLElement
     */
    public function getRequestXml()
    {
        return new SimpleXMLElement($this->getRequest());
    }

    /**
     * @param string|null $access      The access request xml
     * @param string|null $request     The request xml
     * @param string|null $endpointUrl The UPS API Endpoint URL
     *
     * @return ResponseInterface
     */
    public function request($access = null, $request = null, $endpointUrl = null)
    {
        if (null !== $access) {
            $this->setAccess($access);
        }
        if (null !== $request) {
            $this->setRequest($request);
        }
        if (null !== $endpointUrl) {
            $this->setEndpointUrl($endpointUrl);
        }

        $response = realpath(__DIR__.self::RESPONSE_DIRECTORY.$this->responsePath);
        if ($response && is_file($response)) {
            $response = file_get_contents($response);
            if (!empty($response)) {
                if (function_exists('mb_convert_encoding')) {
                    $response = mb_convert_encoding($response, 'UTF-8', mb_detect_encoding($response));
                }
                $response = new SimpleXMLElement($response);
{'fraction_non_alphanumeric': 0.132, 'fraction_numerical': 0.0014545454545454545, 'mean_word_length': 2.0869809203142538, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7577425', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1535, 'n_words': 320}