serendipity_hq / component-geo-builder
Parses the exports of countries from Geonames and exports the data in machine readable formats.
Requires
- php: ^7.4|^8.0
- ext-zip: *
- guzzlehttp/guzzle: ^7.0
- symfony/dom-crawler: ^4.4|^5.4|^6.0
- symfony/serializer: ^4.4|^5.4|^6.0
- thecodingmachine/safe: ^1.3
Requires (Dev)
- ext-ast: *
- bamarni/composer-bin-plugin: ^1.4
- phpstan/phpstan: 1.3.2
- phpstan/phpstan-phpunit: 1.0.0
- rector/rector: 0.12.11
- roave/security-advisories: dev-master
- symfony/console: ^4.4|^5.4|^6.0
- symfony/form: ^4.4|^5.4|^6.0
- symfony/var-dumper: ^4.4|^5.4|^6.0
- thecodingmachine/phpstan-safe-rule: 1.1.0
Suggests
- symfony/form: To use the builtin form types.
This package is auto-updated.
Last update: 2024-12-02 08:53:49 UTC
README
Serendipity HQ Geo Builder
Parses the exports of countries from Geonames and exports the data in machine readable formats.
It downloads information of countries from [Geonames exports](https://www.geonames.org/export/zip/).
Ready to be integrated in Symfony apps.
Current Status
Features
- Download the exports of countries
- Build you custom lists of countries to use in your app
Do you like this library?
LEAVE A ★
or run
composer global require symfony/thanks && composer thanks
to say thank you to all libraries you use in your current project, this included!
Installation and Configuration
Install Component GeoBuilder via Composer
composer req serendipity_hq/component-geo-builder
This library follows the http://semver.org/ versioning conventions.
However, until the version 1, the minor release is treated like a major one.
So it is possible a break in the public API between minor versions (0.1 > 0.2 > 0.3).).
The component is anyway stable and can be used in production, also if it is not very flexible.
See the issues to know more about what we have in mind to implement.
Register the command in a Symfony application
Open the file config/services.yaml
and add the class of the geobuilder
command:
# config/services.yaml services: ... SerendipityHQ\Component\GeoBuilder\Command\BuildCommand: $dumpDir: '%kernel.cache_dir%/geobuilder' # You also need to autowire the Guzzle CLient if you don't already have one GuzzleHttp\Client: ~ ...
Building the list you need
To build the list you need, simply launch the command geobuilder:build
appending the countries you want to build lists for:
$ bin/console geobuilder:build it de
By default the command uses the Hierarchy reader and will create a lot of json
files in the kernel.cache_dir/geobuilder
folder of your Symfony App.
You can read more about readers, saving folders and more reading the full documentation.
For the moment, let's continue putting GeoBuilder
at work.
The next step is to create a form with the data we have just built.
Creating the form
To use the form types, you need to register them as services, so they can be properly initialized by Symfony.
To register them, open the file config/services.yaml
and add the HierarchyJsonType
:
# config/services.yaml services: ... SerendipityHQ\Component\GeoBuilder\Reader\HierarchyJsonReader: $dataFolderPath: '%kernel.cache_dir%/geobuilder' SerendipityHQ\Component\GeoBuilder\Bridge\Symfony\Form\Type\HierarchyJsonType: ~
Then in your form you can add the HierarchyJsonType
:
class UserZoneType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array<string,mixed> $options * @suppress PhanUnusedPublicMethodParameter */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder->add('geobuilder', HierarchyJsonType::class, ['country' => 'it']); } }
About the countries.json
file
This file will contain only the countries you built with the bin/console geobuilder:build
command.
You can get a complete list of localized countries using the Countries::getNames()
method of the symfony/intl
component.
But the Symfony component will return all countries in the world, also if you didn't built them.
Do you like this library?
LEAVE A ★
or run
composer global require symfony/thanks && composer thanks
to say thank you to all libraries you use in your current project, this included!