1414
1515abstract class AbstractVCard
1616{
17+ public const RFC_LINE_BREAK = "\r\n" ;
18+
1719 public string $ version ;
1820
1921 public stdClass $ relevantData ;
@@ -418,19 +420,28 @@ public function getX(string $name, bool $multiple = false): mixed
418420
419421 public function toString (): string
420422 {
421- $ vCardString = 'BEGIN:VCARD ' .PHP_EOL ;
422- $ vCardString .= 'VERSION: ' .$ this ->version .PHP_EOL ;
423+ $ lines = [
424+ 'BEGIN:VCARD ' ,
425+ 'VERSION: ' .$ this ->version ,
426+ ];
423427
424428 foreach ($ this ->properties as $ name => $ property ) {
425429 if ($ name == 'version ' ) {
426430 continue ;
427431 }
428- $ vCardString .= (string ) $ property .PHP_EOL ;
432+
433+ $ propertyString = trim ((string ) $ property );
434+
435+ if ($ propertyString === '' ) {
436+ continue ;
437+ }
438+
439+ $ lines [] = $ propertyString ;
429440 }
430441
431- $ vCardString . = 'END:VCARD ' ;
442+ $ lines [] = 'END:VCARD ' ;
432443
433- return $ vCardString ;
444+ return implode ( self :: RFC_LINE_BREAK , $ lines ) ;
434445 }
435446
436447 public function __toString (): string
@@ -441,7 +452,7 @@ public function __toString(): string
441452 public function export (string $ filePath ): void
442453 {
443454 try {
444- $ fp = fopen ($ filePath , 'w ' );
455+ $ fp = fopen ($ filePath , 'wb ' );
445456 fwrite ($ fp , $ this ->toString ());
446457 fclose ($ fp );
447458 } catch (Throwable $ e ) {
0 commit comments