Skip to content

Commit

Permalink
more bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshKisb committed May 11, 2024
1 parent fc905f2 commit b41b825
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 84 deletions.
45 changes: 24 additions & 21 deletions src/Models/RecordInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

namespace Gedcom\Models;

Expand All @@ -10,28 +10,31 @@
*/
interface RecordInterface
{
/**
* Retrieves the ID of the record.
*
* @return mixed The ID of the record.
*/
public function getId();

/**
* Sets the ID of the record.
*
* @param mixed $id The new ID of the record.
*/
public function setId($id);

/**
* Retrieves the name of the record.
*
* @return string The name of the record.
*/
public function getName();

/**
* Sets the name of the record.
*
* @param string $name The new name of the record.
*/
public function setName($name);
}
/**
* Retrieves the ID of the record.
*
* @return mixed The ID of the record.
*/
/**
* Sets the ID of the record.
*
* @param mixed $id The new ID of the record.
*/
/**
* Retrieves the name of the record.
*
* @return string The name of the record.
*/
/**
* Sets the name of the record.
*
* @param string $name The new name of the record.
*/
3 changes: 2 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public function parse($fileName)
$this->_file = fopen($fileName, 'r'); //explode("\n", mb_convert_encoding($contents, 'UTF-8'));

if (!$this->_file) {
error_log("Failed to open file: ". $fileName);
return null;
}

Expand All @@ -355,7 +356,7 @@ public function parse($fileName)
}

$depth = (int) $record[0];

// We only process 0 level records here. Sub levels are processed
// in methods for those data types (individuals, sources, etc)

Expand Down
4 changes: 4 additions & 0 deletions src/Parser/Indi.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static function parse(\Gedcom\Parser $parser)
break;
}

error_log("RECORD:".$recordType);

if ($recordType == 'BURI') {
$a = '';
}
Expand All @@ -59,6 +61,7 @@ public static function parse(\Gedcom\Parser $parser)
case 'NAME':
$name = \Gedcom\Parser\Indi\Name::parse($parser);
$indi->addName($name);
error_log("NAME:".$name->getName());
break;
case 'SEX':
$indi->setSex(isset($record[2]) ? trim((string) $record[2]) : '');
Expand Down Expand Up @@ -199,6 +202,7 @@ public static function parse(\Gedcom\Parser $parser)
$parser->forward();
}

