You are here

class Field in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/marcParse/php-marc.php \Field
  2. 6 marcParse/php-marc.php \Field
  3. 7.2 modules/marcParse/php-marc.php \Field

Field Class Create a MARC Field object.

Hierarchy

Expanded class hierarchy of Field

File

modules/marcParse/php-marc.php, line 732
@package PHP-MARC

View source
class Field {

  /**
   * ========== VARIABLE DECLARATIONS ==========.
   */

  /**
   * The tag name of the Field.
   *
   * @var string
   */
  public $tagno;

  /**
   * Value of the first indicator.
   *
   * @var string
   */
  public $ind1;

  /**
   * Value of the second indicator.
   *
   * @var string
   */
  public $ind2;

  /**
   * Array of subfields.
   *
   * @var array
   */
  public $subfields = array();

  /**
   * Specify if the Field is a Control field.
   *
   * @var bool
   */
  public $is_control;

  /**
   * Array of warnings.
   *
   * @var array
   */
  public $warn;

  /**
   * Value of field, if field is a Control field.
   *
   * @var string
   */
  public $data;

  /**
   * ========== 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 ==========.
   */

  /**
   * Field init function.
   *
   * Create a new Field object from passed arguments.
   *
   * @param array Array ( tagno, ind1, ind2, subfield_data )
   *
   * @return string Returns warnings if any issued during parse
   */
  public function __construct() {
    $args = func_get_args();
    $tagno = array_shift($args);
    $this->tagno = $tagno;

    // Check if valid tag.
    if (!preg_match("/^[0-9A-Za-z]{3}\$/", $tagno)) {
      return $this
        ->_warn("Tag \"{$tagno}\" is not a valid tag.");
    }

    // Check if field is Control field.
    $this->is_control = preg_match("/^\\d+\$/", $tagno) && $tagno < 10;
    if ($this->is_control) {
      $this->data = array_shift($args);
    }
    else {
      foreach (array(
        "ind1",
        "ind2",
      ) as $indcode) {
        $indicator = array_shift($args);
        if (!preg_match("/^[0-9A-Za-z ]\$/", $indicator)) {
          if ($indicator != "") {
            $this
              ->_warn("Illegal indicator '{$indicator}' in field '{$tagno}' forced to blank");
          }
          $indicator = " ";
        }
        $this->{$indcode} = $indicator;
      }
      $subfields = array_shift($args);
      if (count($subfields) < 1) {
        return $this
          ->_warn("Field {$tagno} must have at least one subfield");
      }
      else {
        $this
          ->add_subfields($subfields);
      }
    }
  }

  /**
   * Add subfield.
   *
   * Appends subfields to existing fields last, not in "correct" plase.
   *
   * @param array Subfield data
   *
   * @return string Returns warnings if issued during parse.
   */
  public function add_subfields() {

    // Process arguments.
    $args = func_get_args();
    if (count($args) == 1 && is_array($args[0])) {
      $args = $args[0];
    }

    // Add subfields, is appropriate.
    if ($this->is_control) {
      return $this
        ->_warn("Subfields allowed only for tags bigger or equal to 10");
    }
    else {
      $this->subfields = array_merge($this->subfields, $args);
    }
    return count($args) / 2;
  }

  /**
   * Return Tag number of Field.
   */
  public function tagno() {
    return $this->tagno;
  }

  /**
   * Set/Get Data of Control field.
   *
   * Sets the Data if argument given, otherwise Data returned.
   *
   * @param string Data to be set
   *
   * @return string Data of Control field if argument not given
   */
  public function data($data = "") {
    if (!$this->is_control) {
      $this
        ->_croak("data() is only allowed for tags bigger or equal to 10");
    }
    if ($data) {
      $this->data = $data;
    }
    else {
      return $this->data;
    }
  }

  /**
   * Get values of indicators.
   *
   * @param string Indicator number
   */
  public function indicator($ind) {
    if ($ind == 1) {
      return $this->ind1;
    }
    elseif ($ind == 2) {
      return $this->ind2;
    }
    else {
      $this
        ->_warn("Invalid indicator: {$ind}");
    }
  }

  /**
   * Check if Field is Control field.
   *
   * @return bool True or False
   */
  public function is_control() {
    return $this->is_control;
  }

  /**
   * Get the value of a subfield.
   *
   * Return of the value of the given subfield, if exists.
   *
   * @param string Name of subfield
   *
   * @return string|FALSE Value of the subfield if exists, otherwise FALSE
   */
  public function subfield($code, $repeatable = FALSE) {
    if (array_key_exists($code, $this->subfields)) {
      return $repeatable ? $this->subfields[$code] : $this->subfields[$code][0];
    }
    else {
      return $repeatable ? array() : FALSE;
    }
  }

