<commit_before><?php
namespace TijsVerkoyen\Bpost;

use Psr\Log\LoggerInterface;
use TijsVerkoyen\Bpost\Bpost\Label;
use TijsVerkoyen\Bpost\Bpost\Order;
use TijsVerkoyen\Bpost\Bpost\Order\Box;
use TijsVerkoyen\Bpost\Bpost\Order\Box\Option\Insurance;
use TijsVerkoyen\Bpost\Bpost\ProductConfiguration;
use TijsVerkoyen\Bpost\Exception\ApiResponseException\BpostCurlException;
use TijsVerkoyen\Bpost\Exception\ApiResponseException\BpostInvalidResponseException;
use TijsVerkoyen\Bpost\Exception\ApiResponseException\BpostInvalidSelectionException;
use TijsVerkoyen\Bpost\Exception\LogicException\BpostInvalidValueException;

/**
 * Bpost class
 *
 * @author    <NAME> <<EMAIL>>
 * @version   3.0.0
 * @copyright Copyright (c), <NAME>. All rights reserved.
 * @license   BSD License
 */
class Bpost
{

    const LABEL_FORMAT_A4 = 'A4';
    const LABEL_FORMAT_A6 = 'A6';

    // URL for the api
    const API_URL = 'https://api.bpost.be/services/shm';

    // current version
    const VERSION = '3.0.2';

    /** Min weight, in grams, for a shipping */
    const MIN_WEIGHT = 0;

    /** Max weight, in grams, for a shipping */
    const MAX_WEIGHT = 30000;

    /**
     * The account id
     *
     * @var string
     */
    private $accountId;

    /**
     * A cURL instance
     *
     * @var resource
     */
    private $curl;

    /**
     * The passPhrase
     *
     * @var string
     */
    private $passPhrase;

    /**
     * The port to use.
     *
     * @var int
     */
    private $port;

    /**
     * The timeout
     *
     * @var int
     */
    private $timeOut = 30;

    /**
     * The user agent
     *
     * @var string
     */
    private $userAgent;

    private $apiUrl;

    /** @var  Logger */
    private $logger;

    // class methods
    /**
     * Create Bpost instance
     *
     * @param string $accountId
     * @param string $passPhrase
     * @param string $apiUrl
     */
    public function __construct($accountId, $passPhrase, $apiUrl = self::API_URL)
    {
        $this->accountId = (string)$accountId;
        $this->passPhrase = (string)$passPhrase;
        $this->apiUrl = (string)$apiUrl;
        $this->logger = new Logger();
    }

    /**
     * Destructor
     */
    public function __destruct()
    {
        if ($this->curl !== null) {
            curl_close($this->curl);
            $this->curl = null;
        }
    }

    /**
     * Decode the response
     *
     * @param  \SimpleXMLElement $item The item to decode.
     * @param  array $return Just a placeholder.
     * @param  int $i A internal counter.
     * @return array
     * @throws BpostException
     */
    private static function decodeResponse($item, $return = null, $i = 0)
    {
        $arrayKeys = array('barcode', 'orderLine', Insurance::INSURANCE_TYPE_ADDITIONAL_INSURANCE, Box\Option\Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, 'infoPugo');
        $integerKeys = array('totalPrice');

        if ($item instanceof \SimpleXMLElement) {
            /** @var \SimpleXMLElement $value */
            foreach ($item as $key => $value) {
                $attributes = (array)$value->attributes();

                if (!empty($attributes) && isset($attributes['@attributes'])) {
                    $return[$key]['@attributes'] = $attributes['@attributes'];
                }

                // empty
                if (isset($value['nil']) && (string)$value['nil'] === 'true') {
                    $return[$key] = null;
                } // empty
                elseif (isset($value[0]) && (string)$value == '') {
                    if (in_array($key, $arrayKeys)) {
                        $return[$key][] = self::decodeResponse($value);
                    } else {
                        $return[$key] = self::decodeResponse($value, null, 1);
                    }
                } else {
                    // arrays
                    if (in_array($key, $arrayKeys)) {
                        $return[$key][] = (string)$value;
                    } // booleans
                    elseif ((string)$value == 'true') {
                        $return[$key] = true;
                    } elseif ((string)$value == 'false') {
                        $return[$key] = false;
                    } // integers
                    elseif (in_array($key, $integerKeys)) {
                        $return[$key] = (int)$value;
                    } // fallback to string
                    else {
                        $return[$key] = (string)$value;
                    }
                }
            }
        } else {
            throw new BpostException('Invalid item.');
        }

        return $return;
    }

