Permalink
Browse files

Add the JSON MARC-HASH output format per http://robotlibrarian.billdu…

  • Loading branch information...
1 parent 7541a89 commit d92fcf83892ef76c2b13eeed95f0dca74d9c2dba Dan Scott committed Aug 14, 2010
Showing with 76 additions and 7 deletions.
  1. +54 −0 MARC/Record.php
  2. +7 −7 package.xml
  3. +15 −0 tests/marc_018.phpt
View
@@ -549,6 +549,60 @@ function toJSON()
// }}}
+ // {{{ toJSONHash()
+ /**
+ * Return the MARC record in Bill Dueber's MARC-HASH JSON format
+ *
+ * This method produces a JSON representation of a MARC record as defined
+ * at http://robotlibrarian.billdueber.com/new-interest-in-marc-hash-json/
+ * The input * encoding must be UTF8, otherwise the returned values will
+ * be corrupted.
+ *
+ * @return string representation of MARC record in JSON format
+ *
+ * @todo Fix encoding input / output issues (PHP 6.0 required?)
+ */
+ function toJSONHash()
+ {
+ $json = new StdClass();
+ $json->type = "marc-hash";
+ $json->version = array(1, 0);
+ $json->leader = utf8_encode($this->getLeader());
+
+ /* Start fields */
+ $fields = array();
+ foreach ($this->fields as $field) {
+ if (!$field->isEmpty()) {
+ switch(get_class($field)) {
+ case "File_MARC_Control_Field":
+ $fields[] = array(utf8_encode($field->getTag()), utf8_encode($field->getData()));
+ break;
+
+ case "File_MARC_Data_Field":
+ $subs = array();
+ foreach ($field->getSubfields() as $sf) {
+ $subs[] = array(utf8_encode($sf->getCode()), utf8_encode($sf->getData()));
+ }
+ $contents = array(
+ utf8_encode($field->getTag()),
+ utf8_encode($field->getIndicator(1)),
+ utf8_encode($field->getIndicator(2)),
+ $subs
+ );
+ $fields[] = $contents;
+ break;
+ }
+ }
+ }
+ /* End fields and record */
+
+ $json->fields = $fields;
+ return json_encode($json);
+ }
+
+ // }}}
+
+
// {{{ toXML()
/**
* Return the MARC record in MARCXML format
View
@@ -19,20 +19,19 @@ This package is based on the PHP MARC package, originally called "php-marc", tha
<email>dbs@php.net</email>
<active>yes</active>
</lead>
- <date>2010-08-05</date>
+ <date>2010-08-14</date>
<version>
- <release>0.5.2</release>
- <api>0.5.0</api>
+ <release>0.6.0</release>
+ <api>0.6.0</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">GNU Lesser General Public License</license>
<notes>
-0.5.2-beta
- * Enable File_MARC_Record to be invoked with a null constructor again, useful for building MARC records from scratch
- * Add a test to check that the null constructor works to avoid future regressions
+0.6.0-beta
+ * Add two flavours of JSON output from File_MARC_Record: toJSON() and toJSONHash()
</notes>
<contents>
<dir name="/" baseinstalldir="File">
@@ -77,6 +76,7 @@ This package is based on the PHP MARC package, originally called "php-marc", tha
<file name="marc_015.phpt" role="test" />
<file name="marc_016.phpt" role="test" />
<file name="marc_017.phpt" role="test" />
+ <file name="marc_018.phpt" role="test" />
<file name="marc_16783.phpt" role="test" />
<file name="marc_field_001.phpt" role="test" />
<file name="marc_field_002.phpt" role="test" />
@@ -108,7 +108,7 @@ This package is based on the PHP MARC package, originally called "php-marc", tha
<dependencies>
<required>
<php>
- <min>5.1</min>
+ <min>5.2</min>
</php>
<pearinstaller>
<min>1.4.0</min>
View
@@ -0,0 +1,15 @@
+--TEST--
+marc_018: iterate and print a MARC record to JSON MARC-HASH format
+--FILE--
+<?php
+$dir = dirname(__FILE__);
+require 'File/MARC.php';
+$marc_file = new File_MARC($dir . '/' . 'example.mrc');
+
+while ($marc_record = $marc_file->next()) {
+ print $marc_record->toJSONHash();
+ print "\n";
+}
+?>
+--EXPECT--
+{"type":"marc-hash","version":[1,0],"leader":"01850 2200517 4500","fields":[["001","0000000044"],["003","EMILDA"],["008","980120s1998 fi j 000 0 swe"],["020"," "," ",[["a","9515008808"],["c","FIM 72:00"]]],["035"," "," ",[["9","9515008808"]]],["040"," "," ",[["a","NB"]]],["042"," "," ",[["9","NB"],["9","SEE"]]],["084"," "," ",[["a","Hcd,u"],["2","kssb\/6"]]],["084"," "," ",[["5","NB"],["a","uHc"],["2","kssb"]]],["084"," "," ",[["5","SEE"],["a","Hcf"],["2","kssb\/6"]]],["084"," "," ",[["5","Q"],["a","Hcd,uf"],["2","kssb\/6"]]],["100","1"," ",[["a","Jansson, Tove,"],["d","1914-2001"]]],["245","0","4",[["a","Det osynliga barnet och andra ber\u00e4ttelser \/"],["c","Tove Jansson"]]],["250"," "," ",[["a","7. uppl."]]],["260"," "," ",[["a","Helsingfors :"],["b","Schildt,"],["c","1998 ;"],["e","(Falun :"],["f","Scandbook)"]]],["300"," "," ",[["a","166, [4] s. :"],["b","ill. ;"],["c","21 cm"]]],["440"," ","0",[["a","Mumin-biblioteket,"],["x","99-0698931-9"]]],["500"," "," ",[["a","Originaluppl. 1962"]]],["599"," "," ",[["a","Li: S"]]],["740","4"," ",[["a","Det osynliga barnet"]]],["775","1"," ",[["z","951-50-0385-7"],["w","9515003857"],["9","07"]]],["841"," "," ",[["5","Li"],["a","xa"],["b","0201080u 0 4000uu |000000"],["e","1"]]],["841"," "," ",[["5","SEE"],["a","xa"],["b","0201080u 0 4000uu |000000"],["e","1"]]],["841"," "," ",[["5","L"],["a","xa"],["b","0201080u 0 4000uu |000000"],["e","1"]]],["841"," "," ",[["5","NB"],["a","xa"],["b","0201080u 0 4000uu |000000"],["e","1"]]],["841"," "," ",[["5","Q"],["a","xa"],["b","0201080u 0 4000uu |000000"],["e","1"]]],["841"," "," ",[["5","S"],["a","xa"],["b","0201080u 0 4000uu |000000"],["e","1"]]],["852"," "," ",[["5","NB"],["b","NB"],["c","NB98:12"],["h","plikt"],["j","R, 980520"]]],["852"," "," ",[["5","Li"],["b","Li"],["c","CNB"],["h","h,u"]]],["852"," "," ",[["5","SEE"],["b","SEE"]]],["852"," "," ",[["5","Q"],["b","Q"],["j","98947"]]],["852"," "," ",[["5","L"],["b","L"],["c","0100"],["h","98\/"],["j","3043 H"]]],["852"," "," ",[["5","S"],["b","S"],["h","Sv97"],["j","7235"]]],["900","1","s",[["a","Yanson, Tobe,"],["d","1914-2001"],["u","Jansson, Tove,"],["d","1914-2001"]]],["900","1","s",[["a","Janssonov\u00e1, Tove,"],["d","1914-2001"],["u","Jansson, Tove,"],["d","1914-2001"]]],["900","1","s",[["a","Jansone, Tuve,"],["d","1914-2001"],["u","Jansson, Tove,"],["d","1914-2001"]]],["900","1","s",[["a","Janson, Tuve,"],["d","1914-2001"],["u","Jansson, Tove,"],["d","1914-2001"]]],["900","1","s",[["a","Jansson, Tuve,"],["d","1914-2001"],["u","Jansson, Tove,"],["d","1914-2001"]]],["900","1","s",[["a","Janssonova, Tove,"],["d","1914-2001"],["u","Jansson, Tove,"],["d","1914-2001"]]],["976"," ","2",[["a","Hcd,u"],["b","Sk\u00f6nlitteratur"]]],["005","20050204111518.0"]]}

0 comments on commit d92fcf8

Please sign in to comment.