~aleteoryx/muditaos

muditaos/module-utils/phonenumber/PhoneNumber.hpp -rw-r--r-- 12.7 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include "country.hpp"

#include <phonenumbers/phonenumberutil.h>

#include <utf8/UTF8.hpp>

namespace utils
{

    /**
     * @brief Phone number representation. It wraps calls to Google's libphonenumber
     * to provide more convienient API. Because operations on phone numbers are
     * substantially resource hungry (in terms of stack and cpu time usage)
     * a lightweight representation of class' data has been provided (View).
     *
     * There are two states of phone number: valid or invalid which means that
     * libphonenumber was or wasn't able to parse provided phone number. It is
     * not possible to determine if the number provided is valid if it is not
     * possible to determine correct country for number so this flag is just a
     * if you can expect E164 number format to be present and meaningful.
     */
    class PhoneNumber
    {
      private:
        using errCode    = ::i18n::phonenumbers::PhoneNumberUtil::ErrorType;
        using formatType = ::i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat;

      public:
        /**
         * @brief Number type enumeration, e.g. fixed line, mobile, etc.
         */
        using numberType = ::i18n::phonenumbers::PhoneNumberUtil::PhoneNumberType;

        /**
         * @brief Phone Number match type:
         *  - EXACT - 100% match (match on E164),
         *  - POSSIBLE - highly likely that numbers match, e.g. one number is in
         *    national format with unknown country code and the other is the
         *    same but has country code.
         *  - PROBABLE - less likely than POSSIBLE but not impossible, e.g. one
         *    number match ending of another number
         *  - NO_MATCH - no match possible
         * number without country code matches same number with country code.
         *
         */
        enum class Match
        {
            NO_MATCH,
            PROBABLE,
            POSSIBLE,
            EXACT
        };

        /**
         * @brief Phone number processing error. Holds reason and string
         * representation of a problematic phone number.
         *
         */
        class Error : public std::exception
        {
          private:
            std::string number;
            std::string reason;

          public:
            Error() = delete;

            /**
             * @brief Construct a new PhoneNumber exception object.
             *
             * @param number - phone number which was processed when an exception occurred
             * @param reason - fail reason
             */
            Error(const std::string &number, const std::string &reason);

            /**
             * @brief Get exception reason
             *
             * @return exception reason
             */
            const char *what() const noexcept override;

            /**
             * @brief Get the phone number related to the exception
             *
             * @return string represenation of a number
             */
            const std::string &getNumber() const;
        };

        /**
         * @brief A lightweight representation of PhoneNumber class. It does not
         * wrap calls to libphonenumber and contains precomputed values only.
         * It is not possible to create an instance of View from outside of
         * PhoneNumber class, which allows to maintain object integrity, i.e.
         * it is not possible to create View with two completely different
         * numbers.
         */
        class View
        {
          public:
            /**
             * @brief Creates empty (null) phone number. It is \a invalid and
             * contains empty strings.
             */
            explicit View() = default;

            /**
             * @brief Default copy constructor.
             *
             * @param view - object to copy from
             */
            View(const View &view) = default;

            /**
             * @brief Default assignment operator.
             *
             * @param view - object to assign from
             * @return View& - reference to self
             */
            View &operator=(const View &view) = default;

            /**
             * @brief Resets state - i.e. turns object into null number (see
             * View()).
             *
             */
            void clear();

            /**
             * @brief A convenience method to get a non empty number.
             *
             * @return E164 number if it has one, entered number otherwise.
             */
            const std::string &getNonEmpty() const;

            /**
             * @brief Getter for the entered number - number parsed from.
             *
             * @return const std::string& - reference to entered number
             */
            const std::string &getEntered() const;

            /**
             * @brief Getter for E164 number representation.
             *
             * @return const std::string& - E164 number representation. Is empty
             * if number is not \a valid (see isValid()).
             */
            const std::string &getE164() const;

            /**
             * @brief Getter for formatted number representation. It is intended
             * to be presented to the user.
             *
             * @return const std::string& - formatted number representation. If
             * number is not \a valid (see isValid()) it is equal to the entered
             * number (see getEntered()).
             */
            const std::string &getFormatted() const;

            /**
             * @brief Object state validator.
             *
             * @return true if number is valid, i.e. when it was successfully
             * parsed using provided country identification.
             *
             * @return false if number is not valid, i.e. when libphonenumber
             * failed to parse number using provided country identification.
             */
            bool isValid() const;

            /**
             * @brief Compares two View objects
             *
             * @param rhs - instance of a VIew to compare to.
             * @return true if objects are equal.
             * @return false if objects are different (not equal).
             */
            bool operator==(const View &rhs) const;

          private:
            View(const std::string &enteredNumber,
                 const std::string &formattedNumber,
                 const std::string &e164Number,
                 bool valid);
            std::string entered   = "";
            std::string formatted = "";
            std::string e164      = "";
            bool valid            = false;

