vendor/commerceguys/addressing/src/Repository/AddressFormatRepository.php line 41

Open in your IDE?
  1. <?php
  2. namespace CommerceGuys\Addressing\Repository;
  3. use CommerceGuys\Addressing\Model\AddressFormat;
  4. class AddressFormatRepository implements AddressFormatRepositoryInterface
  5. {
  6.     use DefinitionTranslatorTrait;
  7.     /**
  8.      * The path where address format definitions are stored.
  9.      *
  10.      * @var string
  11.      */
  12.     protected $definitionPath;
  13.     /**
  14.      * Address format definitions.
  15.      *
  16.      * @var array
  17.      */
  18.     protected $definitions = [];
  19.     /**
  20.      * Creates an AddressFormatRepository instance.
  21.      *
  22.      * @param string $definitionPath Path to the address format definitions.
  23.      *                               Defaults to 'resources/address_format'.
  24.      */
  25.     public function __construct($definitionPath null)
  26.     {
  27.         $this->definitionPath $definitionPath ?: __DIR__ '/../../resources/address_format/';
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function get($countryCode$locale null)
  33.     {
  34.         $definition $this->loadDefinition($countryCode);
  35.         if (!$definition) {
  36.             // No definition found for the given country code, fallback to ZZ.
  37.             $definition $this->loadDefinition('ZZ');
  38.         }
  39.         $definition $this->translateDefinition($definition$locale);
  40.         return $this->createAddressFormatFromDefinition($definition);
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function getAll($locale null)
  46.     {
  47.         // Gather available address formats.
  48.         // This is slow, but survivable because the only use case for
  49.         // fetching all address formats is mass import into another storage.
  50.         $addressFormats = [];
  51.         if ($handle opendir($this->definitionPath)) {
  52.             while (false !== ($entry readdir($handle))) {
  53.                 if (substr($entry01) != '.') {
  54.                     $countryCode strtok($entry'.');
  55.                     $addressFormats[$countryCode] = $this->get($countryCode$locale);
  56.                 }
  57.             }
  58.             closedir($handle);
  59.         }
  60.         return $addressFormats;
  61.     }
  62.     /**
  63.      * Loads the address format definition for the provided country code.
  64.      *
  65.      * @param string $countryCode The country code.
  66.      *
  67.      * @return array The address format definition.
  68.      */
  69.     protected function loadDefinition($countryCode)
  70.     {
  71.         if (!isset($this->definitions[$countryCode])) {
  72.             $filename $this->definitionPath $countryCode '.json';
  73.             $rawDefinition = @file_get_contents($filename);
  74.             if ($rawDefinition) {
  75.                 $rawDefinition json_decode($rawDefinitiontrue);
  76.                 $rawDefinition['country_code'] = $countryCode;
  77.                 $this->definitions[$countryCode] = $rawDefinition;
  78.             } else {
  79.                 // Bypass further loading attempts.
  80.                 $this->definitions[$countryCode] = [];
  81.             }
  82.         }
  83.         return $this->definitions[$countryCode];
  84.     }
  85.     /**
  86.      * Creates an address format object from the provided definition.
  87.      *
  88.      * @param array $definition The address format definition.
  89.      *
  90.      * @return AddressFormat
  91.      */
  92.     protected function createAddressFormatFromDefinition(array $definition)
  93.     {
  94.         $addressFormat = new AddressFormat();
  95.         // Bind the closure to the AddressFormat object, giving it access to
  96.         // its protected properties. Faster than both setters and reflection.
  97.         $setValues = \Closure::bind(function ($definition) {
  98.             $this->countryCode $definition['country_code'];
  99.             $this->format $definition['format'];
  100.             $this->requiredFields $definition['required_fields'];
  101.             $this->uppercaseFields $definition['uppercase_fields'];
  102.             $this->locale $definition['locale'];
  103.             if (isset($definition['administrative_area_type'])) {
  104.                 $this->administrativeAreaType $definition['administrative_area_type'];
  105.             }
  106.             if (isset($definition['locality_type'])) {
  107.                 $this->localityType $definition['locality_type'];
  108.             }
  109.             if (isset($definition['dependent_locality_type'])) {
  110.                 $this->dependentLocalityType $definition['dependent_locality_type'];
  111.             }
  112.             if (isset($definition['postal_code_type'])) {
  113.                 $this->postalCodeType $definition['postal_code_type'];
  114.             }
  115.             if (isset($definition['postal_code_pattern'])) {
  116.                 $this->postalCodePattern $definition['postal_code_pattern'];
  117.             }
  118.             if (isset($definition['postal_code_prefix'])) {
  119.                 $this->postalCodePrefix $definition['postal_code_prefix'];
  120.             }
  121.         }, $addressFormat'\CommerceGuys\Addressing\Model\AddressFormat');
  122.         $setValues($definition);
  123.         return $addressFormat;
  124.     }
  125. }