/** * Creates a subdivision object from the provided definitions. * * @param int $id The subdivision id. * @param array $definition The subdivision definitions. * @param string $locale The locale (e.g. fr-FR). * * @return Subdivision */ protected function createSubdivisionFromDefinitions($id, array $definitions, $locale) { if (!isset($definitions['subdivisions'][$id])) { // No matching definition found. return null; } $definition = $this->translateDefinition($definitions['subdivisions'][$id], $locale); // Add common keys from the root level. $definition['country_code'] = $definitions['country_code']; $definition['parent_id'] = $definitions['parent_id']; $definition['locale'] = $definitions['locale']; // Provide defaults. if (!isset($definition['code'])) { $definition['code'] = $definition['name']; } // Load the parent, if known. $definition['parent'] = null; if (isset($definition['parent_id'])) { $parentId = $definition['parent_id']; if (!isset($this->parents[$parentId])) { $this->parents[$parentId] = $this->get($definition['parent_id']); } $definition['parent'] = $this->parents[$parentId]; } $subdivision = new Subdivision(); // Bind the closure to the Subdivision object, giving it access to its // protected properties. Faster than both setters and reflection. $setValues = \Closure::bind(function ($id, $definition) { $this->parent = $definition['parent']; $this->countryCode = $definition['country_code']; $this->id = $id; $this->code = $definition['code']; $this->name = $definition['name']; $this->locale = $definition['locale']; if (isset($definition['postal_code_pattern'])) { $this->postalCodePattern = $definition['postal_code_pattern']; if (isset($definition['postal_code_pattern_type'])) { $this->postalCodePatternType = $definition['postal_code_pattern_type']; } else { $this->postalCodePatternType = PatternType::getDefault(); } } }, $subdivision, '\\CommerceGuys\\Addressing\\Model\\Subdivision'); $setValues($id, $definition); if (!empty($definition['has_children'])) { $children = new LazySubdivisionCollection($definition['country_code'], $id, $definition['locale']); $children->setRepository($this); $subdivision->setChildren($children); } return $subdivision; }
/** * {@inheritdoc} */ public function setPostalCodePatternType($postalCodePatternType) { PatternType::assertExists($postalCodePatternType); $this->postalCodePatternType = $postalCodePatternType; return $this; }