<commit_before><?php

declare(strict_types=1);

/**
 * This file is part of PHP Nominatim.
 * (c) <NAME> <<EMAIL>>
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace maxh\Nominatim;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use maxh\Nominatim\Exceptions\NominatimException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use SimpleXMLElement;

/**
 *  Wrapper to manage exchanges with OSM Nominatim API.
 *
 * @see http://wiki.openstreetmap.org/wiki/Nominatim
 */
class Nominatim
{
    /**
     * Contain url of the current application.
     *
     * @var string
     */
    private $application_url;

    /**
     * Contain default request headers.
     *
     * @var array
     */
    private $defaultHeaders;

    /**
     * Contain http client connection.
     *
     * @var \GuzzleHttp\Client
     */
    private $http_client;

    /**
     * The search object which serves as a template for new ones created
     * by 'newSearch()' method.
     *
     * @var \maxh\Nominatim\Search
     */
    private $baseSearch;

    /**
     * Template for new ones created by 'newReverser()' method.
     *
     * @var \maxh\Nominatim\Reverse
     */
    private $baseReverse;

    /**
     * Template for new ones created by 'newLookup()' method.
     *
     * @var \maxh\Nominatim\Lookup
     */
    private $baseLookup;

    /**
     * Template for new ones created by 'newDetails()' method.
     *
     * @var \maxh\Nominatim\Lookup
     */
    private $baseDetails;

    /**
     * Constructor.
     *
     * @param string                  $application_url Contain url of the current application
     * @param null|\GuzzleHttp\Client $http_client     Client object from Guzzle
     * @param array                   $defaultHeaders  Define default header for all request
     *
     * @throws NominatimException
     */
    public function __construct(
        string $application_url,
        array $defaultHeaders = [],
        ?Client $http_client = null
    ) {
        if (empty($application_url)) {
            throw new NominatimException('Application url parameter is empty');
        }

        if (null === $http_client) {
            $http_client = new Client([
                'base_uri' => $application_url,
                'timeout' => 30,
                'connection_timeout' => 5,
            ]);
        } elseif ($http_client instanceof Client) {
            $application_url_client = $http_client->getConfig('base_uri');

            if (empty($application_url_client)) {
                throw new NominatimException('http_client must have a configured base_uri.');
            }

            if ($application_url_client !== $application_url) {
                throw new NominatimException('http_client parameter hasn\'t the same url application.');
            }
        } else {<commit_msg>Fix check for baseUri when using custom guzzle client<commit_after><?php

declare(strict_types=1);

/**
 * This file is part of PHP Nominatim.
 * (c) <NAME> <<EMAIL>>
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace maxh\Nominatim;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use maxh\Nominatim\Exceptions\NominatimException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use SimpleXMLElement;

/**
 *  Wrapper to manage exchanges with OSM Nominatim API.
 *
 * @see http://wiki.openstreetmap.org/wiki/Nominatim
 */
class Nominatim
{
    /**
     * Contain url of the current application.
     *
     * @var string
     */
    private $application_url;

    /**
     * Contain default request headers.
     *
     * @var array
     */
    private $defaultHeaders;

    /**
     * Contain http client connection.
     *
     * @var \GuzzleHttp\Client
     */
    private $http_client;

    /**
     * The search object which serves as a template for new ones created
     * by 'newSearch()' method.
     *
     * @var \maxh\Nominatim\Search
     */
    private $baseSearch;

    /**
     * Template for new ones created by 'newReverser()' method.
     *
     * @var \maxh\Nominatim\Reverse
     */
    private $baseReverse;

    /**
     * Template for new ones created by 'newLookup()' method.
     *
     * @var \maxh\Nominatim\Lookup
     */
    private $baseLookup;

    /**
     * Template for new ones created by 'newDetails()' method.
     *
     * @var \maxh\Nominatim\Lookup
     */
    private $baseDetails;

    /**
     * Constructor.
     *
     * @param string                  $application_url Contain url of the current application
     * @param null|\GuzzleHttp\Client $http_client     Client object from Guzzle
     * @param array                   $defaultHeaders  Define default header for all request
     *
     * @throws NominatimException
     */
    public function __construct(
        string $application_url,
        array $defaultHeaders = [],
        ?Client $http_client = null
    ) {
        if (empty($application_url)) {
            throw new NominatimException('Application url parameter is empty');
        }

        if (null === $http_client) {
            $http_client = new Client([
                'base_uri' => $application_url,
                'timeout' => 30,
                'connection_timeout' => 5,
            ]);
        } elseif ($http_client instanceof Client) {
            $application_url_client = (string)$http_client->getConfig('base_uri');

            if (empty($application_url_client)) {
                throw new NominatimException('http_client must have a configured base_uri.');
            }

            if ($application_url_client !== $application_url) {
                throw new NominatimException('http_client parameter hasn\'t the same url application.');
            }
        } else {
{'fraction_non_alphanumeric': 0.10357751277683135, 'fraction_numerical': 0.0017035775127768314, 'mean_word_length': 2.134543513080619, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '761181', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1678, 'n_words': 462}