class Record in Bibliography Module 7
Same name and namespace in other branches
- 6.2 modules/marcParse/php-marc.php \Record
- 6 marcParse/php-marc.php \Record
- 7.2 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 398 - @package PHP-MARC
View source
class Record {
/**
* ========== VARIABLE DECLARATIONS ==========.
*/
/**
* Contain all @link Field objects of the Record.
*
* @var array
*/
public $fields;
/**
* Leader of the Record.
*
* @var string
*/
public $ldr;
/**
* Array of warnings.
*
* @var array
*/
public $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
*/
public 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
*/
public function _warn($msg) {
$this->warn[] = $msg;
return $msg;
}
/**
* Return an array of warnings.
*/
public function warnings() {
return $this->warn;
}
/**
* ========== PROCESSING FUNCTIONS ==========.
*/
/**
* Start function.
*
* Set all variables to defaults to create new Record object.
*/
public function __construct() {
$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.
*/
public 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
*/
public 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 )
*/
public 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;
$total = $baseaddress + $dataend + 1;
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.
*/
public 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
*/
public 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
*/
public 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
*/
public 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
*/
public function delete_field($obj) {
unset($this->fields[$obj->field]);
}
/**
* Clone record.
*
* Clone a record with all its Fields and subfields.
*
* @return Record Clone record
*/
public 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
*/
public 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
*/
public 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
*/
public 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:: |
public | property | Contain all @link Field objects of the Record. | |
Record:: |
public | property | Leader of the Record. | |
Record:: |
public | property | Array of warnings. | |
Record:: |
public | function | Append field to existing. | |
Record:: |
public | function | Delete Field. | |
Record:: |
public | function | Formatted representation of Field. | |
Record:: |
public | function | Get specific field. | |
Record:: |
public | function | Return all Field objects. | |
Record:: |
public | function | Return formatted. | |
Record:: |
public | function | Get/Set Leader. | |
Record:: |
public | function | Set Leader lengths. | |
Record:: |
public | function | Clone record. | |
Record:: |
public | function | Return Raw. | |
Record:: |
public | function | Get subfield of Field object. | |
Record:: |
public | function | Return an array of warnings. | |
Record:: |
public | function | Build Record Directory. | |
Record:: |
public | function | Croaking function. | |
Record:: |
public | function | Fuction to issue warnings. | |
Record:: |
public | function | Start function. |