umpirsky / country-list

:globe_with_meridians: List of all countries with names and ISO 3166-1 codes in all languages and data formats.
MIT License
5.16k stars 1.55k forks source link

how to use #72

Closed denjaland closed 6 years ago

denjaland commented 6 years ago

Hi,

Thanks for this. What's the proper way to use these in any project? As it's just lists and no actual classes, I'm not entirely sure how to use them? Or am I missing something?

David.

umpirsky commented 6 years ago

@denjaland Hi. It's actually up to you. This is a general purpose data, you can choose your preferred format and use if the way you wish. From plain text and importing CSV into google docs to importing it in MySQL database and using with any web language/framework to wrapping XML or YAML and using with Java or PHP. This is just the data source.

denjaland commented 6 years ago

Okay, but I copy those files then? It's just a respository of data you mean?

denjaland commented 6 years ago

I was looking at how one can actually consume this using composer and import the data into my project without the requirement of having to copy the files... - hence my question ;-)

umpirsky commented 6 years ago

@denjaland If you want to use it with composer just run composer require umpirsky/country-list, you can also install it with npm.

MiguelDebruyne commented 6 years ago

I think he was asking if you have to copy the files in country-list/data/ manually or are there functions you can call. I am trying to figuring this out to. The readme doesn't say anything about that. Please consider adding it to the readme on how to load/use.

kruncher commented 5 years ago

+1 needs usage documentation; how do you include the PHP version into a PHP script in the proper way?

umpirsky commented 5 years ago

Same as any other script:

$countries = require `data/en/country.php`;
jszczypk commented 4 years ago

It would be better to encapsulate PHP data in classes somehow to use composer autoloader. Now you have to know exact path from your current script to vendor/umprisky/country-list/data...

umpirsky commented 4 years ago

With autoload you need to know exact path to vendor/autoload.php too.

jszczypk commented 4 years ago

Yes, but only in single file that is run at the begining of every request in your project. With current setup, you can't have a package-submodule required to your main project through composer that uses your data, because you will have to hardcode vendor path in that submodule.

How about creating classes like \Umpirsky\CountryList\en that will have array with data as constant?

I will take a look at https://github.com/umpirsky/list-generator/blob/master/src/Umpirsky/ListGenerator/Exporter/Format/Php.php and try to modify it to make classes. I am not sure if it will not be better to have all classes in single directory to have much simpler autoloader in composer.json

umpirsky commented 4 years ago

I'd rather add utility class that is capable of locating the resource with some trick like https://github.com/composer/composer/issues/2904#issuecomment-40298252.

jszczypk commented 4 years ago

Utility class gave me an idea. Wait a second.