<commit_before><?php

/**
 * (c) FSi sp. z o.o <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace FSi\Bundle\TerytDatabaseBundle\Teryt\Import;

use FSi\Bundle\TerytDatabaseBundle\Entity\Community;
use FSi\Bundle\TerytDatabaseBundle\Entity\District;
use FSi\Bundle\TerytDatabaseBundle\Entity\Province;
use FSi\Bundle\TerytDatabaseBundle\Exception\TerritorialDivisionNodeConverterException;

class TerritorialDivisionNodeConverter extends NodeConverter
{
    const WOJ_CHILD_NODE = 0;
    const POw_CHILD_NODE = 1;
    const GMI_CHILD_NODE = 2;
    const TYPE_CHILD_NODE = 3;
    const NAZWA_CHILD_NODE = 4;

    /**
     * @throws \FSi\Bundle\TerytDatabaseBundle\Exception\TerritorialDivisionNodeConverterException
     * @return \FSi\Bundle\TerytDatabaseBundle\Entity\Community|\FSi\Bundle\TerytDatabaseBundle\Entity\District|\FSi\Bundle\TerytDatabaseBundle\Entity\Province
     */
    public function convertToEntity()
    {
        if ($this->isProvinceNode()) {
            return $this->convertToProvince();
        }

        if ($this->isDistrict()) {
            return $this->convertToDistrict();
        }

        if ($this->isCommunity()) {
            return $this->convertToCommunity();

        }

        throw new TerritorialDivisionNodeConverterException();
    }

    /**
     * @return bool
     */
    private function isProvinceNode()
    {
        return $this->hasProvinceCode() && !$this->hasDistrictCode();
    }

    /**
     * @return bool
     */
    private function isDistrict()
    {
        return $this->hasProvinceCode() && $this->hasDistrictCode() && !$this->hasCommunityCode();
    }

    /**
     * @return bool
     */
    public function isCommunity()
    {
        return $this->hasProvinceCode() && $this->hasDistrictCode() && $this->hasCommunityCode();
    }

    /**
     * @return Province
     */
    private function convertToProvince()
    {
        $provinceEntity = new Province();
        $provinceEntity->setName($this->getTerritoryName())
            ->setCode($this->getProvinceCode());

        return $provinceEntity;
    }

    /**
     * @return District
     */
    private function convertToDistrict()
    {
        $province = $this->om->getRepository('FSiTerytDbBundle:Province')->findOneBy(array(
            'code' => $this->getProvinceCode()
        ));

        $districtEntity = new District();
        $districtEntity->setCode(sprintf('%s%s', $this->getProvinceCode(), $this->getDistrictCode()))
            ->setName($this->getTerritoryName())
            ->setProvince($province);

        return $districtEntity;
    }

    /**
     * @return Community
     */
    private function convertToCommunity()
    {
        $district = $this->om->getRepository('FSiTerytDbBundle:District')->findOneBy(array(
            'code' => sprintf("%s%s", $this->getProvinceCode(), $this->getDistrictCode())
        ));

        $type = $this->om->getRepository('FSiTerytDbBundle:CommunityType')->findOneBy(array(
            'type' => (int) $this->node->col[self::TYPE_CHILD_NODE]
        ));

        $communityEntity = new Community();
        $communityEntity->setCode(sprintf(
            "%s%s%s%s",
            $this->getProvinceCode(),
            $this->getDistrictCode(),
            $this->getCommunityCode(),
            $this->getCommunityType()
        ))
            ->setName($this->getTerritoryName())
            ->setType($type)
            ->setDistrict($district);

        return $communityEntity;
    }

    /**
     * @return string
     */
    public function getDistrictCode()
    {
        return (string) $this->node->col[self::POw_CHILD_NODE];
    }

    /**
     * @return bool
     */
    private function hasProvinceCode()
    {
        return !empty($this->node->col[self::WOJ_CHILD_NODE]);
    }

    /**
     * @return string
     */
    private function getProvinceCode()
    {
        return (string) $this->node->col[self::WOJ_CHILD_NODE];
    }

    /**
     * @return bool
     */
    public function hasDistrictCode()
    {
        return !empty($this->node->col[self::POw_CHILD_NODE]);
    }

    /**
     * @return bool
     */
    public function hasCommunityCode()
    {
        return !empty($this->node->col[self::GMI_CHILD_NODE]);
    }

    /**
     * @return string
     */
    private function getCommunityCode()
    {
        return (string) $this->node->col[self::GMI_CHILD_NODE];
    }

    /**
     * @return string
     */
    private function getTerritoryName()
    {
        return (string) $this->node->col[self::NAZWA_CHILD_NODE];
    }

    /**
     * @return string
     */
    private function getCommunityType()
    {<commit_msg>Fixed typo<commit_after><?php

/**
 * (c) FSi sp. z o.o <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace FSi\Bundle\TerytDatabaseBundle\Teryt\Import;

use FSi\Bundle\TerytDatabaseBundle\Entity\Community;
use FSi\Bundle\TerytDatabaseBundle\Entity\District;
use FSi\Bundle\TerytDatabaseBundle\Entity\Province;
use FSi\Bundle\TerytDatabaseBundle\Exception\TerritorialDivisionNodeConverterException;

class TerritorialDivisionNodeConverter extends NodeConverter
{
    const WOJ_CHILD_NODE = 0;
    const POW_CHILD_NODE = 1;
    const GMI_CHILD_NODE = 2;
    const TYPE_CHILD_NODE = 3;
    const NAZWA_CHILD_NODE = 4;

    /**
     * @throws \FSi\Bundle\TerytDatabaseBundle\Exception\TerritorialDivisionNodeConverterException
     * @return \FSi\Bundle\TerytDatabaseBundle\Entity\Community|\FSi\Bundle\TerytDatabaseBundle\Entity\District|\FSi\Bundle\TerytDatabaseBundle\Entity\Province
     */
    public function convertToEntity()
    {
        if ($this->isProvinceNode()) {
            return $this->convertToProvince();
        }

        if ($this->isDistrict()) {
            return $this->convertToDistrict();
        }

        if ($this->isCommunity()) {
            return $this->convertToCommunity();

        }

        throw new TerritorialDivisionNodeConverterException();
    }

    /**
     * @return bool
     */
    private function isProvinceNode()
    {
        return $this->hasProvinceCode() && !$this->hasDistrictCode();
    }

    /**
     * @return bool
     */
    private function isDistrict()
    {
        return $this->hasProvinceCode() && $this->hasDistrictCode() && !$this->hasCommunityCode();
    }

    /**
     * @return bool
     */
    public function isCommunity()
    {
        return $this->hasProvinceCode() && $this->hasDistrictCode() && $this->hasCommunityCode();
    }

    /**
     * @return Province
     */
    private function convertToProvince()
    {
        $provinceEntity = new Province();
        $provinceEntity->setName($this->getTerritoryName())
            ->setCode($this->getProvinceCode());

        return $provinceEntity;
    }

    /**
     * @return District
     */
    private function convertToDistrict()
    {
        $province = $this->om->getRepository('FSiTerytDbBundle:Province')->findOneBy(array(
            'code' => $this->getProvinceCode()
        ));

        $districtEntity = new District();
        $districtEntity->setCode(sprintf('%s%s', $this->getProvinceCode(), $this->getDistrictCode()))
            ->setName($this->getTerritoryName())
            ->setProvince($province);

        return $districtEntity;
    }

    /**
     * @return Community
     */
    private function convertToCommunity()
    {
        $district = $this->om->getRepository('FSiTerytDbBundle:District')->findOneBy(array(
            'code' => sprintf("%s%s", $this->getProvinceCode(), $this->getDistrictCode())
        ));

        $type = $this->om->getRepository('FSiTerytDbBundle:CommunityType')->findOneBy(array(
            'type' => (int) $this->node->col[self::TYPE_CHILD_NODE]
        ));

        $communityEntity = new Community();
        $communityEntity->setCode(sprintf(
            "%s%s%s%s",
            $this->getProvinceCode(),
            $this->getDistrictCode(),
            $this->getCommunityCode(),
            $this->getCommunityType()
        ))
            ->setName($this->getTerritoryName())
            ->setType($type)
            ->setDistrict($district);

        return $communityEntity;
    }

    /**
     * @return string
     */
    public function getDistrictCode()
    {
        return (string) $this->node->col[self::POW_CHILD_NODE];
    }

    /**
     * @return bool
     */
    private function hasProvinceCode()
    {
        return !empty($this->node->col[self::WOJ_CHILD_NODE]);
    }

    /**
     * @return string
     */
    private function getProvinceCode()
    {
        return (string) $this->node->col[self::WOJ_CHILD_NODE];
    }

    /**
     * @return bool
     */
    public function hasDistrictCode()
    {
        return !empty($this->node->col[self::POW_CHILD_NODE]);
    }

    /**
     * @return bool
     */
    public function hasCommunityCode()
    {
        return !empty($this->node->col[self::GMI_CHILD_NODE]);
    }

    /**
     * @return string
     */
    private function getCommunityCode()
    {
        return (string) $this->node->col[self::GMI_CHILD_NODE];
    }

    /**
     * @return string
     */
    private function getTerritoryName()
    {
        return (string) $this->node->col[self::NAZWA_CHILD_NODE];
    }

    /**
     * @return string
     */
    private function getCommunityType()
    {
{'fraction_non_alphanumeric': 0.14257133831736338, 'fraction_numerical': 0.001052964093924397, 'mean_word_length': 2.436324167872648, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 135, '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': '1881487', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2794, 'n_words': 445}