Commit 085502e3 authored by David Maus's avatar David Maus
Browse files

Allow custom PicaPlainParser implementations

* src/HAB/Pica/Reader/PicaPlainReader.php: Make the parser configurable.
* src/HAB/Pica/Parser/PicaPlainParser.php: Use instance methods instead
  of static methods, implement interface.
* src/HAB/Pica/Parser/PicaPlainParserInterface.php: New interface for
  PicaPlain parsers.
parent 8993a460
......@@ -28,38 +28,27 @@ namespace HAB\Pica\Parser;
use RuntimeException;
class PicaPlainParser
class PicaPlainParser implements PicaPlainParserInterface
{
/**
* Return array representation of the field encoded in a line.
*
* @throws RuntimeException Invalid characters in line
*
* @param string $line PicaPlain record line
* @return array
* {@inheritDoc}
*/
public static function parseField ($line)
public function parseField ($line)
{
$field = array('subfields' => array());
$match = array();
if (preg_match('#^([012][0-9]{2}[A-Z@])(/([0-9]{2}))? (\$.*)$#Du', $line, $match)) {
$field = array('tag' => $match[1],
'occurrence' => $match[3] ?: null,
'subfields' => self::parseSubfields($match[4]));;
'subfields' => $this->parseSubfields($match[4]));;
} else {
throw new RuntimeException("Invalid characters in PicaPlain record at line: {$line}");
}
return $field;
}
/**
* Return array of array representations of the subfields encode in argument.
*
* @param string $str Encoded subfields
* @return array
*/
public static function parseSubfields ($str)
public function parseSubfields ($str)
{
$subfields = array();
$subfield = null;
......
<?php
/**
* This file is part of PicaReader.
*
* PicaReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PicaReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PicaReader. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Pica\Parser;
/**
* Interface of PicaPlain parsers.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface PicaPlainParserInterface
{
/**
* Return array representation of the field encoded in a line.
*
* @throws RuntimeException Invalid characters in line
*
* @param string $line PicaPlain record line
* @return array
*/
public function parseField ($line);
/**
* Return array of array representations of the subfields encode in argument.
*
* @param string $str Encoded subfields
* @return array
*/
public function parseSubfields ($str);
}
\ No newline at end of file
......@@ -26,6 +26,7 @@
namespace HAB\Pica\Reader;
use HAB\Pica\Parser\PicaPlainParserInterface;
use HAB\Pica\Parser\PicaPlainParser;
class PicaPlainReader extends Reader
......@@ -38,6 +39,23 @@ class PicaPlainReader extends Reader
*/
protected $_data;
/**
* Parser instance.
*
* @var PicaPlainParser
*/
private $_parser;
/**
* Constructor.
*
* @param PicaPlainParserInterface $parser Optional parser instance
* @return void
*/
public function __construct (PicaPlainParserInterface $parser = null)
{
$this->_parser = $parser ?: new PicaPlainParser();
}
/**
* Open the reader with input stream.
......@@ -60,7 +78,7 @@ class PicaPlainReader extends Reader
$record = array('fields' => array());
do {
$line = current($this->_data);
$record['fields'] []= PicaPlainParser::parseField($line);
$record['fields'] []= $this->_parser->parseField($line);
} while (next($this->_data));
next($this->_data);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment