@@ -196,7 +196,7 @@ public function addSupportedBrand($name, $expression)
196196 * @param array $parameters An associative array of parameters
197197 * @return CreditCard provides a fluent interface.
198198 */
199- public function initialize ($ parameters = null )
199+ public function initialize (array $ parameters = null )
200200 {
201201 $ this ->parameters = new ParameterBag ;
202202
@@ -595,35 +595,36 @@ public function getTracks()
595595 /**
596596 * Get raw data for track 1 on the credit card magnetic strip.
597597 *
598- * @return string
598+ * @return string|null
599599 */
600600 public function getTrack1 ()
601601 {
602- $ track1 = null ;
603- if ($ tracks = $ this ->getTracks ()) {
604- $ pattern = '/\%B\d{1,19}\^.{2,26}\^\d{4}\d*\?/ ' ;
605- if (preg_match ($ pattern , $ tracks , $ matches ) === 1 ) {
606- $ track1 = $ matches [0 ];
607- }
608- }
609- return $ track1 ;
602+ return $ this ->getTrackByPattern ('/\%B\d{1,19}\^.{2,26}\^\d{4}\d*\?/ ' );
610603 }
611604
612605 /**
613606 * Get raw data for track 2 on the credit card magnetic strip.
614607 *
615- * @return string
608+ * @return string|null
616609 */
617610 public function getTrack2 ()
618611 {
619- $ track2 = null ;
612+ return $ this ->getTrackByPattern ('/;\d{1,19}=\d{4}\d*\?/ ' );
613+ }
614+
615+ /**
616+ * Get raw data for a track on the credit card magnetic strip based on the pattern for track 1 or 2.
617+ *
618+ * @param $pattern
619+ * @return string|null
620+ */
621+ protected function getTrackByPattern ($ pattern )
622+ {
620623 if ($ tracks = $ this ->getTracks ()) {
621- $ pattern = '/;\d{1,19}=\d{4}\d*\?/ ' ;
622624 if (preg_match ($ pattern , $ tracks , $ matches ) === 1 ) {
623- $ track2 = $ matches [0 ];
625+ return $ matches [0 ];
624626 }
625627 }
626- return $ track2 ;
627628 }
628629
629630 /**
@@ -690,6 +691,19 @@ public function getBillingName()
690691 return trim ($ this ->getBillingFirstName () . ' ' . $ this ->getBillingLastName ());
691692 }
692693
694+ /**
695+ * Split the full name in the first and last name.
696+ *
697+ * @param $fullName
698+ * @return array with first and lastname
699+ */
700+ protected function listFirstLastName ($ fullName )
701+ {
702+ $ names = explode (' ' , $ fullName , 2 );
703+
704+ return [$ names [0 ], isset ($ names [1 ]) ? $ names [1 ] : null ];
705+ }
706+
693707 /**
694708 * Sets the card billing name.
695709 *
@@ -698,9 +712,10 @@ public function getBillingName()
698712 */
699713 public function setBillingName ($ value )
700714 {
701- $ names = explode (' ' , $ value , 2 );
715+ $ names = $ this ->listFirstLastName ($ value );
716+
702717 $ this ->setBillingFirstName ($ names [0 ]);
703- $ this ->setBillingLastName (isset ( $ names [1 ]) ? $ names [ 1 ] : null );
718+ $ this ->setBillingLastName ($ names [1 ]);
704719
705720 return $ this ;
706721 }
@@ -996,9 +1011,10 @@ public function getShippingName()
9961011 */
9971012 public function setShippingName ($ value )
9981013 {
999- $ names = explode (' ' , $ value , 2 );
1014+ $ names = $ this ->listFirstLastName ($ value );
1015+
10001016 $ this ->setShippingFirstName ($ names [0 ]);
1001- $ this ->setShippingLastName (isset ( $ names [1 ]) ? $ names [ 1 ] : null );
1017+ $ this ->setShippingLastName ($ names [1 ]);
10021018
10031019 return $ this ;
10041020 }
0 commit comments