Skip to content

Commit

Permalink
Merge pull request #63 from albarin/add-support-for-extension-tags
Browse files Browse the repository at this point in the history
Add support for extension tags
  • Loading branch information
curtisdelicata authored Dec 22, 2024
2 parents 2acf76f + 2321920 commit 01d5abe
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/Parser/Fam.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public static function parse(\Gedcom\Parser $parser)
break;

default:
$parser->logUnhandledRecord(self::class.' @ '.__LINE__);
if (strpos($recordType, '_') === 0) {
$fam->addExtensionTag($recordType, $record[2]);
}

$parser->logUnhandledRecord(self::class . ' @ ' . __LINE__);
}

$parser->forward();
Expand Down
10 changes: 10 additions & 0 deletions src/Record/Extendable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Gedcom\Record;

interface Extendable
{
public function addExtensionTag(string $tag, string $value);

public function getExtensionTag(string $tag): string;
}
22 changes: 21 additions & 1 deletion src/Record/Fam.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Gedcom\Record;

class Fam extends \Gedcom\Record implements Noteable, Sourceable, Objectable
class Fam extends \Gedcom\Record implements Noteable, Sourceable, Objectable, Extendable
{
protected $_id;

Expand Down Expand Up @@ -47,6 +47,8 @@ class Fam extends \Gedcom\Record implements Noteable, Sourceable, Objectable

protected $_obje = [];

protected $_extensiontags = [];

public function addEven($recordType, $even)
{
if (!array_key_exists($recordType, $this->_even)) {
Expand Down Expand Up @@ -106,4 +108,22 @@ public function addObje($obje = [])
{
$this->_obje[] = $obje;
}

public function addExtensionTag($tag, $value)
{
if (strpos($tag, '_') !== 0) {
$tag = "_$tag";
}

$this->_extensiontags[$tag] = $value;
}

public function getExtensionTag(string $tag): string
{
if (!isset($this->_extensiontags["_$tag"])) {
return '';
}

return $this->_extensiontags["_$tag"];
}
}
10 changes: 10 additions & 0 deletions src/Writer/Fam.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public static function convert(\Gedcom\Record\Fam &$fam, $level = 0)
}
}

// Custom tags
$extensionTags = $fam->getExtensionTags();
if (!empty($extensionTags) && (is_countable($extensionTags) ? count($extensionTags) : 0) > 0) {
foreach ($extensionTags as $tag => $value) {
if ($value) {
$output .= $level . ' ' . $tag . ' ' . $value . "\n";
}
}
}

return $output;
}
}
23 changes: 18 additions & 5 deletions tests/library/Gedcom/FamParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setUp(): void
*/
public function testFamilyEventWithNoTypeIsParsed()
{
$this->gedcom = $this->parser->parse(\TEST_DIR.'/stresstestfiles/family/family_event_no_type.ged');
$this->gedcom = $this->parser->parse(\TEST_DIR . '/stresstestfiles/family/family_event_no_type.ged');

$fam = $this->gedcom->getFam('F1');

Expand All @@ -48,7 +48,7 @@ public function testFamilyEventWithNoTypeIsParsed()
*/
public function testFamilyEventWithTypeIsParsed()
{
$this->gedcom = $this->parser->parse(\TEST_DIR.'/stresstestfiles/family/family_event_with_type.ged');
$this->gedcom = $this->parser->parse(\TEST_DIR . '/stresstestfiles/family/family_event_with_type.ged');

$fam = $this->gedcom->getFam('F1');

Expand All @@ -67,7 +67,7 @@ public function testFamilyEventWithTypeIsParsed()
*/
public function testMultipleEventsOfTheSameTypeAreKept()
{
$this->gedcom = $this->parser->parse(\TEST_DIR.'/stresstestfiles/family/family_multiple_events.ged');
$this->gedcom = $this->parser->parse(\TEST_DIR . '/stresstestfiles/family/family_multiple_events.ged');
$fam = $this->gedcom->getFam('F1');

$events = $fam['F1']->getAllEven();
Expand All @@ -88,7 +88,7 @@ public function testMultipleEventsOfTheSameTypeAreKept()
*/
public function testGetEvenReturnsASingleEvent()
{
$this->gedcom = $this->parser->parse(\TEST_DIR.'/stresstestfiles/family/family_event_with_type.ged');
$this->gedcom = $this->parser->parse(\TEST_DIR . '/stresstestfiles/family/family_event_with_type.ged');

$fam = $this->gedcom->getFam('F1');

Expand All @@ -103,7 +103,7 @@ public function testGetEvenReturnsASingleEvent()
*/
public function testGetEvenReturnsMultipleEvents()
{
$this->gedcom = $this->parser->parse(\TEST_DIR.'/stresstestfiles/family/family_multiple_events.ged');
$this->gedcom = $this->parser->parse(\TEST_DIR . '/stresstestfiles/family/family_multiple_events.ged');

$fam = $this->gedcom->getFam('F1');

Expand All @@ -118,4 +118,17 @@ public function testGetEvenReturnsMultipleEvents()
$this->assertEquals('First civil marriage', $event1->getType());
$this->assertEquals('Second civil marriage', $event2->getType());
}

/**
* Test a family event with a custom tag is parsed.
*/
public function testFamilyEventWithExtensionTagIsParsed()
{
$this->gedcom = $this->parser->parse(\TEST_DIR . '/stresstestfiles/family/family_with_extension_tag.ged');

$fam = $this->gedcom->getFam('F1');

$extensionTag = $fam['F1']->getExtensionTag('NAME');
$this->assertEquals('A random family', $extensionTag);
}
}
7 changes: 4 additions & 3 deletions tests/library/Gedcom/FamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function testFamilyEventIsConvertedToTheOriginal($gedcomFile)
public static function families()
{
return [
[\TEST_DIR.'/stresstestfiles/family/family_event_no_type.ged'],
[\TEST_DIR.'/stresstestfiles/family/family_event_with_type.ged'],
[\TEST_DIR.'/stresstestfiles/family/family_multiple_events.ged'],
[\TEST_DIR . '/stresstestfiles/family/family_event_no_type.ged'],
[\TEST_DIR . '/stresstestfiles/family/family_event_with_type.ged'],
[\TEST_DIR . '/stresstestfiles/family/family_multiple_events.ged'],
[\TEST_DIR . '/stresstestfiles/family/family_with_extension_tag.ged'],
];
}
}
3 changes: 3 additions & 0 deletions tests/stresstestfiles/family/family_with_extension_tag.ged
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 @F1@ FAM
1 _NAME A random family
0 TRLR

0 comments on commit 01d5abe

Please sign in to comment.