Last active
July 26, 2016 18:14
-
-
Save atbradley/b803bbc81a2d6608a8b320e3ca957b7f to your computer and use it in GitHub Desktop.
Parse Blacklight's JSON representation of MaRC records.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Functions for treating (blacklight-based) Josiah as an API. | |
*/ | |
namespace ocra\josiah; | |
/** | |
* Find a field (and optionally subfield) in the JSONified MaRCXML record. | |
* | |
* @return Array|string | |
*/ | |
function find_marc_field($marcjson, $field, $subfield=null, $ind1 = false, $ind2 = false) { | |
foreach ( $marcjson->fields as $fld ) { | |
if ( property_exists($fld, $field) ) { | |
if ( !$ind1 || $ind1 == $fld->$field->ind1 ) | |
if ( !$ind2 || $ind2 == $fld->$field->ind2 ) | |
$foutp = $fld->$field; | |
break; | |
} | |
} | |
if ( !isset($foutp) ) return false; | |
if ( !is_null($subfield) ) { | |
foreach ( $foutp->subfields as $sf ) { | |
if ( array_key_exists($subfield, $sf) ) { | |
return $sf->$subfield; | |
} | |
} | |
return false; | |
} | |
return $foutp; | |
} | |
/** | |
* Return all fields (or field/subfield pairs) in a JSONified MaRCXML record. | |
* | |
* @return Array | |
*/ | |
function find_marc_field_multi($marcjson, $field, $subfield=null, $ind1 = false, $ind2 = false) { | |
$fields = array(); | |
foreach ( $marcjson->fields as $fld ) { | |
if ( property_exists($fld, $field) ) { | |
if ( !$ind1 || $ind1 == $fld->$field->ind1 ) | |
if ( !$ind2 || $ind2 == $fld->$field->ind2 ) | |
array_push($fields, $fld->$field); | |
} | |
} | |
if ( !is_null($subfield) ) { | |
$sfields = array(); | |
foreach ( $fields as $fld ) { | |
foreach ( $fld->subfields as $sf ) { | |
if ( array_key_exists($subfield, $sf) ) { | |
array_push($sfields, $sf->$subfield); | |
} | |
} | |
} | |
return $sfields; | |
} | |
return $fields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment