class Record in Bibliography Module 7.2
Same name and namespace in other branches
- 6.2 modules/marcParse/php-marc.php \Record
- 6 marcParse/php-marc.php \Record
- 7 modules/marcParse/php-marc.php \Record
Record Class Create a MARC Record class
Hierarchy
- class \Record
Expanded class hierarchy of Record
File
- modules/
marcParse/ php-marc.php, line 365
View source
class Record {
/**
* ========== VARIABLE DECLARATIONS ==========
*/
/**
* Contain all @link Field objects of the Record
* @var array
*/
var $fields;
/**
* Leader of the Record
* @var string
*/
var $ldr;
/**
* Array of warnings
* @var array
*/
var $warn;
/**
* ========== ERROR FUNCTIONS ==========
*/
/**
* Croaking function
*
* Similar to Perl's croak function, which ends parsing and raises an
* user error with a descriptive message.
* @param string The message to display
*/
function _croak($msg) {
trigger_error($msg, E_USER_ERROR);
}
/**
* Fuction to issue warnings
*
* Warnings will not be displayed unless explicitly accessed, but all
* warnings issued during parse will be stored
* @param string Warning
* @return string Last added warning
*/
function _warn($msg) {
$this->warn[] = $msg;
return $msg;
}
/**
* Return an array of warnings
*/
function warnings() {
return $this->warn;
}
/**
* ========== PROCESSING FUNCTIONS ==========
*/
/**
* Start function
*
* Set all variables to defaults to create new Record object
*/
function record() {
$this->fields = array();
$this->ldr = str_repeat(' ', 24);
}
/**
* Get/Set Leader
*
* If argument specified, sets leader, otherwise gets leader. No validation
* on the specified leader is performed
* @param string Leader
* @return string|null Return leader in case requested.
*/
function leader($ldr = "") {
if ($ldr) {
$this->ldr = $ldr;
}
else {
return $this->ldr;
}
}
/**
* Append field to existing
*
* Given Field object will be appended to the existing list of fields. Field will be
* appended last and not in its "correct" location.
* @param Field The field to append
*/
function append_fields($field) {
if (strtolower(get_class($field)) == "field") {
$this->fields[$field->tagno][] = $field;
}
else {
$this
->_croak(sprintf("Given argument must be Field object, but was '%s'", get_class($field)));
}
}
/**
* Build Record Directory
*
* Generate the directory of the Record according to existing data.
* @return array Array ( $fields, $directory, $total, $baseaddress )
*/
function _build_dir() {
// Vars
$fields = array();
$directory = array();
$dataend = 0;
foreach ($this->fields as $field_group) {
foreach ($field_group as $field) {
// Get data in raw format
$str = $field
->raw();
$fields[] = $str;
// Create directory entry
$len = strlen($str);
$direntry = sprintf("%03s%04d%05d", $field
->tagno(), $len, $dataend);
$directory[] = $direntry;
$dataend += $len;
}
}
/**
* Rules from MARC::Record::USMARC
*/
$baseaddress = LEADER_LEN + count($directory) * DIRECTORY_ENTRY_LEN + 1;
// end-of-field marker
$total = $baseaddress + $dataend + 1;
// End-of-record marker
return array(
$fields,
$directory,
$total,
$baseaddress,
);
}
/**
* Set Leader lengths
*
* Set the Leader lengths of the record according to defaults specified in
* http://www.loc.gov/marc/bibliographic/ecbdldrd.html
*/
function leader_lengths($reclen, $baseaddr) {
$this->ldr = substr_replace($this->ldr, sprintf("%05d", $reclen), 0, 5);
$this->ldr = substr_replace($this->ldr, sprintf("%05d", $baseaddr), 12, 5);
$this->ldr = substr_replace($this->ldr, '22', 10, 2);
$this->ldr = substr_replace($this->ldr, '4500', 20, 4);
}
/**
* Return all Field objects
* @return array Array of Field objects
*/
function fields() {
return $this->fields;
}
/**
* Get specific field
*
* Search for field in Record fields based on field name, e.g. 020
* @param string Field name
* @return Field|FALSE Return Field if found, otherwise FALSE
*/
function field($spec) {
if (array_key_exists($spec, $this->fields)) {
return $this->fields[$spec][0];
}
else {
return FALSE;
}
}
/**
* Get subfield of Field object
*
* Returns the value of a specific subfield of a given Field object
* @param string Name of field
* @param string Name of subfield
* @return string|FALSE Return value of subfield if Field exists, otherwise FALSE
*/
function subfield($field, $subfield) {
if (!($field = $this
->field($field))) {
return FALSE;
}
else {
return $field
->subfield($subfield);
}
}
/**
* Delete Field
*
* Delete a given field from within a Record
* @param Field The field to be deleted
*/
function delete_field($obj) {
unset($this->fields[$obj->field]);
}
/**
* Clone record
*
* Clone a record with all its Fields and subfields
* @return Record Clone record
*/
function make_clone() {
$clone = new Record();
$clone
->leader($this->ldr);
foreach ($this
->fields() as $data) {
foreach ($data as $field) {
$clone
->append_fields($field);
}
}
return $clone;
}
/**
* ========== OUTPUT FUNCTIONS ==========
*/
/**
* Formatted representation of Field
*
* Format a Field with a sprintf()-like formatting syntax. The formatting
* codes are the names of the subfields of the Field.
* @param string Field name
* @param string Format string
* @return string|FALSE Return formatted string if Field exists, otherwise False
*/
function ffield($tag, $format) {
$result = "";
if ($field = $this
->field($tag)) {
for ($i = 0; $i < strlen($format); $i++) {
$curr = $format[$i];
if ($curr != "%") {
$result[] = $curr;
}
else {
$i++;
$curr = $format[$i];
if ($curr == "%") {
$result[] = $curr;
}
else {
$result[] = $field
->subfield($curr);
}
}
}
return implode("", $result);
}
else {
return FALSE;
}
}
/**
* Return Raw
*
* Return the Record in raw MARC format.
* @return string Raw MARC data
*/
function raw() {
list($fields, $directory, $reclen, $baseaddress) = $this
->_build_dir();
$this
->leader_lengths($reclen, $baseaddress);
/**
* Glue together all parts
*/
return $this->ldr . implode("", $directory) . END_OF_FIELD . implode("", $fields) . END_OF_RECORD;
}
/**
* Return formatted
*
* Return the Record in a formatted fashion. Similar to the output
* of the formatted() function in MARC::Record in Perl
* @return string Formatted representation of MARC record
*/
function formatted() {
$formatted = "";
foreach ($this->fields as $field_group) {
foreach ($field_group as $field) {
$formatted .= $field
->formatted() . "\n";
}
}
return $formatted;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Record:: |
property | Contain all @link Field objects of the Record | ||
Record:: |
property | Leader of the Record | ||
Record:: |
property | Array of warnings | ||
Record:: |
function | Append field to existing | ||
Record:: |
function | Delete Field | ||
Record:: |
function | Formatted representation of Field | ||
Record:: |
function | Get specific field | ||
Record:: |
function | Return all Field objects | ||
Record:: |
function | Return formatted | ||
Record:: |
function | Get/Set Leader | ||
Record:: |
function | Set Leader lengths | ||
Record:: |
function | Clone record | ||
Record:: |
function | Return Raw | ||
Record:: |
function | Start function | ||
Record:: |
function | Get subfield of Field object | ||
Record:: |
function | Return an array of warnings | ||
Record:: |
function | Build Record Directory | ||
Record:: |
function | Croaking function | ||
Record:: |
function | Fuction to issue warnings |