webit/terc

提供基于GUS TERC文件的地区实体

1.0.0 2019-03-02 15:35 UTC

This package is auto-updated.

Last update: 2024-08-24 07:46:47 UTC


README

提供基于GUS TERC文件的地区实体 (http://eteryt.stat.gov.pl) 请注意,此仓库仅包含示例数据。请从官方GUS网站获取真实数据。

安装

通过 composer 安装

composer require webit/terc

用法

省级

VoivodeshipRepository 提供访问代表波兰省级(pol. "województwo")的 Voivodeship 对象的方法。

<?php
use Webit\Terc\VoivodeshipRepository;
use Webit\Terc\Voivodeship;
use Webit\Terc\VoivodeshipCode;

// check how to obtain an instance of repository in the Infrastructure section of this README.md
/** @var VoivodeshipRepository $repository */

/** @var Voivodeship $voivodeship */
$voivodeship = $repository->get(new VoivodeshipCode('02'));  // returns `Voivodeship`
echo $voivodeship->code() . "\n"; // shows `VoivodeshipCode` object
echo $voivodeship->name() . "\n"; // shows voivodeship's name
echo (string)$voivodeship . "\n"; // returns voivodeship's name

$voivodeship = $repository->getByName('dolnośląskie'); // returns `Voivodeship`

// getting all the voivodeships
foreach ($repository->getAll() as $province) {
    // do something
}

地区

DistrictRepository 提供访问代表波兰地区(pol. "powiat")的 District 对象的方法。

<?php

use Webit\Terc\DistrictCode;
use Webit\Terc\DistrictRepository;
use Webit\Terc\District;
use Webit\Terc\VoivodeshipCode;

// check how to obtain an instance of repository in the Infrastructure section of this README.md
/** @var DistrictRepository $repository */

/** @var District $district */
$district = $repository->get(new DistrictCode('02'));

echo $district->code() . "\n"; // shows `DistrictCode` object (4 digits code)
echo $district->name() . "\n"; // shows district name
echo $district->type() . "\n"; // shows district type
echo $district->code()->voivodeshipCode() . "\n"; // shows `VoivodeshipCode`
echo $district->code()->districtCode() . "\n"; // shows two digits district code
echo (string)$district . "\n"; // returns districts name

echo $repository->getByVoivodeshipAndName(new VoivodeshipCode('02'), 'dzierżoniowski'); // returns districts's name

// getting all the districts
$limitOffset = \Webit\Terc\LimitOffset::create(250, 0); // optional, 100, 0 by default
foreach ($repository->getAll($limitOffset) as $district) {
    // do something    
}

// getting all the districts of given voivodeship
foreach ($repository->getAllOfVoivodeship(new VoivodeshipCode('02')) as $district) {
    // do something
}

市镇

BoroughRepository 提供访问代表波兰市镇(pol. "gmina")的 Borough 对象的方法。

<?php

use Webit\Terc\DistrictCode;
use Webit\Terc\BoroughCode;
use Webit\Terc\BoroughRepository;
use Webit\Terc\Borough;
use Webit\Terc\VoivodeshipCode;

// check how to obtain an instance of repository in the Infrastructure section of this README.md
/** @var BoroughRepository $repository */

/** @var Borough $borough */
$borough = $repository->get(new BoroughCode('0201011'));

echo $borough->code() . "\n"; // shows `BoroughCode` object (7 digits code)
echo $borough->name() . "\n"; // shows borough name
echo $borough->code()->voivodeshipCode() . "\n"; // shows `VoivodeshipCode` object
echo $borough->code()->districtCode() . "\n"; // shows `DistrictCode` object
echo $borough->code()->boroughCode() . "\n"; // shows two digits borough code
echo $borough->code()->boroughType() . "\n"; // shows `BoroughType` object
echo (string)$borough . "\n"; // returns districts's name

$boroughs = $repository->getByDistrictAndName(new DistrictCode('0201'), 'Boleslawiec'); // returns `BoroughCollection`

// getting all the boroughs
$limitOffset = \Webit\Terc\LimitOffset::create(250, 0); // optional, 100, 0 by default
foreach ($repository->getAll($limitOffset) as $borough) {
    // do something
}

// getting all the boroughs of given voivodeship
$limitOffset = \Webit\Terc\LimitOffset::create(250, 0); // optional, 100, 0 by default
foreach ($repository->getAllOfVoivodeship(new VoivodeshipCode('02'), $limitOffset) as $boroughs) {
    // do something
}

// getting all the boroughs of given district
foreach ($repository->getAllOfDistrict(new DistrictCode('0201')) as $boroughs) {
    // do something
}

基础设施

该库提供两种基础设施实现

  • InMemory - 将所有实体保存在内存中
  • Doctrine - 基于Doctrine ORM

内存基础设施

省级

<?php
use Webit\Terc\Infrastructure\InMemory\VoivodeshipRepositoryBuilder;
use Webit\Terc\Infrastructure\TercFile;

$builder = VoivodeshipRepositoryBuilder::create();
$builder->setLazy(false); // if you want initialise repository on build, true by default
$builder->fromFile(TercFile::create('my_TERC_filename.csv')); // if you want to provide own TERC.csv file (using build-in be default)
$builder->setCacheDir('/cache/dir'); // sets cache to FilesystemCache of Doctrine with a given directory
$builder->setCache(new \Doctrine\Common\Cache\ArrayCache()); // sets preconfigured cache

$voivodeshipRepository = $builder->build();

地区

<?php
use Webit\Terc\Infrastructure\InMemory\DistrictRepositoryBuilder;
use Webit\Terc\Infrastructure\TercFile;

$builder = DistrictRepositoryBuilder::create();
$builder->setLazy(false); // if you want initialise repository on build, true by default
$builder->fromFile(TercFile::create('my_TERC_filename.csv')); // if you want to provide own TERC.csv file (using build-in be default)
$builder->setCacheDir('/cache/dir'); // sets cache to FilesystemCache of Doctrine with a given directory
$builder->setCache(new \Doctrine\Common\Cache\ArrayCache()); // sets preconfigured cache

$districtRepository = $builder->build();

市镇

<?php
use Webit\Terc\Infrastructure\InMemory\BoroughRepositoryBuilder;
use Webit\Terc\Infrastructure\TercFile;

$builder = BoroughRepositoryBuilder::create();
$builder->setLazy(false); // if you want initialise repository on build, true by default
$builder->fromFile(TercFile::create('my_TERC_filename.csv')); // if you want to provide own TERC.csv file (using build-in be default)
$builder->setCacheDir('/cache/dir'); // sets cache to FilesystemCache of Doctrine with a given directory
$builder->setCache(new \Doctrine\Common\Cache\ArrayCache()); // sets preconfigured cache

$boroughRepository = $builder->build();

使用新文件更新TERC实体

如果 InMemory 存储库正在使用缓存,则应使用 TercEntitiesInMemoryUpdater 以替换当前实体与来自新文件的实体。

<?php
use Webit\Terc\Infrastructure\InMemory\TercEntitiesInMemoryUpdater;
use Webit\Terc\Infrastructure\TercEntityFromCsvLoader;
use Webit\Terc\Infrastructure\TercFile;
use Doctrine\Common\Cache\FilesystemCache;

$tercEntityLoader = new TercEntityFromCsvLoader(
    TercFile::create('new_TERC_file.csv'),
    $cache = new FilesystemCache('/cache/dir') // or any other used
);

$updater = new TercEntitiesInMemoryUpdater(
    $tercEntityLoader,
    $cache
);
$updater->update();

Doctrine ORM基础设施

为了使用Doctrine基础设施,您需要配置 EntityManager 并更新数据库模式

<?php
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Webit\Terc\Infrastructure\Doctrine\Mapping\Mapping;
use Doctrine\DBAL\DriverManager;

$configuration = new Configuration();
$configuration->setMetadataDriverImpl(Mapping::createDriver());
$configuration->setProxyDir(sys_get_temp_dir().substr(md5(time().mt_rand(0, 100000)), 0, 6));
$configuration->setProxyNamespace('DoctrineProxy\\');

$entityManager = EntityManager::create(
    DriverManager::getConnection(['url' => 'sqlite:///:memory:']),
    $configuration
);

省级

<?php
use Webit\Terc\Infrastructure\Doctrine\Voivodeship;

/** @var Doctrine\ORM\EntityManager $entityManager */

$voivodeshipRepository = $entityManager->getRepository(Voivodeship::class);

地区

<?php
use Webit\Terc\Infrastructure\Doctrine\District;

/** @var Doctrine\ORM\EntityManager $entityManager */

$districtRepository = $entityManager->getRepository(District::class);

市镇

<?php
use Webit\Terc\Infrastructure\Doctrine\Borough;

/** @var Doctrine\ORM\EntityManager $entityManager */

$boroughRepository = $entityManager->getRepository(Borough::class);

使用新文件更新TERC实体

<?php
use Webit\Terc\Infrastructure\Doctrine\TercEntitiesDoctrineUpdater;
use Webit\Terc\Infrastructure\TercEntityFromCsvLoader;
use Webit\Terc\Infrastructure\TercFile;

/** @var Doctrine\ORM\EntityManager $entityManager */

$updater = new TercEntitiesDoctrineUpdater(
    $entityManager,
    new TercEntityFromCsvLoader(
        TercFile::create('new_TERC_file.csv')
    )
);

$updater->update();

测试

docker-compose run --rm composer
docker-compose run --rm phpunit