/** * {@inheritdoc} */ public function __construct($options = null) { parent::__construct($options); // Validate all fields by default. if (empty($this->fields)) { $this->fields = AddressField::getAll(); } }
/** * Converts the provided data into php and writes it to the disk. */ function file_put_php($filename, $data) { $data = var_export($data, true); // The var_export output is terrible, so try to get it as close as possible // to the final result. $array_keys = ['0 => ', '1 => ', '2 => ', '3 => ', '4 => ', '5 => ', '6 => ', '7 => ', '8 => ', '9 => ', '10 => ', '11 => ']; $data = str_replace(['array (', "),\n", "=> \n "], ['[', "],\n", '=> '], $data); $data = str_replace('=> [', '=> [', $data); $data = str_replace($array_keys, '', $data); // Put fields into one row. $find = []; $replace = []; foreach (AddressField::getAll() as $field) { $find[] = "'{$field}',\n '"; $replace[] = "'{$field}', '"; } $data = str_replace($find, $replace, $data); // Replace format single quotes with double quotes, to parse \n properly. $data = str_replace(["format' => '", ";;;'"], ['format\' => "', '"'], $data); // Reindent (from 2 to 4 spaces). $data = str_replace(' ', ' ', $data); // Unescape backslashes. $data = str_replace('\\\\', '\\', $data); $data = '<?php' . "\n\n" . '$data = ' . $data . ';'; file_put_contents($filename, $data); }
/** * @covers \CommerceGuys\Addressing\Validator\Constraints\AddressFormatConstraintValidator */ public function testConstraintFields() { $allFields = AddressField::getAll(); $nameFields = [AddressField::GIVEN_NAME, AddressField::FAMILY_NAME]; $this->constraint->fields = array_diff($allFields, $nameFields); $address = new Address(); $address = $address->withCountryCode('CN')->withAdministrativeArea('Beijing Shi')->withLocality('Xicheng Qu')->withPostalCode('123456')->withAddressLine1('Yitiao Lu'); $this->validator->validate($address, $this->constraint); $this->assertNoViolation(); $this->constraint->fields = array_diff($allFields, [AddressField::POSTAL_CODE]); $address = $address->withPostalCode('INVALID')->withGivenName('John')->withFamilyName('Smith'); $this->validator->validate($address, $this->constraint); $this->assertNoViolation(); $this->constraint->fields = array_diff($allFields, [AddressField::ADMINISTRATIVE_AREA]); $address = $address->withAdministrativeArea('INVALID')->withPostalCode('123456'); $this->validator->validate($address, $this->constraint); $this->assertNoViolation(); $address = $address->withAdministrativeArea('Beijing Shi')->withLocality('INVALID'); $this->validator->validate($address, $this->constraint); $this->assertNoViolation(); }
/** * Extracts the address values. * * @param AddressInterface $address The address. * * @return array An array of values keyed by field constants. */ protected function extractAddressValues(AddressInterface $address) { $values = []; foreach (AddressField::getAll() as $field) { $getter = 'get' . ucfirst($field); $values[$field] = $address->{$getter}(); } return $values; }
/** * Gets the address values used to build the view. * * @param AddressInterface $address The address. * @param AddressFormat $addressFormat The address format. * * @return array The values, keyed by address field. */ protected function getValues(AddressInterface $address, AddressFormat $addressFormat) { $values = []; foreach (AddressField::getAll() as $field) { $getter = 'get' . ucfirst($field); $values[$field] = $address->{$getter}(); } // Replace the subdivision values with the names of any predefined ones. $originalValues = []; $subdivisionFields = $addressFormat->getUsedSubdivisionFields(); $parents = []; foreach ($subdivisionFields as $index => $field) { if (empty($values[$field])) { // This level is empty, so there can be no sublevels. break; } $parents[] = $index ? $originalValues[$subdivisionFields[$index - 1]] : $address->getCountryCode(); $subdivision = $this->subdivisionRepository->get($values[$field], $parents); if (!$subdivision) { break; } // Remember the original value so that it can be used for $parents. $originalValues[$field] = $values[$field]; // Replace the value with the expected code. $useLocalName = LocaleHelper::match($address->getLocale(), $subdivision->getLocale()); $values[$field] = $useLocalName ? $subdivision->getLocalCode() : $subdivision->getCode(); if (!$subdivision->hasChildren()) { // The current subdivision has no children, stop. break; } } return $values; }