Skip to content

Commit b55f12a

Browse files
committed
Improved title extractor
1 parent 77fabc7 commit b55f12a

File tree

2 files changed

+159
-5
lines changed

2 files changed

+159
-5
lines changed

src/Fields/Title.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,44 @@
44

55
class Title extends Field
66
{
7+
8+
/**
9+
* See tests/TitleFieldTest.php for more info.
10+
*/
711
public function __toString()
812
{
13+
// $a is not repeated
914
$a = $this->field->getSubfield('a');
1015
if (!$a) {
1116
return null;
1217
}
13-
$a = $a->getData();
18+
$title = trim($a->getData());
19+
20+
// $b is not repeated
21+
$b = $this->field->getSubfield('b');
22+
if ($b) {
23+
if (!in_array(substr($title, strlen($title) -1), array(':', ';', '=', '.'))) {
24+
// Add colon if no ISBD marker present ("British style")
25+
$title .= ' :';
26+
}
27+
$title .= ' ' . trim($b->getData());
28+
}
29+
30+
// Part number and title can be repeated
31+
foreach ($this->field->getSubfields() as $sf) {
32+
if (in_array($sf->getCode(), array('n', 'p'))) {
33+
$title .= ' ' . $sf->getData();
34+
}
35+
}
1436

15-
// TODO:
16-
// foreach ($this->field->getSubfields('b') as $b) {
37+
// Strip off 'Statement of responsibility' marker
38+
// I would like to strip of the final dot as well, but we can't really distinguish
39+
// between dot as an ISBD marker and dot as part of the actual title
40+
// (for instance when the title is an abbreviation)
41+
$title = rtrim($title, ' /');
1742

18-
// }
43+
// TODO: Handle more subfields like $k, $f and $g ?? Probably should...
1944

20-
return $a;
45+
return $title;
2146
}
2247
}

