Skip to content

Commit 76a0020

Browse files
committed
Fix Subject interface violation
1 parent 82475b1 commit 76a0020

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/Fields/Field.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,48 @@ public function __get($key)
3737
}
3838

3939
/**
40-
* Return concatenated string of the given subfields.
41-
*
42-
* @param string[] $codes
43-
* @param string $glue
44-
* @return string
40+
* @param string|string[] $codes
41+
* @return array
4542
*/
46-
protected function toString($codes, $glue = ' ')
43+
protected function getSubfieldValues($codes)
4744
{
45+
if (!is_array($codes)) {
46+
$codes = [$codes];
47+
}
4848
$parts = [];
4949
foreach ($this->field->getSubfields() as $sf) {
5050
if (in_array($sf->getCode(), $codes)) {
5151
$parts[] = trim($sf->getData());
5252
}
5353
}
5454

55-
return trim(implode($glue, $parts));
55+
return $parts;
56+
}
57+
58+
/**
59+
* Return concatenated string of the given subfields.
60+
*
61+
* @param string[] $codes
62+
* @param string $glue
63+
* @return string
64+
*/
65+
protected function toString($codes, $glue = ' ')
66+
{
67+
return trim(implode($glue, $this->getSubfieldValues($codes)));
5668
}
5769

5870
/**
5971
* Return the data value of the *first* subfield with a given code.
72+
*
73+
* @param string $code
74+
* @param mixed $default
75+
* @return mixed
6076
*/
61-
public function sf($code)
77+
public function sf($code, $default = '')
6278
{
6379
$subfield = $this->getSubfield($code);
6480
if (!$subfield) {
65-
return null;
81+
return $default;
6682
}
6783

6884
return trim($subfield->getData());

src/Fields/Subject.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Subject extends Field implements FieldInterface, SubjectInterface
88
{
99
public static $glue = ' : ';
10+
public static $termComponentCodes = ['a', 'b', 'x', 'y', 'z'];
1011

1112
const PERSONAL_NAME = '600';
1213
const CORPORATION_NAME = '601';
@@ -80,9 +81,14 @@ public function getControlNumber()
8081
return $value ?: null;
8182
}
8283

84+
public function getParts()
85+
{
86+
return $this->getSubfields(self::$termComponentCodes);
87+
}
88+
8389
public function getTerm()
8490
{
85-
return $this->toString(['a', 'b', 'x', 'y', 'z'], self::$glue);
91+
return $this->toString(self::$termComponentCodes, self::$glue);
8692
}
8793

8894
public function __toString()

0 commit comments

Comments
 (0)