Skip to content

Commit 23d989c

Browse files
committed
Add chopPunctuation to Subject
1 parent 0c05786 commit 23d989c

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,16 @@ foreach ($record->getSubjects('mesh', Subject::TOPICAL_TERM) as $subject) {
213213
}
214214
```
215215

216-
The string representation of this field makes use of the constant `Subject::glue`
217-
to glue subject components together. The default value is a space-padded colon,
218-
making `Physics : History : 20th century` the string representation of
219-
`650 $aPhysics $xHistory $yHistory`. If you prefer the "LCSH-way" of
220-
`Physics--History--20th century`, just set `Subject::glue = '--'`.
216+
Static options:
217+
218+
* `Subject::glue` (default: ` : `) defines what string is used to glue the subfields
219+
together in the string representation. For instance, `650 $aPhysics $xHistory $yHistory`
220+
becomes `Physics : History : 20th century` when using ` : ` as glue, or
221+
`Physics--History--20th century` with `'--'`.
222+
* `Subject::chopPunctuation` (default: `true`) defines if ending punctuation (.:,;/)
223+
is to be chopped off at the end of subjects. Usually, any ending punctuation is an
224+
ISBD character that can be safely chopped off, but it might also indicate an abbreviation,
225+
and unfortunately there is no way to know.
221226

222227
## Notes
223228

src/Fields/Field.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ protected function getSubfieldValues($codes)
7979
* @param string $glue
8080
* @return string
8181
*/
82-
protected function toString($codes, $glue = ' ')
82+
protected function toString($codes, $glue = ' ', $chopPunctuation = true)
8383
{
84-
return trim(implode($glue, $this->getSubfieldValues($codes)));
84+
$value = trim(implode($glue, $this->getSubfieldValues($codes)));
85+
if ($chopPunctuation) {
86+
$value = preg_replace('/[.:,;]$/', '', $value);
87+
}
88+
return $value;
8589
}
8690

8791
/**

src/Fields/Subject.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Subject extends Field implements FieldInterface, SubjectInterface
1212
public $properties = ['type', 'vocabulary', 'term', 'id'];
1313

1414
public static $glue = ' : ';
15+
public static $chopPunctuation = true;
1516
public static $termComponentCodes = ['a', 'b', 'x', 'y', 'z'];
1617

1718
const PERSONAL_NAME = '600';
@@ -92,7 +93,7 @@ public function getParts()
9293

9394
public function getTerm()
9495
{
95-
return $this->toString(self::$termComponentCodes, self::$glue);
96+
return $this->toString(self::$termComponentCodes, self::$glue, self::$chopPunctuation);
9697
}
9798

9899
public function __toString()

tests/SubjectFieldTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
class SubjectFieldTest extends TestCase
99
{
10-
protected function getNthrecord($n)
10+
protected function getNthrecord($filename, $n)
1111
{
12-
$records = Collection::fromFile('tests/data/sru-alma.xml')->toArray();
12+
$records = Collection::fromFile('tests/data/' . $filename)->toArray();
1313

1414
return $records[$n - 1];
1515
}
1616

1717
public function testSubjectString()
1818
{
19-
$record = $this->getNthrecord(1);
19+
$record = $this->getNthrecord('sru-alma.xml', 1);
2020

2121
# Vocabulary from indicator2
2222
$sub = $record->subjects[0];
@@ -25,9 +25,20 @@ public function testSubjectString()
2525
$this->assertEquals('Eightfold way (Nuclear physics) : Addresses, essays, lectures', strval($sub));
2626
}
2727

28+
public function testChopPunctuation()
29+
{
30+
$record = $this->getNthrecord('sru-loc.xml', 2);
31+
32+
# Vocabulary from indicator2
33+
$sub = $record->subjects[0];
34+
$this->assertEquals('lcsh', $sub->vocabulary);
35+
$this->assertEquals(Subject::TOPICAL_TERM, $sub->type);
36+
$this->assertEquals('Popular music : 1961-1970', strval($sub));
37+
}
38+
2839
public function testSubjects()
2940
{
30-
$record = $this->getNthrecord(3);
41+
$record = $this->getNthrecord('sru-alma.xml', 3);
3142

3243
# Vocabulary from subfield 2
3344
$subject = $record->subjects[1];
@@ -40,7 +51,7 @@ public function testSubjects()
4051

4152
public function testRepeated653a()
4253
{
43-
$record = $this->getNthrecord(3);
54+
$record = $this->getNthrecord('sru-alma.xml', 3);
4455

4556
$subjects = $record->getSubjects(null, Subject::UNCONTROLLED_INDEX_TERM);
4657
$this->assertCount(2, $subjects);
@@ -53,7 +64,7 @@ public function testRepeated653a()
5364

5465
public function testGetSubjectsFiltering()
5566
{
56-
$record = $this->getNthrecord(3);
67+
$record = $this->getNthrecord('sru-alma.xml', 3);
5768

5869
$lcsh = $record->getSubjects('lcsh');
5970
$noubomn = $record->getSubjects('noubomn');
@@ -70,7 +81,7 @@ public function testGetSubjectsFiltering()
7081

7182
public function testEdit()
7283
{
73-
$record = $this->getNthrecord(3);
84+
$record = $this->getNthrecord('sru-alma.xml', 3);
7485
$this->assertCount(5, $record->subjects);
7586

7687
$this->assertInstanceOf(Subject::class, $record->subjects[0]);
@@ -94,7 +105,7 @@ public function testEdit()
94105

95106
public function testJsonSerialization()
96107
{
97-
$record = $this->getNthrecord(3);
108+
$record = $this->getNthrecord('sru-alma.xml', 3);
98109
$subject = $record->subjects[1];
99110

100111
$this->assertJsonStringEqualsJsonString(

tests/data/examples/bibliographic4.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{
2121
"type": "650",
2222
"vocabulary": "lcsh",
23-
"term": "Semiconductors."
23+
"term": "Semiconductors"
2424
},
2525
{
2626
"type": "650",
@@ -67,4 +67,4 @@
6767
"assigning_source": "Provided by publisher."
6868
},
6969
"part_of": null
70-
}
70+
}

0 commit comments

Comments
 (0)