<commit_before><?php

namespace Galahad\LaravelAddressing\Validator;

use Galahad\LaravelAddressing\Entity\AdministrativeArea;
use Galahad\LaravelAddressing\Entity\Country;
use Galahad\LaravelAddressing\LaravelAddressing;
use Illuminate\Validation\Validator;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * Class AdministrativeAreaValidator
 *
 * @package Galahad\LaravelAddressing\Validator
 * @author <NAME> <<EMAIL>>
 */
class AdministrativeAreaValidator extends Validator
{
    /**
     * The validator messages
     *
     * @var array
     */
    protected $messages = [
        'administrative_area' => 'The :attribute has not a valid administrative area name.',
        'administrative_area_code' => 'The :attribute has not a valid administrative area code.',
        'administrative_area_name' => 'The :attribute has not a valid administrative area name.',
    ];

    /**
     * @var LaravelAddressing
     */
    protected $addressing;

    /**
     * @param TranslatorInterface $translator
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $customAttributes
     */
    public function __construct(
        TranslatorInterface $translator,
        array $data = [],
        array $rules = [],
        array $messages = [],
        array $customAttributes = []
    ) {
        parent::__construct($translator, $data, $rules, $messages, $customAttributes);
        $this->setCustomMessages($this->messages);
        $this->addressing = new LaravelAddressing();
    }

    /**
     * Validate an Administrative Area by its code and country
     *
     * @param string $attribute
     * @param mixed $value
     * @param array $parameters
     * @return bool
     */
    protected function validateAdministrativeAreaCode($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'administrative_area_code');
        $country = $this->getCountryInstance($parameters);
        $admArea = $country->getAdministrativeAreas()->getByCode($value);

        return $admArea instanceof AdministrativeArea;
    }

    /**
     * Validate an Administrative Area by its name and country
     *
     * @param $attribute
     * @param $value
     * @param $parameters
     * @return bool
     */
    protected function validateAdministrativeAreaName($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'administrative_area_name');
        $country = $this->getCountryInstance($parameters);
        $admArea = $country->getAdministrativeAreas()->getByName($value);

        return $admArea instanceof AdministrativeArea;
    }

    /**
     * Get the country instance according the parameters and values
     *
     * @param array $parameters
     * @return Country|null
     */
    private function getCountryInstance(array $parameters)<commit_msg>Add general validation for administrative area entity<commit_after><?php

namespace Galahad\LaravelAddressing\Validator;

use Galahad\LaravelAddressing\Entity\AdministrativeArea;
use Galahad\LaravelAddressing\Entity\Country;
use Galahad\LaravelAddressing\LaravelAddressing;
use Illuminate\Validation\Validator;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * Class AdministrativeAreaValidator
 *
 * @package Galahad\LaravelAddressing\Validator
 * @author <NAME> <<EMAIL>>
 */
class AdministrativeAreaValidator extends Validator
{
    /**
     * The validator messages
     *
     * @var array
     */
    protected $messages = [
        'administrative_area' => 'The :attribute has not a valid administrative area name.',
        'administrative_area_code' => 'The :attribute has not a valid administrative area code.',
        'administrative_area_name' => 'The :attribute has not a valid administrative area name.',
    ];

    /**
     * @var LaravelAddressing
     */
    protected $addressing;

    /**
     * @param TranslatorInterface $translator
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $customAttributes
     */
    public function __construct(
        TranslatorInterface $translator,
        array $data = [],
        array $rules = [],
        array $messages = [],
        array $customAttributes = []
    ) {
        parent::__construct($translator, $data, $rules, $messages, $customAttributes);
        $this->setCustomMessages($this->messages);
        $this->addressing = new LaravelAddressing();
    }

    /**
     * Validate an Administrative Area by its code and country
     *
     * @param string $attribute
     * @param mixed $value
     * @param array $parameters
     * @return bool
     */
    protected function validateAdministrativeAreaCode($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'administrative_area_code');
        $country = $this->getCountryInstance($parameters);
        $admArea = $country->getAdministrativeAreas()->getByCode($value);

        return $admArea instanceof AdministrativeArea;
    }

    /**
     * Validate an Administrative Area by its name and country
     *
     * @param $attribute
     * @param $value
     * @param $parameters
     * @return bool
     */
    protected function validateAdministrativeAreaName($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'administrative_area_name');
        $country = $this->getCountryInstance($parameters);
        $admArea = $country->getAdministrativeAreas()->getByName($value);

        return $admArea instanceof AdministrativeArea;
    }

    /**
     * Validate an administrative area trying first by code and after by name
     *
     * @param string $attribute
     * @param mixed $value
     * @param array $parameters
     * @return bool
     */
    public function validateAdministrativeArea($attribute, $value, $parameters)
    {
        $codeValidation = $this->validateAdministrativeAreaCode($attribute, $value, $parameters);
        if (! $codeValidation) {
            return $this->validateAdministrativeAreaName($attribute, $value, $parameters);
        }

        return true;
    }

    /**
     * Get the country instance according the parameters and values
     *
     * @param array $parameters
     * @return Country|null
     */
    private function getCountryInstance(array $parameters)
{'fraction_non_alphanumeric': 0.10680069106329512, 'fraction_numerical': 0.0006282393591958537, 'mean_word_length': 3.071611253196931, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, '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': '3391750', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1645, 'n_words': 423}