tests/TitleFieldTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
use Scriptotek\Marc\Record;
4+
use Scriptotek\Marc\Fields\Title;
5+
6+
class TitleFieldTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/*
9+
In records formulated according to ISBD principles, subfield $b contains
10+
all the data following the first mark of ISBD punctuation and up to and
11+
including the mark of ISBD punctuation that introduces the first author
12+
statement (i.e., the first slash (/)) or precedes either the number
13+
(subfield $n) or the name (subfield $p) of a part/section of a work. Note
14+
that subfield $b is not repeated when more than one parallel title,
15+
subsequent title, and/or other title information are given in the field.
16+
17+
1. Title and statement of responsibility area:
18+
19+
= Parallel title R
20+
: Other title information R
21+
Statement of responsibility:
22+
/ First statement MA
23+
; Subsequent statement R
24+
; Subsequent title by same author, etc. MA R
25+
. Subsequent title by different author, etc. MA R
26+
*/
27+
28+
public function testIsbdUsStyle()
29+
{
30+
// ISBD style: US
31+
// Title components: [Main title, Other title information, Other title information]
32+
$field = new File_MARC_Data_Field('245', array(
33+
new File_MARC_Subfield('a', 'Eternal darkness :'),
34+
new File_MARC_Subfield('b', 'sanity\'s requiem : Prima\'s official strategy guide /'),
35+
new File_MARC_Subfield('c', 'the Stratton Bros.'),
36+
));
37+
$title = new Title($field);
38+
$this->assertEquals('Eternal darkness : sanity\'s requiem : Prima\'s official strategy guide', strval($title));
39+
}
40+
41+
public function testIsbdUkStyle()
42+
{
43+
// ISBD style: UK
44+
// Title components: [Main title, Other title information, Other title information]
45+
$field = new File_MARC_Data_Field('245', array(
46+
new File_MARC_Subfield('a', 'Eternal darkness'),
47+
new File_MARC_Subfield('b', 'sanity\'s requiem : Prima\'s official strategy guide'),
48+
new File_MARC_Subfield('c', 'the Stratton Bros.'),
49+
));
50+
$title = new Title($field);
51+
$this->assertEquals('Eternal darkness : sanity\'s requiem : Prima\'s official strategy guide', strval($title));
52+
}
53+
54+
public function testParallelTitleUs()
55+
{
56+
// ISBD style: US
57+
// Title components: Main title, Parallel title
58+
$field = new File_MARC_Data_Field('245', array(
59+
new File_MARC_Subfield('a', 'Lageru ='),
60+
new File_MARC_Subfield('b', 'The land of eternal darkness /'),
61+
new File_MARC_Subfield('c', 'Kwak Pyŏng-gyu chŏ.'),
62+
));
63+
$title = new Title($field);
64+
$this->assertEquals('Lageru = The land of eternal darkness', strval($title));
65+
}
66+
67+
public function testParallelTitleUk()
68+
{
69+
// ISBD style: UK (note: = mark still included)
70+
// Components: Main title, Parallel title
71+
$field = new File_MARC_Data_Field('245', array(
72+
new File_MARC_Subfield('a', 'Byggekunst ='),
73+
new File_MARC_Subfield('b', 'The Norwegian review of architecture'),
74+
new File_MARC_Subfield('c', 'Norske arkitekters landsforbund'),
75+
));
76+
$title = new Title($field);
77+
$this->assertEquals('Byggekunst = The Norwegian review of architecture', strval($title));
78+
}
79+
80+
public function testIsbdSymbolsInTitle()
81+
{
82+
# http://lccn.loc.gov/2006589502
83+
# Here, the = does not indicate the start of a parallel title, but is part of the title.
84+
# How can we know?? Because it don't have a space in front?
85+
$field = new File_MARC_Data_Field('245', array(
86+
new File_MARC_Subfield('a', '2 + 2 = 5 :'),
87+
new File_MARC_Subfield('b', 'innovative ways of organising people in the Australian Public Service.'),
88+
));
89+
$title = new Title($field);
90+
$this->assertEquals('2 + 2 = 5 : innovative ways of organising people in the Australian Public Service.', strval($title));
91+
}
92+
93+
public function testMultipleTitles()
94+
{
95+
# http://lccn.loc.gov/2006589502
96+
# An example of where we really shouldn't strip of the final dot(s)
97+
$field = new File_MARC_Data_Field('245', array(
98+
new File_MARC_Subfield('a', 'Hamlet ;'),
99+
new File_MARC_Subfield('b', 'Romeo and Juliette ; Othello ...'),
100+
));
101+
$title = new Title($field);
102+
$this->assertEquals('Hamlet ; Romeo and Juliette ; Othello ...', strval($title));
103+
}
104+
105+
public function testTitleWithPart()
106+
{
107+
$field = new File_MARC_Data_Field('245', array(
108+
new File_MARC_Subfield('a', 'Love from Joy :'),
109+
new File_MARC_Subfield('b', 'letters from a farmer’s wife.'),
110+
new File_MARC_Subfield('n', 'Part III,'),
111+
new File_MARC_Subfield('p', '1987-1995, At the bungalow.'),
112+
));
113+
$title = new Title($field);
114+
$this->assertEquals('Love from Joy : letters from a farmer’s wife. Part III, 1987-1995, At the bungalow.', strval($title));
115+
}
116+
117+
public function testTitleWithMultiplePartSubfields()
118+
{
119+
$field = new File_MARC_Data_Field('245', array(
120+
new File_MARC_Subfield('a', 'Zentralblatt für Bakteriologie.'),
121+
new File_MARC_Subfield('n', '1. Abt. Originale.'),
122+
new File_MARC_Subfield('n', 'Reihe B,'),
123+
new File_MARC_Subfield('p', 'Hygiene, Krankenhaushygiene, Betriebshygiene, präventive Medizin.'),
124+
));
125+
$title = new Title($field);
126+
$this->assertEquals('Zentralblatt für Bakteriologie. 1. Abt. Originale. Reihe B, Hygiene, Krankenhaushygiene, Betriebshygiene, präventive Medizin.', strval($title));
127+
}
128+
129+
}

0 commit comments

Comments
 (0)