  /**
   * Return array of subfields.
   *
   * @return array Array of subfields
   */
  public function subfields() {
    return $this->subfields;
  }

  /**
   * Update Field.
   *
   * Update Field with given array of arguments.
   *
   * @param array Array of key->value pairs of data
   */
  public function update() {

    // Process arguments.
    $args = func_get_args();
    if (count($args) == 1 && is_array($args[0])) {
      $args = $args[0];
    }
    if ($this->is_control) {
      $this->data = array_shift($args);
    }
    else {
      foreach ($args as $subfield => $value) {
        if ($subfield == "ind1") {
          $this->ind1 = $value;
        }
        elseif ($subfield == "ind2") {
          $this->ind2 = $value;
        }
        else {
          $this->subfields[$subfield] = $value;
        }
      }
    }
  }

  /**
   * Replace Field with given Field.
   *
   * @param Field Field to replace with
   */
  public function replace_with($obj) {
    if (strtolower(get_class($obj)) == "field") {
      $this->tagno = $obj->tagno;
      $this->ind1 = $obj->ind1;
      $this->ind2 = $obj->ind2;
      $this->subfields = $obj->subfields;
      $this->is_control = $obj->is_control;
      $this->warn = $obj->warn;
      $this->data = $obj->data;
    }
    else {
      $this
        ->_croak(sprintf("Argument must be Field-object, but was '%s'", get_class($obj)));
    }
  }

  /**
   * Clone Field.
   *
   * @return Field Cloned Field object
   */
  public function make_clone() {
    if ($this->is_control) {
      return new Field($this->tagno, $this->data);
    }
    else {
      return new Field($this->tagno, $this->ind1, $this->ind2, $this->subfields);
    }
  }

  /**
   * ========== OUTPUT FUNCTIONS ==========.
   */

  /**
   * Return Field formatted.
   *
   * Return Field as string, formatted in a similar fashion to the
   * MARC::Record formatted() functio in Perl.
   *
   * @return string Formatted output of Field
   */
  public function formatted() {

    // Variables.
    $lines = array();

    // Process.
    if ($this->is_control) {
      return sprintf("%3s     %s", $this->tagno, $this->data);
    }
    else {
      $pre = sprintf("%3s %1s%1s", $this->tagno, $this->ind1, $this->ind2);
    }

    // Process subfields.
    foreach ($this->subfields as $subfield => $value) {
      $lines[] = sprintf("%6s _%1s%s", $pre, $subfield, $value);
      $pre = "";
    }
    return join("\n", $lines);
  }

  /**
   * Return Field in Raw MARC.
   *
   * Return the Field formatted in Raw MARC for saving into MARC files.
   *
   * @return string Raw MARC
   */
  public function raw() {
    if ($this->is_control) {
      return $this->data . END_OF_FIELD;
    }
    else {
      $subfields = array();
      foreach ($this->subfields as $subfield => $value) {
        $subfields[] = SUBFIELD_INDICATOR . $subfield . $value;
      }
      return $this->ind1 . $this->ind2 . implode("", $subfields) . END_OF_FIELD;
    }
  }

  /**
   * Return Field as String.
   *
   * Return Field formatted as String, with either all subfields or special
   * subfields as specified.
   *
   * @return string Formatted as String
   */
  public function string($fields = "") {
    $matches = array();
    if ($fields) {
      for ($i = 0; $i < strlen($fields); $i++) {
        if (array_key_exists($fields[$i], $this->subfields)) {
          $matches[] = $this->subfields[$fields[$i]];
        }
      }
    }
    else {
      $matches = $this->subfields;
    }
    return implode(" ", $matches);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Field::$data public property Value of field, if field is a Control field.
Field::$ind1 public property Value of the first indicator.
Field::$ind2 public property Value of the second indicator.
Field::$is_control public property Specify if the Field is a Control field.
Field::$subfields public property Array of subfields.
Field::$tagno public property The tag name of the Field.
Field::$warn public property Array of warnings.
Field::add_subfields public function Add subfield.
Field::data public function Set/Get Data of Control field.
Field::formatted public function Return Field formatted.
Field::indicator public function Get values of indicators.
Field::is_control public function Check if Field is Control field.
Field::make_clone public function Clone Field.
Field::raw public function Return Field in Raw MARC.
Field::replace_with public function Replace Field with given Field.
Field::string public function Return Field as String.
Field::subfield public function Get the value of a subfield.
Field::subfields public function Return array of subfields.
Field::tagno public function Return Tag number of Field.
Field::update public function Update Field.
Field::warnings public function Return an array of warnings.
Field::_croak public function Croaking function.
Field::_warn public function Fuction to issue warnings.
Field::__construct public function Field init function.