error_log("VAL: ".json_encode($indi->getName()));
return $indi;
}
}
2 changes: 1 addition & 1 deletion src/Parser/Indi/Lds.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function parse(\Gedcom\Parser $parser)
$record = $parser->getCurrentLineRecord();
$depth = (int) $record[0];
if (isset($record[1])) {
$className = 'GedcomRecordIndi'.ucfirst(strtolower(trim((string) $record[1])));
$className = '\\Gedcom\\Record\\Indi\\'.ucfirst(strtolower(trim((string) $record[1])));
$lds = new $className();
} else {
$parser->skipToNextLevel($depth);
Expand Down
106 changes: 46 additions & 60 deletions src/Record.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* php-gedcom.
*
Expand All @@ -21,6 +22,11 @@
*/
abstract class Record implements \Gedcom\Models\RecordInterface
{

private $_id;

private $_name;

/**
* Retrieves the ID of the record.
*
Expand All @@ -31,69 +37,57 @@ public function getId()
return $this->_id ?? null;
}

/**
* Sets the ID of the record.
*
* @param mixed $id The new ID of the record.
*/
public function setId($id)
{
$this->_id = $id;
}

/**
* Retrieves the name of the record.
*
* @return string|null The name of the record, or null if not set.
*/
public function getName()
{
return $this->_name ?? null;
}

public function setName($name)
/**
* Sets the ID of the record.
*
* @param mixed $id The new ID of the record.
*/
/**
* Retrieves the name of the record.
*
* @return string|null The name of the record, or null if not set.
*/
/**
* Sets the name of the record.
*
* @param string $name The new name of the record.
*/

public function setId($id)
{
$this->_id = $id;
}

public function getName()
{
return $this->_name ?? null;
}

/**
* Sets the name of the record.
*
* @param string $name The new name of the record.
*/
public function setName($name)
{
$this->_name = $name;
}
{

public function __call($method, $args)
{
if (str_starts_with((string) $method, 'add')) {
$arr = strtolower(substr((string) $method, 3));

if (!property_exists($this, '_'.$arr) || !is_array($this->{'_'.$arr})) {
throw new \Exception('Unknown '.static::class.'::'.$arr);
if (!property_exists($this, '_' . $arr) || !is_array($this->{'_' . $arr})) {
throw new \Exception('Unknown ' . static::class . '::' . $arr);
}

if (!is_array($args)) {
throw new \Exception('Incorrect arguments to '.$method);
throw new \Exception('Incorrect arguments to ' . $method);
}

if (!isset($args[0])) {
// Argument can be empty since we trim it's value
return;
throw new \Exception('Unknown '.static::class.'::'.$arr);
throw new \Exception('Unknown ' . static::class . '::' . $arr);
}

if (!is_array($args)) {
throw new \Exception('Incorrect arguments to '.$method);
throw new \Exception('Incorrect arguments to ' . $method);
}

if (!isset($args[0])) {
Expand All @@ -105,18 +99,18 @@ public function __call($method, $args)
// Type safety?
}

$this->{'_'.$arr}[] = $args[0];
$this->{'_' . $arr}[] = $args[0];

return $this;
} elseif (str_starts_with((string) $method, 'set')) {
$arr = strtolower(substr((string) $method, 3));

if (!property_exists($this, '_'.$arr)) {
throw new \Exception('Unknown '.static::class.'::'.$arr);
if (!property_exists($this, '_' . $arr)) {
throw new \Exception('Unknown ' . static::class . '::' . $arr);
}

if (!is_array($args)) {
throw new \Exception('Incorrect arguments to '.$method);
throw new \Exception('Incorrect arguments to ' . $method);
}

if (!isset($args[0])) {
Expand All @@ -128,7 +122,7 @@ public function __call($method, $args)
// Type safety?
}

$this->{'_'.$arr} = $args[0];
$this->{'_' . $arr} = $args[0];

return $this;
} elseif (str_starts_with((string) $method, 'get')) {
Expand All @@ -137,26 +131,33 @@ public function __call($method, $args)
// hotfix getData
if ('data' == $arr) {
if (!property_exists($this, '_text')) {
throw new \Exception('Unknown '.static::class.'::'.$arr);
throw new \Exception('Unknown ' . static::class . '::' . $arr);
}

return $this->{'_text'};
}

if (!property_exists($this, '_'.$arr)) {
throw new \Exception('Unknown '.static::class.'::'.$arr);
if (!property_exists($this, '_' . $arr)) {
throw new \Exception('Unknown ' . static::class . '::' . $arr);
}

return $this->{'_'.$arr};
return $this->{'_' . $arr};
} else {
throw new \Exception('Unknown method called: '.$method);
throw new \Exception('Unknown method called: ' . $method);
}
}

/**
* Magic method to prevent setting of undefined properties.
*
* @param string $var The name of the property being set.
* @param mixed $val The value being assigned to the property.
* @throws \Exception Always thrown to indicate an undefined property.
*/
public function __set($var, $val)
{
// this class does not have any public vars
throw new \Exception('Undefined property '.self::class.'::'.$var);
throw new \Exception('Undefined property ' . self::class . '::' . $var);
}

/**
Expand All @@ -169,21 +170,6 @@ public function __set($var, $val)
*/
public function hasAttribute($var)
{
return property_exists($this, '_'.$var) || property_exists($this, $var);
return property_exists($this, '_' . $var) || property_exists($this, $var);
}
}
/**
* Magic method to prevent setting of undefined properties.
*
* @param string $var The name of the property being set.
* @param mixed $val The value being assigned to the property.
* @throws \Exception Always thrown to indicate an undefined property.
*/
/**
* Checks if this GEDCOM object has the provided attribute.
*
* Determines if the specified attribute exists below the current object in its tree.
*
* @param string $var The name of the attribute.
* @return bool True if this object has the provided attribute, false otherwise.
*/
2 changes: 1 addition & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private function generateOutputFromParsedGedcom($gedcomFileName)
$parser = new Parser();
$gedcom = $parser->parse($gedcomFileName);
$output = '';

error_log(json_encode($gedcom));
ob_start();
foreach ($gedcom->getIndi() as $individual) {
$names = $individual->getName();
Expand Down

0 comments on commit b41b825

Please sign in to comment.