OLD | NEW |
---|---|
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 // | 14 // |
15 // An object to store validation rules. | 15 // An object to store validation rules. |
16 | 16 |
17 #ifndef I18N_ADDRESSINPUT_RULE_H_ | 17 #ifndef I18N_ADDRESSINPUT_RULE_H_ |
18 #define I18N_ADDRESSINPUT_RULE_H_ | 18 #define I18N_ADDRESSINPUT_RULE_H_ |
19 | 19 |
20 #include <libaddressinput/address_field.h> | 20 #include <libaddressinput/address_field.h> |
21 #include <libaddressinput/util/basictypes.h> | 21 #include <libaddressinput/util/basictypes.h> |
22 | 22 |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 | 25 |
26 namespace i18n { | 26 namespace i18n { |
27 namespace addressinput { | 27 namespace addressinput { |
28 | 28 |
29 struct FormatElement { | |
30 enum Type { | |
Evan Stade
2014/01/14 18:05:22
imo, you don't need this type. Just key off the em
please use gerrit instead
2014/01/14 23:21:12
Done.
| |
31 FIELD, | |
32 LITERAL | |
Evan Stade
2014/01/14 18:05:22
docs
please use gerrit instead
2014/01/14 23:21:12
Done.
| |
33 }; | |
34 | |
35 explicit FormatElement(AddressField field); | |
36 explicit FormatElement(const std::string& literal); | |
37 ~FormatElement(); | |
38 | |
39 bool operator==(const FormatElement& other) const; | |
40 | |
41 Type type; | |
42 AddressField field; | |
43 std::string literal; | |
44 }; | |
45 | |
29 // Stores the validation rules. Sample usage: | 46 // Stores the validation rules. Sample usage: |
30 // Rule rule; | 47 // Rule rule; |
31 // if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) { | 48 // if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) { |
32 // Process(rule.GetFormat()); | 49 // Process(rule.GetFormat()); |
33 // } | 50 // } |
34 class Rule { | 51 class Rule { |
35 public: | 52 public: |
36 Rule(); | 53 Rule(); |
37 ~Rule(); | 54 ~Rule(); |
38 | 55 |
39 // Returns the default rule at a country level. If a country does not specify | 56 // Returns the default rule at a country level. If a country does not specify |
40 // address format, for example, then the format from this rule should be used | 57 // address format, for example, then the format from this rule should be used |
41 // instead. | 58 // instead. |
42 static const Rule& GetDefault(); | 59 static const Rule& GetDefault(); |
43 | 60 |
44 // Copies all data from |rule|. | 61 // Copies all data from |rule|. |
45 void CopyFrom(const Rule& rule); | 62 void CopyFrom(const Rule& rule); |
46 | 63 |
47 // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid | 64 // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid |
48 // format (JSON dictionary). | 65 // format (JSON dictionary). |
49 bool ParseSerializedRule(const std::string& serialized_rule); | 66 bool ParseSerializedRule(const std::string& serialized_rule); |
50 | 67 |
51 // Returns the address format for this rule. | 68 // Returns the address format for this rule. |
52 const std::vector<std::vector<AddressField> >& GetFormat() const { | 69 const std::vector<std::vector<FormatElement> >& GetFormat() const { |
53 return format_; | 70 return format_; |
54 } | 71 } |
55 | 72 |
56 // Returns the required fields for this rule. | 73 // Returns the required fields for this rule. |
57 const std::vector<AddressField>& GetRequired() const { return required_; } | 74 const std::vector<AddressField>& GetRequired() const { return required_; } |
58 | 75 |
59 // Returns the sub-keys for this rule, which are the administrative areas of a | 76 // Returns the sub-keys for this rule, which are the administrative areas of a |
60 // country, the localities of an administrative area, or the dependent | 77 // country, the localities of an administrative area, or the dependent |
61 // localities of a locality. For example, the rules for "US" have sub-keys of | 78 // localities of a locality. For example, the rules for "US" have sub-keys of |
62 // "CA", "NY", "TX", etc. | 79 // "CA", "NY", "TX", etc. |
(...skipping 28 matching lines...) Expand all Loading... | |
91 // The error message string identifier for an invalid postal code. If not set, | 108 // The error message string identifier for an invalid postal code. If not set, |
92 // then INVALID_MESSAGE_ID. | 109 // then INVALID_MESSAGE_ID. |
93 int GetInvalidPostalCodeMessageId() const { | 110 int GetInvalidPostalCodeMessageId() const { |
94 return invalid_postal_code_message_id_; | 111 return invalid_postal_code_message_id_; |
95 } | 112 } |
96 | 113 |
97 // Returns the error message string identifier for an invalid |field|. | 114 // Returns the error message string identifier for an invalid |field|. |
98 int GetInvalidFieldMessageId(AddressField field) const; | 115 int GetInvalidFieldMessageId(AddressField field) const; |
99 | 116 |
100 private: | 117 private: |
101 std::vector<std::vector<AddressField> > format_; | 118 std::vector<std::vector<FormatElement> > format_; |
102 std::vector<AddressField> required_; | 119 std::vector<AddressField> required_; |
103 std::vector<std::string> sub_keys_; | 120 std::vector<std::string> sub_keys_; |
104 std::vector<std::string> languages_; | 121 std::vector<std::string> languages_; |
105 std::string language_; | 122 std::string language_; |
106 std::string postal_code_format_; | 123 std::string postal_code_format_; |
107 int admin_area_name_message_id_; | 124 int admin_area_name_message_id_; |
108 int invalid_admin_area_message_id_; | 125 int invalid_admin_area_message_id_; |
109 int postal_code_name_message_id_; | 126 int postal_code_name_message_id_; |
110 int invalid_postal_code_message_id_; | 127 int invalid_postal_code_message_id_; |
111 | 128 |
112 DISALLOW_COPY_AND_ASSIGN(Rule); | 129 DISALLOW_COPY_AND_ASSIGN(Rule); |
113 }; | 130 }; |
114 | 131 |
115 } // namespace addressinput | 132 } // namespace addressinput |
116 } // namespace i18n | 133 } // namespace i18n |
117 | 134 |
118 #endif // I18N_ADDRESSINPUT_RULE_H_ | 135 #endif // I18N_ADDRESSINPUT_RULE_H_ |
OLD | NEW |