diff --git a/src/HAB/Pica/Parser/PicaPlainParser.php b/src/HAB/Pica/Parser/PicaPlainParser.php index c35ce275af7011652b78c7074414fd50d57bdf93..daea2352effdd9819ce32e0d58b1ff4337ebb5dc 100644 --- a/src/HAB/Pica/Parser/PicaPlainParser.php +++ b/src/HAB/Pica/Parser/PicaPlainParser.php @@ -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; diff --git a/src/HAB/Pica/Parser/PicaPlainParserInterface.php b/src/HAB/Pica/Parser/PicaPlainParserInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..66368140eb3da0764972952d782d727148cea6f5 --- /dev/null +++ b/src/HAB/Pica/Parser/PicaPlainParserInterface.php @@ -0,0 +1,52 @@ +<?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 diff --git a/src/HAB/Pica/Reader/PicaPlainReader.php b/src/HAB/Pica/Reader/PicaPlainReader.php index 3f428eb8ac7310650db183df24708a9ede605711..0825c6285719c6627979ec8178369808638413c4 100644 --- a/src/HAB/Pica/Reader/PicaPlainReader.php +++ b/src/HAB/Pica/Reader/PicaPlainReader.php @@ -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); }