<commit_before><?php

/**
 * This file is part of the Geocoder package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

namespace Geocoder\Provider;

use Geocoder\HttpAdapter\HttpAdapterInterface;
use Geocoder\Provider\ProviderInterface;
use DOMDocument;

/**
 * @author <NAME> <<EMAIL>>
 */
class OpenStreetMapsProvider extends AbstractProvider implements ProviderInterface
{
    /**
     * @var string
     */
    const GEOCODE_ENDPOINT_URL = 'http://nominatim.openstreetmap.org/search?q=%s&format=xml&addressdetails=1';

    /**
     * @var string
     */
    const REVERSE_ENDPOINT_URL = 'http://nominatim.openstreetmap.org/reverse?format=xml&lat=%F&lon=%F&addressdetails=1';

    /**
     * {@inheritDoc}
     */
    public function getGeocodedData($address)
    {
        if ('127.0.0.1' === $address) {
            return $this->getLocalhostDefaults();
        }

        $query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address));

        $content = $this->executeQuery($query);

        if (null === $content) {
            return $this->getDefaults();
        }

        $doc = new DOMDocument();
        if (@$doc->loadXML($content)) {
            $maxRank      = 0;
            $bestNode     = null;
            $searchResult = $doc->getElementsByTagName('searchresults')->item(0);

            if (null === $searchResult) {
                return $this->getDefaults();
            }

            foreach ($searchResult->getElementsByTagName('place') as $node) {
                if ($node->getAttribute('place_rank') > $maxRank) {
                    $maxRank  = $node->getAttribute('place_rank');
                    $bestNode = $node;
                }
            }

            $bounds = null;
            $boundsAttr = $bestNode->getAttribute('boundingbox');
            if ($boundsAttr) {
                $bounds = array();
                list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr);
            }

            $ret = $this->getDefaults();
            $ret['latitude']  = $bestNode->getAttribute('lat');
            $ret['longitude'] = $bestNode->getAttribute('lon');
            $ret['bounds']    = $bounds;
            $ret['zipcode']   = $bestNode->getElementsByTagName('postcode')->item(0)->nodeValue;
            $ret['county']    = $bestNode->getElementsByTagName('county')->length ? $bestNode->getElementsByTagName('county')->item(0)->nodeValue : null;
            $ret['region']    = $bestNode->getElementsByTagName('state')->length ? $bestNode->getElementsByTagName('state')->item(0)->nodeValue : null;
            $ret['streetNumber'] = $bestNode->getElementsByTagName('house_number')->length ? $bestNode->getElementsByTagName('house_number')->item(0)->nodeValue : null;
            $ret['streetName']   = $bestNode->getElementsByTagName('road')->length ? $bestNode->getElementsByTagName('road')->item(0)->nodeValue : null;
            $ret['city']      = $bestNode->getElementsByTagName('city')->item(0)->nodeValue;
            $ret['country']   = $bestNode->getElementsByTagName('country')->item(0)->nodeValue;
            $ret['countryCode']  = $bestNode->getElementsByTagName('country_code')->item(0)->nodeValue;

            return $ret;
        } else {
            return $this->getDefaults();
        }
    }

    /**
     * {@inheritDoc}
     */
    public function getReversedData(array $coordinates)
    {
        $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1]);

        $content = $this->executeQuery($query);

        if (null === $content) {
            return $this->getDefaults();
        }

        $doc = new DOMDocument();
        if (@$doc->loadXML($content)) {
            $maxRank          = 0;
            $bestNode         = null;
            $searchResult     = $doc->getElementsByTagName('reversegeocode')->item(0);
            $addressParts     = $searchResult->getElementsByTagName('addressparts')->item(0);
            $result           = $searchResult->getElementsByTagName('result')->item(0);
            $ret              = $this->getDefaults();
            $ret['latitude']  = $result->getAttribute('lat');
            $ret['longitude'] = $result->getAttribute('lon');
            $ret['zipcode']   = $addressParts->getElementsByTagName('postcode')->item(0)->nodeValue;
            $ret['county']    = $addressParts->getElementsByTagName('county')->length ? $addressParts->getElementsByTagName('county')->item(0)->nodeValue : null;
            $ret['region']    = $addressParts->getElementsByTagName('state')->length ? $addressParts->getElementsByTagName('state')->item(0)->nodeValue : null;
            $ret['streetNumber'] = $addressParts->getElementsByTagName('house_number')->length ? $addressParts->getElementsByTagName('house_number')->item(0)->nodeValue : null;
            $ret['streetName']   = $addressParts->getElementsByTagName('road')->length ? $addressParts->getElementsByTagName('road')->item(0)->nodeValue : null;
            $ret['city']      = $addressParts->getElementsByTagName('city')->item(0)->nodeValue;
            $ret['country']   = $addressParts->getElementsByTagName('country')->item(0)->nodeValue;<commit_msg>[Provider] [OpenStreetMaps] Fixed countryCode result.<commit_after><?php

/**
 * This file is part of the Geocoder package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

namespace Geocoder\Provider;

use Geocoder\HttpAdapter\HttpAdapterInterface;
use Geocoder\Provider\ProviderInterface;
use DOMDocument;

/**
 * @author <NAME> <<EMAIL>>
 */
class OpenStreetMapsProvider extends AbstractProvider implements ProviderInterface
{
    /**
     * @var string
     */
    const GEOCODE_ENDPOINT_URL = 'http://nominatim.openstreetmap.org/search?q=%s&format=xml&addressdetails=1';

    /**
     * @var string
     */
    const REVERSE_ENDPOINT_URL = 'http://nominatim.openstreetmap.org/reverse?format=xml&lat=%F&lon=%F&addressdetails=1';

    /**
     * {@inheritDoc}
     */
    public function getGeocodedData($address)
    {
        if ('127.0.0.1' === $address) {
            return $this->getLocalhostDefaults();
        }

        $query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address));

        $content = $this->executeQuery($query);

        if (null === $content) {
            return $this->getDefaults();
        }

        $doc = new DOMDocument();
        if (@$doc->loadXML($content)) {
            $maxRank      = 0;
            $bestNode     = null;
            $searchResult = $doc->getElementsByTagName('searchresults')->item(0);

            if (null === $searchResult) {
                return $this->getDefaults();
            }

            foreach ($searchResult->getElementsByTagName('place') as $node) {
                if ($node->getAttribute('place_rank') > $maxRank) {
                    $maxRank  = $node->getAttribute('place_rank');
                    $bestNode = $node;
                }
            }

            $bounds = null;
            $boundsAttr = $bestNode->getAttribute('boundingbox');
            if ($boundsAttr) {
                $bounds = array();
                list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = explode(',', $boundsAttr);
            }

            $ret = $this->getDefaults();
            $ret['latitude']  = $bestNode->getAttribute('lat');
            $ret['longitude'] = $bestNode->getAttribute('lon');
            $ret['bounds']    = $bounds;
            $ret['zipcode']   = $bestNode->getElementsByTagName('postcode')->item(0)->nodeValue;
            $ret['county']    = $bestNode->getElementsByTagName('county')->length ? $bestNode->getElementsByTagName('county')->item(0)->nodeValue : null;
            $ret['region']    = $bestNode->getElementsByTagName('state')->length ? $bestNode->getElementsByTagName('state')->item(0)->nodeValue : null;
            $ret['streetNumber'] = $bestNode->getElementsByTagName('house_number')->length ? $bestNode->getElementsByTagName('house_number')->item(0)->nodeValue : null;
            $ret['streetName']   = $bestNode->getElementsByTagName('road')->length ? $bestNode->getElementsByTagName('road')->item(0)->nodeValue : null;
            $ret['city']      = $bestNode->getElementsByTagName('city')->item(0)->nodeValue;
            $ret['country']   = $bestNode->getElementsByTagName('country')->item(0)->nodeValue;
            $ret['countryCode']  = strtoupper($bestNode->getElementsByTagName('country_code')->item(0)->nodeValue);

            return $ret;
        } else {
            return $this->getDefaults();
        }
    }

    /**
     * {@inheritDoc}
     */
    public function getReversedData(array $coordinates)
    {
        $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1]);

        $content = $this->executeQuery($query);

        if (null === $content) {
            return $this->getDefaults();
        }

        $doc = new DOMDocument();
        if (@$doc->loadXML($content)) {
            $maxRank          = 0;
            $bestNode         = null;
            $searchResult     = $doc->getElementsByTagName('reversegeocode')->item(0);
            $addressParts     = $searchResult->getElementsByTagName('addressparts')->item(0);
            $result           = $searchResult->getElementsByTagName('result')->item(0);
            $ret              = $this->getDefaults();
            $ret['latitude']  = $result->getAttribute('lat');
            $ret['longitude'] = $result->getAttribute('lon');
            $ret['zipcode']   = $addressParts->getElementsByTagName('postcode')->item(0)->nodeValue;
            $ret['county']    = $addressParts->getElementsByTagName('county')->length ? $addressParts->getElementsByTagName('county')->item(0)->nodeValue : null;
            $ret['region']    = $addressParts->getElementsByTagName('state')->length ? $addressParts->getElementsByTagName('state')->item(0)->nodeValue : null;
            $ret['streetNumber'] = $addressParts->getElementsByTagName('house_number')->length ? $addressParts->getElementsByTagName('house_number')->item(0)->nodeValue : null;
            $ret['streetName']   = $addressParts->getElementsByTagName('road')->length ? $addressParts->getElementsByTagName('road')->item(0)->nodeValue : null;
            $ret['city']      = $addressParts->getElementsByTagName('city')->item(0)->nodeValue;
            $ret['country']   = $addressParts->getElementsByTagName('country')->item(0)->nodeValue;
            $ret['countryCode']  = strtoupper($addressParts->getElementsByTagName('country_code')->item(0)->nodeValue);
{'fraction_non_alphanumeric': 0.1690127801444712, 'fraction_numerical': 0.005834413780329691, 'mean_word_length': 2.7719175689835835, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 190, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, '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': '1881178', 'n_tokens_mistral': 3115, 'n_tokens_neox': 3055, 'n_words': 433}