commerceguys / addressing

A PHP addressing library, powered by CLDR and Google's address data.
MIT License
922 stars 135 forks source link

Replace subdivision_depth with subdivision_data_fields #213

Open bojanz opened 8 months ago

bojanz commented 8 months ago

Right now each address format has a subdivision_depth, used to determine whether to attempt to load subdivisions of a specific level.

The depth is an integer, which is imprecise, as it requires us to also look up the used fields. For example, "AD" has a subdivision_depth of 1, but the predefined subdivisions are localities, not administrative areas.

Let's replace subdivision_depth with subdivision_data_fields, which is an array of field names. So, before:

'subdivision_depth' => 2,

After:

'subdivision_data_fields' => ['administrativeArea', 'locality'],

The matching getter (getSubdivisionDataFields) could also replace getUsedSubdivisionFields().

The benefit of this, other than explicitness, is to enable future subdivision data opt-in. For example, we could define administrative areas for Hungary (as PR 208 does), define subdivision_data_fields, but keep administrativeArea out of the format itself. Then, anyone who wants to have these administrative areas (even though they're not required for addressing) could simply alter the format itself (or we could have an opt-in setting on our side), without the need to touch subdivision_depth.