|
| 1 | +# scriptotek/marc |
| 2 | + |
| 3 | +This is a small package that provides a simple interface to parsing |
| 4 | +MARC records using the [File_MARC package](https://github.com/pear/File_MARC). |
| 5 | + |
| 6 | +The package has only been tested with XML encoded MARC 21. |
| 7 | +It should likely support everything File_MARC supports, but that |
| 8 | +remains to be tested. |
| 9 | + |
| 10 | +## Installation using Composer: |
| 11 | + |
| 12 | +``` |
| 13 | +composer require scriptotek/marc dev-master |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage examples |
| 17 | + |
| 18 | +### Records from a file or string |
| 19 | + |
| 20 | +```php |
| 21 | +use Scriptotek\Marc\Collection; |
| 22 | + |
| 23 | +$collection = Collection::fromFile($someFileName); |
| 24 | +foreach ($collection->records as $record) { |
| 25 | + echo $record->getField('250')->getSubfield('a') . "\n"; |
| 26 | +} |
| 27 | +``` |
| 28 | +It should detect if the data is Binary MARC or XML. |
| 29 | +If you have the data as a string, use |
| 30 | +`Collection::fromFile()` instead. |
| 31 | + |
| 32 | +### Records from SRU/OAI-PMH response |
| 33 | + |
| 34 | +The package makes it easy to handle records from an SRU or OAI/PMH response. |
| 35 | + |
| 36 | +```php |
| 37 | +$response = file_get_contents('http://lx2.loc.gov:210/NLSBPH?' . http_build_query(array( |
| 38 | + 'operation' => 'searchRetrieve', |
| 39 | + 'version' => '1.1', |
| 40 | + 'query' => 'dc.publisher=CNIB%20AND%20dc.date=2005', |
| 41 | + 'maximumRecords' => '10', |
| 42 | + 'recordSchema' => 'marcxml' |
| 43 | +)); |
| 44 | + |
| 45 | +$collection = Collection::fromSruResponse($response); |
| 46 | +foreach ($collection->records as $record) { |
| 47 | + echo $record->getField('250')->getSubfield('a') . "\n"; |
| 48 | +} |
| 49 | + |
| 50 | +``` |
| 51 | + |
| 52 | +### Using MARC spec |
| 53 | + |
| 54 | +To easily look up a MARC (sub)field, you can use the MARC spec syntax provided |
| 55 | +by the [php-marc-spec package](https://github.com/MARCspec/php-marc-spec): |
| 56 | + |
| 57 | +```php |
| 58 | +use Scriptotek\Marc\Collection; |
| 59 | + |
| 60 | +$collection = Collection::from($someMarcDataOrFile); |
| 61 | + |
| 62 | +foreach ($collection->records as $record) { |
| 63 | + echo $record->get('250$a'); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Convenience methods for handling common fields |
| 68 | + |
| 69 | +The `Record` class has been extended with a few convenience methods to make |
| 70 | +handling of everyday tasks easier, in the spirit of |
| 71 | +[pymarc](https://github.com/edsu/pymarc). These generally make some |
| 72 | +assumptions, for instance that a compound subject string should be joined using |
| 73 | +a colon character. |
| 74 | +These assumptions may or may not meet *your* expectations. You should inspect |
| 75 | +the relevant field class before using it. |
| 76 | + |
| 77 | +```php |
| 78 | +use Scriptotek\Marc\Record; |
| 79 | + |
| 80 | +$source = '<?xml version="1.0" encoding="UTF-8" ?> |
| 81 | + <record xmlns="info:lc/xmlns/marcxchange-v1"> |
| 82 | + <leader>99999cam a2299999 u 4500</leader> |
| 83 | + <controlfield tag="001">98218834x</controlfield> |
| 84 | + <datafield tag="020" ind1=" " ind2=" "> |
| 85 | + <subfield code="a">8200424421</subfield> |
| 86 | + <subfield code="q">h.</subfield> |
| 87 | + <subfield code="c">Nkr 98.00</subfield> |
| 88 | + </datafield> |
| 89 | + </record>'; |
| 90 | + |
| 91 | +$record = Record::from($source); |
| 92 | +echo $record->isbns[0]; |
| 93 | + |
| 94 | +``` |
0 commit comments