            friend class PhoneNumber;
        };

      private:
        using phn_util = ::i18n::phonenumbers::PhoneNumberUtil;
        using gnumber  = ::i18n::phonenumbers::PhoneNumber;

      public:
        /**
         * @brief Construct an empty PhoneNumber object
         *
         */
        PhoneNumber() = default;

        /**
         * @brief Creates PhoneNumber from a string.
         *
         * @param phoneNumber - phone number provided by the user
         * @param defaultCountryCode - if number is local this country will be
         * used when generating E164 format. If phoneNumber format does not
         * match local number format for the country identified by
         * defaultCountryCode the object will be invalid (see isValid()).
         */
        PhoneNumber(const std::string &phoneNumber, country::Id defaultCountryCode = country::defaultCountry);

        /**
         * @brief Construct a new Phone Number object from its raw and e164
         * representations checking if they match in the process.
         *
         * If e164 is empty PhoneNumber is constructed based on entered number
         * only.
         *
         * Country code is retrieved from E164 number format if possible.
         *
         * @param phoneNumber - phone number provided by the user
         * @param e164number - E164 number representation
         *
         * \throws PhoneNumber::Error when numbers do not match or a parsing error
         * occurs.
         */
        PhoneNumber(const std::string &phoneNumber, const std::string &e164number);

        /**
         * @brief Construct a PhoneNumber object from View representation.
         *
         * @param numberView - lightweight number representation
         */
        PhoneNumber(const View &numberView);

        /**
         * @brief Creates E164 number representation.
         *
         * @return std::string - E164 number representation.
         */
        std::string toE164() const;

        /**
         * @brief Gets orignal number as provided to the constructor.
         *
         * @return const std::string& - number used to construct PhoneNumber
         * instance.
         */
        const std::string &get() const;

        /**
         * @brief Gets string representation of a number formatted for the end
         * user.
         *
         * @return std::string - formatted string.
         */
        std::string getFormatted() const;

        /**
         * @brief Get detected number type (mobile, fixed line, etc.).
         *
         * @return numberType - number type
         */
        numberType getType() const;

        /**
         * @brief Get the country code for number
         *
         * @return country::Id
         */
        country::Id getCountryCode() const noexcept;

        /**
         * @brief Get lightweight representation of a PhoneNumber (see View class).
         *
         * @return View - instance of View object which represents state of
         * PhoneNumber instance.
         */
        const View &getView() const;

        /**
         * @brief Returns validity state, i.e. if number has been successfully
         * parsed when local number had been provided.
         *
         * @return true if number is valid
         * @return false if number is not valid
         */
        bool isValid() const;

        /**
         * @brief Compare current instance with an other number.
         *
         * @param other - number to compare
         * @return type of match, Match::NO_MATCH when number do not match at all
         */
        Match match(const PhoneNumber &other) const;

        /**
         * @brief Compares two PhoneNumber objects (exact match).
         *
         * @param right - instance of PhoneNumber to compare to.
         * @return true if objects are equal.
         * @return false if objects are different (not equal).
         */
        bool operator==(const PhoneNumber &right) const;

        /**
         * @brief Compares PhoneNumber with number represented by View.
         *
         * @param view - instance of View to compare to.
         * @return true if objects are equal.
         * @return false if objects are different (not equal).
         */
        bool operator==(const View &view) const;

        /**
         * @brief Convenience wrapper for E164 number formatting.
         *
         * @param number - number to be formatted
         * @param e164 - where to store the result
         * @param defaultCountryCode - default country code to be used if local
         * number has been provided. Can be country::Id::UNKNOWN if number has
         * been provided with international prefix.
         * @return true if it was possible to format number.
         * @return false if there was an error during number formatting.
         */
        static bool e164format(const std::string &number,
                               std::string &e164,
                               country::Id defaultCountryCode = country::defaultCountry);

        /**
         * @brief Check whether character must be removed
         *
         * @param c - a character to check
         * @return true is character is on the list
         */
        static bool CharacterToRemove(char c);

        /**
         * @brief Create instance of View directly from the E164 format.
         *
         * @param inputNumber - a string represenation of a number to create View from
         * @return View - phone number representation
         */
        static View parse(const std::string &inputNumber);

        /**
         * @brief Get lightweight representation of a PhoneNumber (see View class).
         *
         * @param received number - utf8 representation of received number
         * @return View - instance of View object which represents state of
         * PhoneNumber instance.
         */
        static const View getReceivedNumberView(const UTF8 &receivedNumber);

      private:
        View makeView(const std::string &input) const;

        country::Id countryCode = country::Id::UNKNOWN;
        gnumber pbNumber;
        View viewSelf;
    };
} // namespace utils