1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* php-gedcom |
4
|
|
|
* |
5
|
|
|
* php-gedcom is a library for parsing, manipulating, importing and exporting |
6
|
|
|
* GEDCOM 5.5 files in PHP 5.3+. |
7
|
|
|
* |
8
|
|
|
* @author Kristopher Wilson <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2010-2013, Kristopher Wilson |
10
|
|
|
* @package php-gedcom |
11
|
|
|
* @license MIT |
12
|
|
|
* @link http://github.com/mrkrstphr/php-gedcom |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace PhpGedcom; |
16
|
|
|
|
17
|
|
|
use \PhpGedcom\Gedcom; |
18
|
|
|
use \PhpGedcom\Writer\Head; |
19
|
|
|
use \PhpGedcom\Writer\Subn; |
20
|
|
|
use \PhpGedcom\Writer\Subm; |
21
|
|
|
use \PhpGedcom\Writer\Sour; |
22
|
|
|
use \PhpGedcom\Writer\Indi; |
23
|
|
|
use \PhpGedcom\Writer\Fam; |
24
|
|
|
use \PhpGedcom\Writer\Note; |
25
|
|
|
use \PhpGedcom\Writer\Repo; |
26
|
|
|
use \PhpGedcom\Writer\Obje; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class Writer |
32
|
|
|
{ |
33
|
|
|
const GEDCOM55 = 'gedcom5.5'; |
34
|
|
|
|
35
|
|
|
protected $_output = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @param \PhpGedcom\Gedcom $gedcom The GEDCOM object |
40
|
|
|
* @param string $format The format to convert the GEDCOM object to |
41
|
|
|
* @return string The contents of the document in the converted format |
42
|
|
|
*/ |
43
|
|
|
public static function convert(Gedcom $gedcom, $format = self::GEDCOM55) |
44
|
|
|
{ |
45
|
|
|
$head = $gedcom->getHead(); |
46
|
|
|
$subn = $gedcom->getSubn(); |
47
|
|
|
$subms = $gedcom->getSubm(); // array() |
48
|
|
|
$sours = $gedcom->getSour(); // array() |
49
|
|
|
$indis = $gedcom->getIndi(); // array() |
50
|
|
|
$fams = $gedcom->getFam(); // array() |
51
|
|
|
$notes = $gedcom->getNote(); // array() |
52
|
|
|
$repos = $gedcom->getRepo(); // array() |
53
|
|
|
$objes = $gedcom->getObje(); // array() |
54
|
|
|
|
55
|
|
|
$output = "0 FORMAT ".$format."\n"; |
56
|
|
|
|
57
|
|
|
// head |
58
|
|
|
if($head){ |
|
|
|
|
59
|
|
|
$output = Head::convert($head, $format); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// subn |
63
|
|
|
if($subn){ |
|
|
|
|
64
|
|
|
$output .= Subn::convert($subn); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// subms |
68
|
|
|
if(!empty($subms) && count($subms) > 0){ |
69
|
|
|
foreach($subms as $item){ |
70
|
|
|
if($item){ |
71
|
|
|
$output .= Subm::convert($item); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// sours |
77
|
|
|
if(!empty($sours) && count($sours) > 0){ |
78
|
|
|
foreach($sours as $item){ |
79
|
|
|
if($item){ |
80
|
|
|
$output .= Sour::convert($item); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// indis |
86
|
|
|
if(!empty($indis) && count($indis) > 0){ |
87
|
|
|
foreach($indis as $item){ |
88
|
|
|
if($item){ |
89
|
|
|
$output .= Indi::convert($item); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// fams |
95
|
|
|
if(!empty($fams) && count($fams) > 0){ |
96
|
|
|
foreach($fams as $item){ |
97
|
|
|
if($item){ |
98
|
|
|
$output .= Fam::convert($item); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
// notes |
103
|
|
|
if(!empty($notes) && count($notes) > 0){ |
104
|
|
|
foreach($notes as $item){ |
105
|
|
|
if($item){ |
106
|
|
|
$output .= Note::convert($item); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// repos |
112
|
|
|
if(!empty($repos) && count($repos) > 0){ |
113
|
|
|
foreach($repos as $item){ |
114
|
|
|
if($item){ |
115
|
|
|
$output .= Repo::convert($item); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
// Objes |
120
|
|
|
if(!empty($objes) && count($objes) > 0){ |
121
|
|
|
foreach($objes as $item){ |
122
|
|
|
if($item){ |
123
|
|
|
$output .= Obje::convert($item); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
// EOF |
128
|
|
|
$output .= "0 TRLR\n"; |
129
|
|
|
return $output; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|