Skip to content

Commit a74007f

Browse files
committed
Let Field implement JsonSerializable
1 parent 60e1750 commit a74007f

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

src/Fields/Field.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Scriptotek\Marc\Record;
66

7-
class Field
7+
abstract class Field implements \JsonSerializable
88
{
99
protected $field;
1010

@@ -13,6 +13,11 @@ public function __construct(\File_MARC_Field $field)
1313
$this->field = $field;
1414
}
1515

16+
public function jsonSerialize()
17+
{
18+
return (string) $this;
19+
}
20+
1621
public function __call($name, $args)
1722
{
1823
return call_user_func_array([$this->field, $name], $args);

src/Fields/Subject.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,14 @@ public function __toString()
8383
{
8484
return implode(self::$glue, $this->getParts());
8585
}
86+
87+
public function jsonSerialize()
88+
{
89+
return [
90+
'type' => $this->getType(),
91+
'vocabulary' => $this->getVocabulary(),
92+
'id' => $this->getControlNumber(),
93+
'term' => (string) $this,
94+
];
95+
}
8696
}

tests/FieldsTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function testIsbn()
2020
$record = Record::fromString($source);
2121
$this->assertEquals(['8200424421'], $record->isbns);
2222
$this->assertEquals('Nkr 98.00', $record->isbns[0]->sf('c'));
23+
$this->assertEquals(
24+
json_encode(['isbns' => ['8200424421']]),
25+
json_encode(['isbns' => $record->isbns])
26+
);
2327
}
2428

2529
public function test020withoutA()
@@ -35,7 +39,11 @@ public function test020withoutA()
3539
</record>';
3640

3741
$record = Record::fromString($source);
38-
$this->assertEquals(array(''), $record->isbns);
42+
$this->assertEquals([''], $record->isbns);
43+
$this->assertEquals(
44+
json_encode(['isbns' => ['']]),
45+
json_encode(['isbns' => $record->isbns])
46+
);
3947
}
4048

4149
public function testId()
@@ -48,5 +56,9 @@ public function testId()
4856

4957
$record = Record::fromString($source);
5058
$this->assertEquals('98218834x', $record->id);
59+
$this->assertEquals(
60+
json_encode(['id' => '98218834x']),
61+
json_encode(['id' => $record->id])
62+
);
5163
}
5264
}

tests/SubjectFieldTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,20 @@ public function testEdit()
6868
$record->subjects[0]->delete();
6969
$this->assertCount(0, $record->subjects);
7070
}
71+
72+
public function testJsonSerialization()
73+
{
74+
$record = $this->getNthrecord(3);
75+
$subject = $record->subjects[1];
76+
77+
$this->assertJsonStringEqualsJsonString(
78+
json_encode([
79+
'vocabulary' => 'noubomn',
80+
'type' => Subject::TOPICAL_TERM,
81+
'id' => null,
82+
'term' => 'Elementærpartikler'
83+
]),
84+
json_encode($subject)
85+
);
86+
}
7187
}

tests/TitleFieldTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ public function testTitleWithMultiplePartSubfields()
124124
$title = new Title($field);
125125
$this->assertEquals('Zentralblatt für Bakteriologie. 1. Abt. Originale. Reihe B, Hygiene, Krankenhaushygiene, Betriebshygiene, präventive Medizin.', strval($title));
126126
}
127+
128+
public function testJsonSerialization()
129+
{
130+
$field = new File_MARC_Data_Field('245', array(
131+
new File_MARC_Subfield('a', 'Zentralblatt für Bakteriologie.'),
132+
new File_MARC_Subfield('n', '1. Abt. Originale.'),
133+
new File_MARC_Subfield('n', 'Reihe B,'),
134+
new File_MARC_Subfield('p', 'Hygiene, Krankenhaushygiene, Betriebshygiene, präventive Medizin.'),
135+
));
136+
$title = new Title($field);
137+
138+
$this->assertJsonStringEqualsJsonString(
139+
json_encode([
140+
'title' => 'Zentralblatt für Bakteriologie. 1. Abt. Originale. Reihe B, Hygiene, Krankenhaushygiene, Betriebshygiene, präventive Medizin.',
141+
]),
142+
json_encode(['title' => $title])
143+
);
144+
}
127145
}

0 commit comments

Comments
 (0)