Commit c0504b7d authored by David Maus's avatar David Maus
Browse files

Set and get containing parent records

* php/HAB/Pica/Record/TitleRecord.php (removeLocalRecord)
(addLocalRecord): Set and unset parent record reference.
* php/HAB/Pica/Record/LocalRecord.php (getTitleRecord)
(unsetTitleRecord, setTitleRecord): New functions. Set, get, unset
containing title record.
(addCopyRecord, removeCopyRecord): Set and unset parent record
reference.
* php/HAB/Pica/Record/CopyRecord.php (getLocalRecord)
(unsetLocalRecord, setLocalRecord): New functions. Set, get, unset
containing local record.
parent 80d360b1
...@@ -125,4 +125,42 @@ class CopyRecord extends Record { ...@@ -125,4 +125,42 @@ class CopyRecord extends Record {
$this->append(new Field('203@', $this->getItemNumber(), array(new Subfield('0', $epn)))); $this->append(new Field('203@', $this->getItemNumber(), array(new Subfield('0', $epn))));
} }
} }
/**
* Set the containing local record.
*
* @param \HAB\Pica\Record\LocalRecord $record Local record
* @return void
*/
public function setLocalRecord (\HAB\Pica\Record\LocalRecord $record) {
$this->unsetLocalRecord();
if (!$record->containsCopyRecord($this)) {
$record->addCopyRecord($this);
}
$this->_parent = $record;
}
/**
* Unset the containing local record.
*
* @return void
*/
public function unsetLocalRecord () {
if ($this->_parent) {
if ($this->_parent->containsCopyRecord($this)) {
$this->_parent->removeCopyRecord($this);
}
$this->_parent = null;
}
}
/**
* Return the containing local record.
*
* @return \HAB\Pica\Record\LocalRecord|null
*/
public function getLocalRecord () {
return $this->_parent;
}
} }
\ No newline at end of file
...@@ -68,6 +68,7 @@ class LocalRecord extends NestedRecord { ...@@ -68,6 +68,7 @@ class LocalRecord extends NestedRecord {
throw new \InvalidArgumentException("Cannot add copy record: Copy record with item number {$record->getItemNumber()} already present"); throw new \InvalidArgumentException("Cannot add copy record: Copy record with item number {$record->getItemNumber()} already present");
} }
$this->addRecord($record); $this->addRecord($record);
$record->setLocalRecord($this);
} }
/** /**
...@@ -79,6 +80,7 @@ class LocalRecord extends NestedRecord { ...@@ -79,6 +80,7 @@ class LocalRecord extends NestedRecord {
*/ */
public function removeCopyRecord (\HAB\Pica\Record\CopyRecord $record) { public function removeCopyRecord (\HAB\Pica\Record\CopyRecord $record) {
$this->removeRecord($record); $this->removeRecord($record);
$record->unsetLocalRecord();
} }
/** /**
...@@ -131,6 +133,43 @@ class LocalRecord extends NestedRecord { ...@@ -131,6 +133,43 @@ class LocalRecord extends NestedRecord {
return $this->containsRecord($record); return $this->containsRecord($record);
} }
/**
* Set the containing title record.
*
* @param \HAB\Pica\Record\TitleRecord $record Title record
* @return void
*/
public function setTitleRecord (\HAB\Pica\Record\TitleRecord $record) {
$this->unsetTitleRecord();
if (!$record->containsLocalRecord($this)) {
$record->addLocalRecord($this);
}
$this->_parent = $record;
}
/**
* Unset the containing title record.
*
* @return void
*/
public function unsetTitleRecord () {
if ($this->_parent) {
if ($this->_parent->containsLocalRecord($this)) {
$this->_parent->removeLocalRecord($this);
}
$this->_parent = null;
}
}
/**
* Return the containing local record.
*
* @return \HAB\Pica\Record\TitleRecord|null
*/
public function getTitleRecord () {
return $this->_parent;
}
/** /**
* Compare two copy records. * Compare two copy records.
* *
...@@ -146,4 +185,5 @@ class LocalRecord extends NestedRecord { ...@@ -146,4 +185,5 @@ class LocalRecord extends NestedRecord {
protected function compareRecords (\HAB\Pica\Record\Record $a, \HAB\Pica\Record\Record $b) { protected function compareRecords (\HAB\Pica\Record\Record $a, \HAB\Pica\Record\Record $b) {
return $a->getItemNumber() - $b->getItemNumber(); return $a->getItemNumber() - $b->getItemNumber();
} }
} }
\ No newline at end of file
...@@ -103,6 +103,7 @@ class TitleRecord extends NestedRecord { ...@@ -103,6 +103,7 @@ class TitleRecord extends NestedRecord {
*/ */
public function addLocalRecord (\HAB\Pica\Record\LocalRecord $record) { public function addLocalRecord (\HAB\Pica\Record\LocalRecord $record) {
$this->addRecord($record); $this->addRecord($record);
$record->setTitleRecord($this);
} }
/** /**
...@@ -114,6 +115,7 @@ class TitleRecord extends NestedRecord { ...@@ -114,6 +115,7 @@ class TitleRecord extends NestedRecord {
*/ */
public function removeLocalRecord (\HAB\Pica\Record\LocalRecord $record) { public function removeLocalRecord (\HAB\Pica\Record\LocalRecord $record) {
$this->removeRecord($record); $this->removeRecord($record);
$record->unsetTitleRecord();
} }
/** /**
......
...@@ -42,6 +42,17 @@ class CopyRecordTest extends \PHPUnit_FrameWork_TestCase { ...@@ -42,6 +42,17 @@ class CopyRecordTest extends \PHPUnit_FrameWork_TestCase {
$this->assertEquals('epn', $r->getEPN()); $this->assertEquals('epn', $r->getEPN());
} }
public function testLocalRecordReference () {
$l = new LocalRecord();
$c = new CopyRecord();
$this->assertNull($c->getLocalRecord());
$l->addCopyRecord($c);
$this->assertSame($l, $c->getLocalRecord());
$c->unsetLocalRecord();
$this->assertNull($c->getLocalRecord());
$this->assertFalse($l->containsCopyRecord($c));
}
/// ///
/** /**
......
...@@ -111,6 +111,17 @@ class LocalRecordTest extends \PHPUnit_FrameWork_TestCase { ...@@ -111,6 +111,17 @@ class LocalRecordTest extends \PHPUnit_FrameWork_TestCase {
$this->assertTrue($r->containsCopyRecord($c)); $this->assertTrue($r->containsCopyRecord($c));
} }
public function testTitleRecordReference () {
$t = new TitleRecord();
$l = new LocalRecord();
$this->assertNull($l->getTitleRecord());
$t->addLocalRecord($l);
$this->assertSame($t, $l->getTitleRecord());
$l->unsetTitleRecord();
$this->assertNull($l->getTitleRecord());
$this->assertFalse($t->containsLocalRecord($l));
}
/// ///
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment