LabelHelper.php 8.39 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\address;

5
6
7
8
9
10
use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\AdministrativeAreaType;
use CommerceGuys\Addressing\AddressFormat\DependentLocalityType;
use CommerceGuys\Addressing\AddressFormat\LocalityType;
use CommerceGuys\Addressing\AddressFormat\PostalCodeType;
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
11
12
13
14
15
16
17
18
19
20
21
22
23

/**
 * Provides translated labels for the library enums.
 */
class LabelHelper {

  /**
   * Gets the field labels suitable for the given address format.
   *
   * Intended to be shown to the end user, they sometimes use a more familiar
   * term than the field name (Company instead of Organization, Contact name
   * instead of Recipient, etc).
   *
24
   * @param \CommerceGuys\Addressing\AddressFormat\AddressFormat $address_format
25
26
27
28
29
   *   The address format.
   *
   * @return string[]
   *   An array of labels, keyed by field.
   */
30
  public static function getFieldLabels(AddressFormat $address_format) {
31
32
33
34
    $administrative_area_type = $address_format->getAdministrativeAreaType();
    $locality_type = $address_format->getLocalityType();
    $dependent_locality_type = $address_format->getDependentLocalityType();
    $postal_code_type = $address_format->getPostalCodeType();
35
36

    return [
37
38
39
40
      AddressField::ADMINISTRATIVE_AREA => self::getAdministrativeAreaLabel($administrative_area_type),
      AddressField::LOCALITY => self::getLocalityLabel($locality_type),
      AddressField::DEPENDENT_LOCALITY => self::getDependentLocalityLabel($dependent_locality_type),
      AddressField::POSTAL_CODE => self::getPostalCodeLabel($postal_code_type),
41
      // Google's library always labels the sorting code field as "Cedex".
42
43
      AddressField::SORTING_CODE => t('Cedex', [], ['context' => 'Address label']),
      AddressField::ADDRESS_LINE1 => t('Street address', [], ['context' => 'Address label']),
44
      // The address line 2 label is usually shown only to screen-reader users.
45
46
      AddressField::ADDRESS_LINE2 => t('Street address line 2', [], ['context' => 'Address label']),
      AddressField::ORGANIZATION => t('Company', [], ['context' => 'Address label']),
47
48
49
      AddressField::GIVEN_NAME => t('First name', [], ['context' => 'Address label']),
      AddressField::ADDITIONAL_NAME => t('Middle name', [], ['context' => 'Address label']),
      AddressField::FAMILY_NAME => t('Last name', [], ['context' => 'Address label']),
50
51
52
53
54
55
56
57
58
59
60
61
62
    ];
  }

  /**
   * Gets the generic field labels.
   *
   * Intended primarily for backend settings screens.
   *
   * @return string[]
   *   The field labels, keyed by field.
   */
  public static function getGenericFieldLabels() {
    return [
63
64
65
66
67
68
69
      AddressField::ADMINISTRATIVE_AREA => t('Administrative area', [], ['context' => 'Address label']),
      AddressField::LOCALITY => t('Locality', [], ['context' => 'Address label']),
      AddressField::DEPENDENT_LOCALITY => t('Dependent locality', [], ['context' => 'Address label']),
      AddressField::POSTAL_CODE => t('Postal code', [], ['context' => 'Address label']),
      AddressField::SORTING_CODE => t('Sorting code', [], ['context' => 'Address label']),
      AddressField::ADDRESS_LINE1 => t('Address line 1', [], ['context' => 'Address label']),
      AddressField::ADDRESS_LINE2 => t('Address line 2', [], ['context' => 'Address label']),
70
71
72
73
      AddressField::ORGANIZATION => t('Company', [], ['context' => 'Address label']),
      AddressField::GIVEN_NAME => t('First name', [], ['context' => 'Address label']),
      AddressField::ADDITIONAL_NAME => t('Middle name', [], ['context' => 'Address label']),
      AddressField::FAMILY_NAME => t('Last name', [], ['context' => 'Address label']),
74
75
76
77
78
79
    ];
  }

  /**
   * Gets the administrative area label for the given type.
   *
80
   * @param string $administrative_area_type
81
82
83
84
85
   *   The administrative area type.
   *
   * @return string
   *   The administrative area label.
   */
86
87
  public static function getAdministrativeAreaLabel($administrative_area_type) {
    if (!$administrative_area_type) {
88
89
      return NULL;
    }
90
    AdministrativeAreaType::assertExists($administrative_area_type);
91
92
    $labels = self::getAdministrativeAreaLabels();

93
    return $labels[$administrative_area_type];
94
95
96
97
98
99
100
101
102
103
  }

