scriptotek / php-marc

Simple interface for working with MARC records using the File_MARC package
MIT License
51 stars 11 forks source link

Feature Request: Editing the fields and full documentation #13

Open yfhui opened 5 years ago

yfhui commented 5 years ago

This plug in is excellent, but can we have a documentation for this plugin, or some example for us to read? So that it makes us easier to use

Other than that, I hope that this plug can provide interface for us to edit the fields. This plug in will be perfect if this function is implemented.

Hopefully I am not mistakenly missed those feature and actually they had existed in the plug in.

danmichaelo commented 5 years ago

Hi @huiyunfung

Editing is indeed possible using File_MARC, but I agree that it should be documented. Here's an example to get you started:

<?php

require('vendor/autoload.php');

use Scriptotek\Marc\Record;

$source = '<?xml version="1.0" encoding="UTF-8" ?>
  <record xmlns="http://www.loc.gov/MARC21/slim">
    <leader>99999cam a2299999 u 4500</leader>
    <controlfield tag="001">98218834x</controlfield>
    <datafield tag="020" ind1=" " ind2=" ">
      <subfield code="a">8200424421</subfield>
      <subfield code="q">h.</subfield>
      <subfield code="c">Nkr 98.00</subfield>
    </datafield>
  </record>';

$record = Record::fromString($source);

$field = $record->query('020')->first();  // instance of Scriptotek\Marc\Fields\Field, wrapper for File_MARC_Data_Field

// Deleting a subfield
$subfield = $field->getSubfield('q');   // instance of File_MARC_Subfield
$field->deleteSubfield($subfield);

// Updating a subfield
$subfield = $field->getSubfield('c');   // instance of File_MARC_Subfield
$subfield->setData('New value');

// Adding a subfield
$newSubfield = new File_MARC_Subfield('9', 'Local value');
$field->appendSubfield($newSubfield);

echo $record->toXML();

For more options, I recommend checking out the source code of File_MARC (vendor/pear/file_marc). It comes with a few examples, and the source code is very well documented.

reza0770 commented 11 months ago

What are the other methods of the record class? You have given XML as an example, but what are the rest of the methods? Especially those that convert into binary marc?