| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Z38\SwissPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This class holds a structured representation of a postal address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class StructuredPostalAddress implements PostalAddressInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $street; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $buildingNo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $postCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $town; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $country; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string|null $street     Street name or null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string|null $buildingNo Building number or null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string      $postCode   Postal code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string      $town       Town name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string      $country    Country code (ISO 3166-1 alpha-2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @throws \InvalidArgumentException When the address contains invalid characters or is too long. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |     public function __construct($street, $buildingNo, $postCode, $town, $country = 'CH') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |         $this->street = Text::assertOptional($street, 70); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |         $this->buildingNo = Text::assertOptional($buildingNo, 16); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         $this->postCode = Text::assert($postCode, 16); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         $this->town = Text::assert($town, 35); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |         $this->country = Text::assertCountryCode($country); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Creates a new instance after sanitizing all inputs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string|null $street     Street name or null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param string|null $buildingNo Building number or null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param string      $postCode   Postal code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string      $town       Town name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string      $country    Country code (ISO 3166-1 alpha-2) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |     public static function sanitize($street, $buildingNo, $postCode, $town, $country = 'CH') | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 1 |  |         return new self( | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |             Text::sanitizeOptional($street, 70), | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |             Text::sanitizeOptional($buildingNo, 16), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |             Text::sanitize($postCode, 16), | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |             Text::sanitize($town, 35), | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             $country | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |     public function asDom(\DOMDocument $doc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |         $root = $doc->createElement('PstlAdr'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         if ($this->street !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |             $root->appendChild(Text::xml($doc, 'StrtNm', $this->street)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |         if ($this->buildingNo !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |             $root->appendChild(Text::xml($doc, 'BldgNb', $this->buildingNo)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |         $root->appendChild(Text::xml($doc, 'PstCd', $this->postCode)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         $root->appendChild(Text::xml($doc, 'TwnNm', $this->town)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |         $root->appendChild(Text::xml($doc, 'Ctry', $this->country)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |         return $root; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  |