1515use libphonenumber \PhoneNumberFormat ;
1616use libphonenumber \PhoneNumberType ;
1717use libphonenumber \PhoneNumberUtil ;
18- use Misd \PhoneNumberBundle \Exception \ InvalidArgumentException ;
18+ use Misd \PhoneNumberBundle \Formatter \ PhoneNumberFormatter ;
1919use Symfony \Component \Templating \Helper \HelperInterface ;
2020
2121/**
2424class PhoneNumberHelper implements HelperInterface
2525{
2626 /**
27- * Phone number utility .
27+ * Phone number formatter .
2828 *
29- * @var PhoneNumberUtil
29+ * @var PhoneNumberFormatter
3030 */
31- protected $ phoneNumberUtil ;
31+ protected $ formatter ;
3232
3333 /**
3434 * Charset.
@@ -40,11 +40,15 @@ class PhoneNumberHelper implements HelperInterface
4040 /**
4141 * Constructor.
4242 *
43- * @param PhoneNumberUtil $phoneNumberUtil Phone number utility .
43+ * @param PhoneNumberFormatter $formatter Phone number formatter .
4444 */
45- public function __construct (PhoneNumberUtil $ phoneNumberUtil )
45+ public function __construct ($ formatter )
4646 {
47- $ this ->phoneNumberUtil = $ phoneNumberUtil ;
47+ if ($ formatter instanceof PhoneNumberUtil) {
48+ // throw deprecation message
49+ $ formatter = new PhoneNumberFormatter ($ formatter );
50+ }
51+ $ this ->formatter = $ formatter ;
4852 }
4953
5054 /**
@@ -78,44 +82,20 @@ public function getName()
7882 * @param int|string $format Format, or format constant name.
7983 *
8084 * @return string Formatted phone number.
81- *
82- * @throws InvalidArgumentException If an argument is invalid.
8385 */
8486 public function format (PhoneNumber $ phoneNumber , $ format = PhoneNumberFormat::INTERNATIONAL )
8587 {
86- if (true === is_string ($ format )) {
87- $ constant = '\libphonenumber\PhoneNumberFormat:: ' . $ format ;
88-
89- if (false === defined ($ constant )) {
90- throw new InvalidArgumentException ('The format must be either a constant value or name in libphonenumber\PhoneNumberFormat ' );
91- }
92-
93- $ format = constant ('\libphonenumber\PhoneNumberFormat:: ' . $ format );
94- }
95-
96- return $ this ->phoneNumberUtil ->format ($ phoneNumber , $ format );
88+ return $ this ->formatter ->format ($ phoneNumber , $ format );
9789 }
9890
9991 /**
10092 * @param PhoneNumber $phoneNumber Phone number.
10193 * @param int|string $type PhoneNumberType, or PhoneNumberType constant name.
10294 *
10395 * @return bool
104- *
105- * @throws InvalidArgumentException If type argument is invalid.
10696 */
10797 public function isType (PhoneNumber $ phoneNumber , $ type = PhoneNumberType::UNKNOWN )
10898 {
109- if (true === is_string ($ type )) {
110- $ constant = '\libphonenumber\PhoneNumberType:: ' . $ type ;
111-
112- if (false === defined ($ constant )) {
113- throw new InvalidArgumentException ('The format must be either a constant value or name in libphonenumber\PhoneNumberType ' );
114- }
115-
116- $ type = constant ('\libphonenumber\PhoneNumberType:: ' . $ type );
117- }
118-
119- return $ this ->phoneNumberUtil ->getNumberType ($ phoneNumber ) === $ type ? true : false ;
99+ return $ this ->formatter ->getNumberType ($ phoneNumber ) === $ type ? true : false ;
120100 }
121101}
0 commit comments