    /**
     * Make the call
     *
     * @param  string $url The URL to call.
     * @param  string $body The data to pass.
     * @param  array $headers The headers to pass.<commit_msg>feat: refactor: Move throw Exception of the begin of method<commit_after><?php
namespace TijsVerkoyen\Bpost;

use Psr\Log\LoggerInterface;
use TijsVerkoyen\Bpost\Bpost\Label;
use TijsVerkoyen\Bpost\Bpost\Order;
use TijsVerkoyen\Bpost\Bpost\Order\Box;
use TijsVerkoyen\Bpost\Bpost\Order\Box\Option\Insurance;
use TijsVerkoyen\Bpost\Bpost\ProductConfiguration;
use TijsVerkoyen\Bpost\Exception\ApiResponseException\BpostCurlException;
use TijsVerkoyen\Bpost\Exception\ApiResponseException\BpostInvalidResponseException;
use TijsVerkoyen\Bpost\Exception\ApiResponseException\BpostInvalidSelectionException;
use TijsVerkoyen\Bpost\Exception\LogicException\BpostInvalidValueException;

/**
 * Bpost class
 *
 * @author    <NAME> <<EMAIL>>
 * @version   3.0.0
 * @copyright Copyright (c), <NAME>. All rights reserved.
 * @license   BSD License
 */
class Bpost
{

    const LABEL_FORMAT_A4 = 'A4';
    const LABEL_FORMAT_A6 = 'A6';

    // URL for the api
    const API_URL = 'https://api.bpost.be/services/shm';

    // current version
    const VERSION = '3.0.2';

    /** Min weight, in grams, for a shipping */
    const MIN_WEIGHT = 0;

    /** Max weight, in grams, for a shipping */
    const MAX_WEIGHT = 30000;

    /**
     * The account id
     *
     * @var string
     */
    private $accountId;

    /**
     * A cURL instance
     *
     * @var resource
     */
    private $curl;

    /**
     * The passPhrase
     *
     * @var string
     */
    private $passPhrase;

    /**
     * The port to use.
     *
     * @var int
     */
    private $port;

    /**
     * The timeout
     *
     * @var int
     */
    private $timeOut = 30;

    /**
     * The user agent
     *
     * @var string
     */
    private $userAgent;

    private $apiUrl;

    /** @var  Logger */
    private $logger;

    // class methods
    /**
     * Create Bpost instance
     *
     * @param string $accountId
     * @param string $passPhrase
     * @param string $apiUrl
     */
    public function __construct($accountId, $passPhrase, $apiUrl = self::API_URL)
    {
        $this->accountId = (string)$accountId;
        $this->passPhrase = (string)$passPhrase;
        $this->apiUrl = (string)$apiUrl;
        $this->logger = new Logger();
    }

    /**
     * Destructor
     */
    public function __destruct()
    {
        if ($this->curl !== null) {
            curl_close($this->curl);
            $this->curl = null;
        }
    }

    /**
     * Decode the response
     *
     * @param  \SimpleXMLElement $item The item to decode.
     * @param  array $return Just a placeholder.
     * @param  int $i A internal counter.
     * @return array
     * @throws BpostException
     */
    private static function decodeResponse($item, $return = null, $i = 0)
    {
        if (!$item instanceof \SimpleXMLElement) {
            throw new BpostException('Invalid item.');
        }

        $arrayKeys = array(
            'barcode',
            'orderLine',
            Insurance::INSURANCE_TYPE_ADDITIONAL_INSURANCE,
            Box\Option\Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
            'infoPugo'
        );
        $integerKeys = array('totalPrice');

        /** @var \SimpleXMLElement $value */
        foreach ($item as $key => $value) {
            $attributes = (array) $value->attributes();

            if (!empty($attributes) && isset($attributes['@attributes'])) {
                $return[$key]['@attributes'] = $attributes['@attributes'];
            }

            // empty
            if (isset($value['nil']) && (string) $value['nil'] === 'true') {
                $return[$key] = null;
            } // empty
            elseif (isset($value[0]) && (string) $value == '') {
                if (in_array($key, $arrayKeys)) {
                    $return[$key][] = self::decodeResponse($value);
                } else {
                    $return[$key] = self::decodeResponse($value, null, 1);
                }
            } else {
                // arrays
                if (in_array($key, $arrayKeys)) {
                    $return[$key][] = (string) $value;
                } // booleans
                elseif ((string) $value == 'true') {
                    $return[$key] = true;
                } elseif ((string) $value == 'false') {
                    $return[$key] = false;
                } // integers
                elseif (in_array($key, $integerKeys)) {
                    $return[$key] = (int) $value;
                } // fallback to string
                else {
                    $return[$key] = (string) $value;
                }
            }
        }

        return $return;
    }

    /**
     * Make the call
     *
     * @param  string $url The URL to call.
     * @param  string $body The data to pass.
     * @param  array $headers The headers to pass.
{'fraction_non_alphanumeric': 0.1331884507916796, 'fraction_numerical': 0.004346476249611922, 'mean_word_length': 1.7501422879908937, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1916913', 'n_tokens_mistral': 3005, 'n_tokens_neox': 2874, 'n_words': 636}