  /**
   * Gets all administrative area labels.
   *
   * @return string[]
   *   The administrative area labels, keyed by type.
   */
  public static function getAdministrativeAreaLabels() {
    return [
104
105
106
107
108
109
110
111
112
113
114
115
      AdministrativeAreaType::AREA => t('Area', [], ['context' => 'Address label']),
      AdministrativeAreaType::COUNTY => t('County', [], ['context' => 'Address label']),
      AdministrativeAreaType::DEPARTMENT => t('Department', [], ['context' => 'Address label']),
      AdministrativeAreaType::DISTRICT => t('District', [], ['context' => 'Address label']),
      AdministrativeAreaType::DO_SI => t('Do si', [], ['context' => 'Address label']),
      AdministrativeAreaType::EMIRATE => t('Emirate', [], ['context' => 'Address label']),
      AdministrativeAreaType::ISLAND => t('Island', [], ['context' => 'Address label']),
      AdministrativeAreaType::OBLAST => t('Oblast', [], ['context' => 'Address label']),
      AdministrativeAreaType::PARISH => t('Parish', [], ['context' => 'Address label']),
      AdministrativeAreaType::PREFECTURE => t('Prefecture', [], ['context' => 'Address label']),
      AdministrativeAreaType::PROVINCE => t('Province', [], ['context' => 'Address label']),
      AdministrativeAreaType::STATE => t('State', [], ['context' => 'Address label']),
116
117
118
119
120
121
    ];
  }

  /**
   * Gets the locality label for the given type.
   *
122
   * @param string $locality_type
123
124
125
126
127
   *   The locality type.
   *
   * @return string
   *   The locality label.
   */
128
129
  public static function getLocalityLabel($locality_type) {
    if (!$locality_type) {
130
131
      return NULL;
    }
132
    LocalityType::assertExists($locality_type);
133
134
    $labels = self::getLocalityLabels();

135
    return $labels[$locality_type];
136
137
138
139
140
141
142
143
144
145
  }

  /**
   * Gets all locality labels.
   *
   * @return string[]
   *   The locality labels, keyed by type.
   */
  public static function getLocalityLabels() {
    return [
146
147
148
      LocalityType::CITY => t('City', [], ['context' => 'Address label']),
      LocalityType::DISTRICT => t('District', [], ['context' => 'Address label']),
      LocalityType::POST_TOWN => t('Post town', [], ['context' => 'Address label']),
149
      LocalityType::SUBURB => t('Suburb', [], ['context' => 'Address label']),
150
151
152
153
154
155
    ];
  }

  /**
   * Gets the dependent locality label for the given type.
   *
156
   * @param string $dependent_locality_type
157
158
159
160
161
   *   The dependent locality type.
   *
   * @return string
   *   The dependent locality label.
   */
162
163
  public static function getDependentLocalityLabel($dependent_locality_type) {
    if (!$dependent_locality_type) {
164
165
      return NULL;
    }
166
    DependentLocalityType::assertExists($dependent_locality_type);
167
168
    $labels = self::getDependentLocalityLabels();

169
    return $labels[$dependent_locality_type];
170
171
172
173
174
175
176
177
178
179
  }

  /**
   * Gets all dependent locality labels.
   *
   * @return string[]
   *   The dependent locality labels, keyed by type.
   */
  public static function getDependentLocalityLabels() {
    return [
180
181
182
183
      DependentLocalityType::DISTRICT => t('District', [], ['context' => 'Address label']),
      DependentLocalityType::NEIGHBORHOOD => t('Neighborhood', [], ['context' => 'Address label']),
      DependentLocalityType::VILLAGE_TOWNSHIP => t('Village township', [], ['context' => 'Address label']),
      DependentLocalityType::SUBURB => t('Suburb', [], ['context' => 'Address label']),
184
      DependentLocalityType::TOWNLAND => t('Townland', [], ['context' => 'Address label']),
185
186
187
188
189
190
    ];
  }

  /**
   * Gets the postal code label for the given type.
   *
191
   * @param string $postal_code_type
192
193
194
195
196
   *   The postal code type.
   *
   * @return string
   *   The postal code label.
   */
197
198
  public static function getPostalCodeLabel($postal_code_type) {
    if (!$postal_code_type) {
199
200
      return NULL;
    }
201
    PostalCodeType::assertExists($postal_code_type);
202
203
    $labels = self::getPostalCodeLabels();

204
    return $labels[$postal_code_type];
205
206
207
208
209
210
211
212
213
214
  }

  /**
   * Gets all postal code labels.
   *
   * @return string[]
   *   The postal code labels, keyed by type.
   */
  public static function getPostalCodeLabels() {
    return [
215
216
217
      PostalCodeType::POSTAL => t('Postal code', [], ['context' => 'Address label']),
      PostalCodeType::ZIP => t('Zip code', [], ['context' => 'Address label']),
      PostalCodeType::PIN => t('Pin code', [], ['context' => 'Address label']),
218
219
220
221
    ];
  }

}