/** * {@inheritdoc} * * @throws \InvalidArgumentException * @throws InvalidMARCspecException */ public function __construct($subfieldspec = null) { if (is_null($subfieldspec)) { return; } if (!is_string($subfieldspec)) { throw new \InvalidArgumentException('Method only accepts string as argument. ' . gettype($subfieldspec) . ' given.'); } $argLength = strlen($subfieldspec); if (0 === $argLength) { throw new InvalidMARCspecException(InvalidMARCspecException::SF . InvalidMARCspecException::MISSINGTAG, $subfieldspec); } if (1 == $argLength) { $subfieldspec = '$' . $subfieldspec; } if (1 < $argLength) { if ('-' == $subfieldspec[1]) { // assuming subfield range throw new InvalidMARCspecException(InvalidMARCspecException::SF . InvalidMARCspecException::SFRANGE, $subfieldspec); } } if (preg_match('/\\{.*\\}$/', $subfieldspec)) { throw new InvalidMARCspecException(InvalidMARCspecException::SF . InvalidMARCspecException::DETECTEDSS, $subfieldspec); } $parser = new MARCspecParser(); $subfield = $parser->subfieldToArray($subfieldspec); $this->setTag($subfield['subfieldtag']); if (array_key_exists('index', $subfield)) { $_pos = MARCspec::validatePos($subfield['index']); $this->setIndexStartEnd($_pos[0], $_pos[1]); } else { // as of MARCspec 3.2.2 spec without index is always an abbreviation $this->setIndexStartEnd(0, '#'); } if (array_key_exists('charpos', $subfield)) { $_chars = MARCspec::validatePos($subfield['charpos']); $this->setCharStartEnd($_chars[0], $_chars[1]); } }
/** * {@inheritdoc} * * @throws InvalidMARCspecException */ public function __construct($fieldspec = null) { if (is_null($fieldspec)) { return; } $this->checkIfString($fieldspec); $spec = trim($fieldspec); $specLength = strlen($fieldspec); // check string length if (3 > $specLength) { throw new InvalidMARCspecException(InvalidMARCspecException::FS . InvalidMARCspecException::MINIMUM3, $fieldspec); } if (preg_match('/\\s/', $fieldspec)) { throw new InvalidMARCspecException(InvalidMARCspecException::FS . InvalidMARCspecException::SPACE, $fieldspec); } if ($strpos = strpos('{', $fieldspec)) { throw new InvalidMARCspecException(InvalidMARCspecException::FS . InvalidMARCspecException::DETECTEDSS, $fieldspec); } $parser = new MARCspecParser(); $parser->fieldToArray($fieldspec); if (array_key_exists('subfields', $parser->parsed)) { throw new InvalidMARCspecException(InvalidMARCspecException::FS . InvalidMARCspecException::DETECTEDSF, $fieldspec); } $this->setTag($parser->field['tag']); if (array_key_exists('index', $parser->field)) { $_pos = MARCspec::validatePos($parser->field['index']); $this->setIndexStartEnd($_pos[0], $_pos[1]); } else { // as of MARCspec 3.2.2 spec without index is always an abbreviation $this->setIndexStartEnd(0, '#'); } if (array_key_exists('indicators', $parser->field)) { $this->setIndicators($parser->field['indicators']); } elseif (array_key_exists('charpos', $parser->field)) { $_chars = MARCspec::validatePos($parser->field['charpos']); $this->setCharStartEnd($_chars[0], $_chars[1]); } }
/** * assert same specs. */ public function testValidMarcSpec3() { $field = new Field('245'); $marcSpec = MARCspec::setField($field); $marcSpec['subfields'] = '$d{$c/#=\\.}{?$a}'; $_subfields = $marcSpec['subfields']; $this->assertSame(1, count($_subfields)); $leftFieldTag = $marcSpec['d'][0]['subSpecs'][0]['leftSubTerm']['field']['tag']; $this->assertSame('245', $leftFieldTag); $rightSubfieldTag = $marcSpec['d'][0]['subSpecs'][1]['rightSubTerm']['subfields'][0]['tag']; $this->assertSame('.', $marcSpec['d'][0]['subSpecs'][0]['rightSubTerm']['comparable']); }