diff --git a/src/Parser/Chr.php b/src/Parser/Chr.php new file mode 100644 index 00000000..dba2bb13 --- /dev/null +++ b/src/Parser/Chr.php @@ -0,0 +1,54 @@ + + * @copyright Copyright (c) 2010-2013, Kristopher Wilson + * @license MIT + * + * @link http://github.com/mrkrstphr/php-gedcom + */ + +namespace Gedcom\Parser; + +class Chr extends \Gedcom\Parser\Component +{ + public static function parse(\Gedcom\Parser $parser) + { + $record = $parser->getCurrentLineRecord(); + $depth = (int) $record[0]; + + $parser->forward(); + + $chr = new \Gedcom\Record\Chr(); + + while (!$parser->eof()) { + $record = $parser->getCurrentLineRecord(); + $recordType = trim($record[1]); + $currentDepth = (int) $record[0]; + + if ($currentDepth <= $depth) { + $parser->back(); + break; + } + + switch ($recordType) { + case 'DATE': + $chr->setDate(trim($record[2])); + break; + case 'PLAC': + $chr->setPlac(trim($record[2])); + break; + default: + $parser->logUnhandledRecord(self::class.' @ '.__LINE__); + } + + $parser->forward(); + } + + return $chr; + } +} diff --git a/src/Parser/Indi.php b/src/Parser/Indi.php index a7968a1c..6105b095 100644 --- a/src/Parser/Indi.php +++ b/src/Parser/Indi.php @@ -78,6 +78,9 @@ public static function parse(\Gedcom\Parser $parser) break; case 'CENS': case 'CHR': + $chr = \Gedcom\Parser\Chr::parse($parser); + $indi->setChr($chr); + break; case 'CHRA': case 'CONF': case 'CREM': diff --git a/src/Parser/Indi/Attr.php b/src/Parser/Indi/Attr.php index 84431ae2..070d75a9 100644 --- a/src/Parser/Indi/Attr.php +++ b/src/Parser/Indi/Attr.php @@ -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($record[1]))); + $className = '\\Gedcom\\Record\\Indi\\'.ucfirst(strtolower(trim($record[1]))); $attr = new $className(); $attr->setType(trim($record[1])); diff --git a/src/Record/Birt.php b/src/Record/Birt.php index d9899814..d88809aa 100644 --- a/src/Record/Birt.php +++ b/src/Record/Birt.php @@ -27,6 +27,8 @@ class Birt extends \Gedcom\Record ]; public $date; + + public $month; public $year; @@ -42,8 +44,8 @@ public function setDate($date) { $this->dateFormatted = $this->getYear() .'-'. $this->getMonth() .'-'. substr("0{$this->getDay()}", -2); } else { - $this->year = $date; - $this->date = null; + $this->month = $this->getMonth(); + $this->year = $this->getYear(); } } diff --git a/src/Record/Buri.php b/src/Record/Buri.php index 468600f7..742c2840 100644 --- a/src/Record/Buri.php +++ b/src/Record/Buri.php @@ -28,6 +28,8 @@ class Buri extends \Gedcom\Record public $date; + public $month; + public $year; public $dateFormatted = null; @@ -42,9 +44,8 @@ public function setDate($date) { $this->dateFormatted = $this->getYear() .'-'. $this->getMonth() .'-'. substr("0{$this->getDay()}", -2); } else { - $this->dateFormatted = null; - $this->year = $date; - $this->date = null; + $this->month = $this->getMonth(); + $this->year = $this->getYear(); } } diff --git a/src/Record/Chr.php b/src/Record/Chr.php new file mode 100644 index 00000000..0019d9f0 --- /dev/null +++ b/src/Record/Chr.php @@ -0,0 +1,110 @@ + + * @copyright Copyright (c) 2010-2013, Kristopher Wilson + * @license MIT + * + * @link http://github.com/mrkrstphr/php-gedcom + */ + +namespace Gedcom\Record; + +use Gedcom\Record; + +/** + * Class Chan. + */ +class Chr extends \Gedcom\Record +{ + private $months = [ + 'JAN' => '01', 'FEB' => '02', 'MAR' => '03', 'APR' => '04', 'MAY' => '05', 'JUN' => '06', + 'JUL' => '07', 'AUG' => '08', 'SEP' => '09', 'OCT' => '10', 'NOV' => '11', 'DEC' => '12', + ]; + + public $date; + + public $dateFormatted = null; + + public $plac; + + public function setDate($date) { + $this->date = $date; + $this->dateFormatted = $this->getYear() .'-'. $this->getMonth() .'-'. substr("0{$this->getDay()}", -2); + } + + public function getDateFormatted() { + return $this->dateFormatted; + } + + public function getDate() { + return $this->date; + } + + public function setPlac($plac) { + $this->plac = $plac; + } + + public function getPlac() { + return $this->plac; + } + + public function getDay() + { + $record = explode(' ', $this->date); + if (!empty($record[0])) { + if ($this->isPrefix($record[0])) { + unset($record[0]); + } + if (count($record) > 0) { + $day = (int) reset($record); + if ($day >= 1 && $day <= 31) { + return $day; + } + } + } + + return null; + } + + public function getMonth() + { + $record = explode(' ', $this->date); + if (count($record) > 0) { + if ($this->isPrefix($record[0])) { + unset($record[0]); + } + foreach ($record as $part) { + if (isset($this->months[trim($part)])) { + return $this->months[trim($part)]; + } + } + } + + return null; + } + + public function getYear() + { + $record = explode(' ', $this->date); + if (count($record) > 0) { + if ($this->isPrefix($record[0])) { + unset($record[0]); + } + if (count($record) > 0) { + return (int) end($record); + } + } + + return null; + } + + private function isPrefix($datePart) + { + return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']); + } +} diff --git a/src/Record/Deat.php b/src/Record/Deat.php index bd7facf7..5c323100 100644 --- a/src/Record/Deat.php +++ b/src/Record/Deat.php @@ -28,6 +28,8 @@ class Deat extends \Gedcom\Record public $date; + public $month; + public $year; public $dateFormatted = null; @@ -44,8 +46,8 @@ public function setDate($date) { $this->dateFormatted = $this->getYear() .'-'. $this->getMonth() .'-'. substr("0{$this->getDay()}", -2); } else { - $this->year = $date; - $this->date = null; + $this->month = $this->getMonth(); + $this->year = $this->getYear(); } } diff --git a/src/Record/Indi.php b/src/Record/Indi.php index 8ccdc8fb..f2228f08 100644 --- a/src/Record/Indi.php +++ b/src/Record/Indi.php @@ -151,6 +151,8 @@ class Indi extends \Gedcom\Record implements Noteable, Objectable, Sourceable protected $buri; protected $deat; + + protected $chr; public function setBirt($birt) { $this->birt = $birt; @@ -176,6 +178,14 @@ public function getDeat() { return $this->deat; } + public function setChr($chr) { + $this->chr = $chr; + } + + public function getChr() { + return $this->chr; + } + /** * @param string $id *