From 40e13b34c16ce4e536591ab3e8815f2945977b9c Mon Sep 17 00:00:00 2001 From: Carsten Klee Date: Fri, 4 Dec 2015 04:50:13 -0500 Subject: [PATCH] Applied fixes from StyleCI --- ComparisonString.php | 72 ++-- ComparisonStringInterface.php | 81 +++-- Exception/InvalidMARCspecException.php | 104 +++--- Field.php | 185 +++++----- FieldInterface.php | 163 ++++----- MARCspec.php | 211 ++++++------ MARCspecInterface.php | 58 ++-- MARCspecParser.php | 120 +++---- PositionOrRange.php | 151 ++++----- PositionOrRangeInterface.php | 176 ++++------ SpecIterator.php | 7 +- SubSpec.php | 74 ++-- SubSpecInterface.php | 45 ++- Subfield.php | 161 +++++---- SubfieldInterface.php | 198 +++++------ Test/ComparisonStringTest.php | 64 ++-- Test/FieldTest.php | 451 ++++++++++++------------- Test/MarcSpecTest.php | 289 ++++++++-------- Test/SingleTest.php | 19 +- Test/SubSpecTest.php | 37 +- Test/SubfieldTest.php | 319 +++++++++-------- Test/autoload.php | 9 +- 22 files changed, 1404 insertions(+), 1590 deletions(-) diff --git a/ComparisonString.php b/ComparisonString.php index 970f3f4..1e1856e 100755 --- a/ComparisonString.php +++ b/ComparisonString.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,31 +12,28 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** -* A MARCspec comparison string class -*/ + * A MARCspec comparison string class. + */ class ComparisonString implements ComparisonStringInterface, \JsonSerializable, \ArrayAccess { - /** * @var string The escaped comparison string */ private $raw; /** - * - * {@inheritdoc} - * - * @throws \InvalidArgumentException if argument is not a string or - * comparison string is not properly escaped - */ + * {@inheritdoc} + * + * @throws \InvalidArgumentException if argument is not a string or + * comparison string is not properly escaped + */ public function __construct($raw) { - if (!is_string($raw)) { throw new \InvalidArgumentException('Argument must be of type string. Got ' .gettype($raw).'.'); } - + if (false !== strpos($raw, ' ')) { throw new InvalidMARCspecException( InvalidMARCspecException::CS. @@ -45,8 +41,8 @@ public function __construct($raw) $raw ); } - - /** char of list ${}!=~?|\s must be escaped if not at index 0*/ + + /* char of list ${}!=~?|\s must be escaped if not at index 0*/ if (!preg_match('/^(.(?:[^${}!=~?| ]|(?<=\\\\)[${}!=~?|])*)$/', $raw)) { throw new InvalidMARCspecException( InvalidMARCspecException::CS. @@ -54,57 +50,59 @@ public function __construct($raw) $raw ); } - + $this->raw = $raw; } - + /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getComparable() { $comparable = str_replace('\s', ' ', $this->raw); + return stripcslashes($comparable); } - + /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getRaw() { return $this->raw; } - + /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public static function escape($arg) { - $specialChars = ['{','}','!','=','~','?']; + $specialChars = ['{', '}', '!', '=', '~', '?']; for ($i = 0; $i < count($specialChars); $i++) { $arg = str_replace($specialChars[$i], '\\'.$specialChars[$i], $arg); } + return $arg = str_replace(' ', '\s', $arg); } - + /** * {@inheritdoc} */ public function __toString() { - return "\\".$this->raw; + return '\\'.$this->raw; } - + /** * {@inheritdoc} */ public function jsonSerialize() { - return ['comparisonString'=>$this->raw]; + return ['comparisonString' => $this->raw]; } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -121,9 +119,9 @@ public function offsetExists($offset) return false; } } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -142,9 +140,9 @@ public function offsetGet($offset) throw new \UnexpectedValueException("Offset $offset does not exist."); } } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -154,9 +152,9 @@ public function offsetSet($offset, $value) { throw new \UnexpectedValueException("Offset $offset cannot be set."); } - + /** - * Access object like an associative array + * Access object like an associative array. * * @param string $offset */ diff --git a/ComparisonStringInterface.php b/ComparisonStringInterface.php index 7415603..f62ca17 100755 --- a/ComparisonStringInterface.php +++ b/ComparisonStringInterface.php @@ -4,68 +4,65 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace CK\MARCspec; /** - * MARCspec comparison string interface + * MARCspec comparison string interface. */ interface ComparisonStringInterface { - /** - * Constructor for ComparisonString - * - * @api - * - * @param string $raw The escaped comparison string - */ + * Constructor for ComparisonString. + * + * @api + * + * @param string $raw The escaped comparison string + */ public function __construct($raw); - + /** - * Get unescaped comparable string - * - * @api - * - * @return string The comparable string - */ + * Get unescaped comparable string. + * + * @api + * + * @return string The comparable string + */ public function getComparable(); - + /** - * Get raw escaped string - * - * @api - * - * @return string The escaped string - */ + * Get raw escaped string. + * + * @api + * + * @return string The escaped string + */ public function getRaw(); - + /** - * Escape a comparison string - * - * @api - * - * @param string $arg The unescaped string - * - * @return string The escaped string - */ + * Escape a comparison string. + * + * @api + * + * @param string $arg The unescaped string + * + * @return string The escaped string + */ public static function escape($arg); - + /** - * encodes ComparisonString as string - * - * @api - * - * @return string - */ + * encodes ComparisonString as string. + * + * @api + * + * @return string + */ public function __toString(); - + /** - * Serialize ComparisonString as JSON + * Serialize ComparisonString as JSON. * * @api * diff --git a/Exception/InvalidMARCspecException.php b/Exception/InvalidMARCspecException.php index 9a41af8..51ebbdc 100755 --- a/Exception/InvalidMARCspecException.php +++ b/Exception/InvalidMARCspecException.php @@ -3,71 +3,69 @@ * MARCspec is the specification of a reference, encoded as string, to a set of data from within a MARC record. * * @author Carsten Klee -* @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace CK\MARCspec\Exception; /** - * Exception is thrown if an invalid MARCspec is detected + * Exception is thrown if an invalid MARCspec is detected. */ -class InvalidMARCspecException extends \UnexpectedValueException { - - - const METHOD = 'In method '; - const ARGUMENT = 'Tried to parse '; - const UNKNOWN = 'Assuming invalid spec.'; - const MS = 'MARCspec. '; - const FS = 'Fieldspec. '; - const SF = 'Subfieldspec. '; - const PR = 'PositionOrRange. '; - const CS = 'ComparisonString. '; - const SS = 'Subspec. '; - const RANGE = 'Only ranges between "a-z", "A-Z" or "0-9" allowed.'; - const BRACKET = 'Unequal count of opening and closing brackets'; - const SPACE = 'Whitespace detected.'; +class InvalidMARCspecException extends \UnexpectedValueException +{ + const METHOD = 'In method '; + const ARGUMENT = 'Tried to parse '; + const UNKNOWN = 'Assuming invalid spec.'; + const MS = 'MARCspec. '; + const FS = 'Fieldspec. '; + const SF = 'Subfieldspec. '; + const PR = 'PositionOrRange. '; + const CS = 'ComparisonString. '; + const SS = 'Subspec. '; + const RANGE = 'Only ranges between "a-z", "A-Z" or "0-9" allowed.'; + const BRACKET = 'Unequal count of opening and closing brackets'; + const SPACE = 'Whitespace detected.'; const MISSINGFIELD = 'Cannot detect fieldspec.'; const MISSINGRIGHT = 'Right hand subTerm is missing.'; - const MINIMUM2 = 'Spec must be at least two characters long.'; - const MINIMUM3 = 'Spec must be at least three characters long.'; - const MINIMUM4 = 'Spec must be at least four characters long.'; - const LENGTH = 'Invalid spec length.'; - const LENGTH3 = 'Invalid spec length. At minimum spec must be three characters long.'; - const PREFIX = 'Missing prefixed character "$".'; - const ESCAPE = 'Unescaped character detected'; - const DETECTEDSF = 'Detected Subfield. Use method MARCspec::addSubfields to add subfields.'; - const DETECTEDSS = 'Detected Subspec. Use method addSubSpec to add subspecs.'; - const MULTISF = 'Detected more than one subfieldspecs. Use method addSubfields to add more than one subfield.'; - const INDEX = 'Invalid index detected.'; - const PRCHAR = 'For character position or range minimum one digit or character # is required.'; - const USELESS = 'Detected useless data fragment.'; - const FTAG = 'For fieldtag only "." and digits and lowercase alphabetic or digits and upper case alphabetics characters are allowed'; - const LENGTHIND = 'For indicators only two characters at are allowed.'; - const INDCHAR1 = 'At minimum one indicator must be a digit or a lowercase alphabetic character.'; - const INDCHAR2 = 'For indicators only digits, lowercase alphabetic characters and "_" are allowed.'; - const NEGATIVE = 'Ending character or index position must be equal or higher than starting character or index position.'; - const PR1 = 'Assuming index or character position or range. Minimum one character is required. None given.'; - const PR2 = 'Assuming index or character position or range. Only digits, the character # and one "-" is allowed.'; - const PR3 = 'Assuming index or character range. At least two digits or the character # must be present.'; - const PR4 = 'Assuming index or character position or range. First character must not be "-".'; - const PR5 = 'Assuming index or character position or range. Only one "-" character allowed.'; - const PR6 = 'Assuming index or character position or range. Only digits and one "-" is allowed.'; - const PR7 = 'Assuming index or character position or range. Starting index must be positive int, 0 or "#".'; - const PR8 = 'Assuming index or character position or range. Ending index must be a higher number (or equal) than starting index.'; - const MISSINGTAG = 'Unexpected empty subfield tag'; - const SFCHAR = 'For subfields only digits, lowercase alphabetic characters or one of "!"#$%&\'()*+,-./0-9:;<=>?[\]^_`a-z{}~" are allowed.'; - const SFRANGE = 'Assuming subfield range. Use CK\MARCspec::addSubfields() to add multiple subfields via a subfield range.'; + const MINIMUM2 = 'Spec must be at least two characters long.'; + const MINIMUM3 = 'Spec must be at least three characters long.'; + const MINIMUM4 = 'Spec must be at least four characters long.'; + const LENGTH = 'Invalid spec length.'; + const LENGTH3 = 'Invalid spec length. At minimum spec must be three characters long.'; + const PREFIX = 'Missing prefixed character "$".'; + const ESCAPE = 'Unescaped character detected'; + const DETECTEDSF = 'Detected Subfield. Use method MARCspec::addSubfields to add subfields.'; + const DETECTEDSS = 'Detected Subspec. Use method addSubSpec to add subspecs.'; + const MULTISF = 'Detected more than one subfieldspecs. Use method addSubfields to add more than one subfield.'; + const INDEX = 'Invalid index detected.'; + const PRCHAR = 'For character position or range minimum one digit or character # is required.'; + const USELESS = 'Detected useless data fragment.'; + const FTAG = 'For fieldtag only "." and digits and lowercase alphabetic or digits and upper case alphabetics characters are allowed'; + const LENGTHIND = 'For indicators only two characters at are allowed.'; + const INDCHAR1 = 'At minimum one indicator must be a digit or a lowercase alphabetic character.'; + const INDCHAR2 = 'For indicators only digits, lowercase alphabetic characters and "_" are allowed.'; + const NEGATIVE = 'Ending character or index position must be equal or higher than starting character or index position.'; + const PR1 = 'Assuming index or character position or range. Minimum one character is required. None given.'; + const PR2 = 'Assuming index or character position or range. Only digits, the character # and one "-" is allowed.'; + const PR3 = 'Assuming index or character range. At least two digits or the character # must be present.'; + const PR4 = 'Assuming index or character position or range. First character must not be "-".'; + const PR5 = 'Assuming index or character position or range. Only one "-" character allowed.'; + const PR6 = 'Assuming index or character position or range. Only digits and one "-" is allowed.'; + const PR7 = 'Assuming index or character position or range. Starting index must be positive int, 0 or "#".'; + const PR8 = 'Assuming index or character position or range. Ending index must be a higher number (or equal) than starting index.'; + const MISSINGTAG = 'Unexpected empty subfield tag'; + const SFCHAR = 'For subfields only digits, lowercase alphabetic characters or one of "!"#$%&\'()*+,-./0-9:;<=>?[\]^_`a-z{}~" are allowed.'; + const SFRANGE = 'Assuming subfield range. Use CK\MARCspec::addSubfields() to add multiple subfields via a subfield range.'; const MISSINGSLASH = 'Assuming subfield character position or range. Missing "/" delimiter'; - const OPERATOR = 'Operator must be one of "=" / "!=" / "~" / "!~" / "!" / "?".'; - const HINTESCAPED = 'Hint: Check for unescaped characters.'; - const CHARORIND = 'Either characterSpec or indicators are allowed.'; - const CHARANDSF = 'Either characterSpec for field or subfields are allowed.'; - + const OPERATOR = 'Operator must be one of "=" / "!=" / "~" / "!~" / "!" / "?".'; + const HINTESCAPED = 'Hint: Check for unescaped characters.'; + const CHARORIND = 'Either characterSpec or indicators are allowed.'; + const CHARANDSF = 'Either characterSpec for field or subfields are allowed.'; + public function __construct($message, $context = null) { $context = (!is_null($context)) ? "\n".self::ARGUMENT.'"'.$context.'"' : null; - - parent::__construct("Detected invalid ".$message.$context); + + parent::__construct('Detected invalid '.$message.$context); } } diff --git a/Field.php b/Field.php index 860086f..0196330 100755 --- a/Field.php +++ b/Field.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,11 +12,10 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** -* A MARCspec fieldspec class -*/ + * A MARCspec fieldspec class. + */ class Field extends PositionOrRange implements FieldInterface, \JsonSerializable, \ArrayAccess { - /** * @var string field tag */ @@ -56,26 +54,25 @@ class Field extends PositionOrRange implements FieldInterface, \JsonSerializable /** * @var array subSpec */ - private $subSpecs = array(); - + private $subSpecs = []; /** - * {@inheritdoc} - * - * @throws InvalidMARCspecException - */ + * {@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( @@ -98,11 +95,11 @@ public function __construct($fieldspec = null) $fieldspec ); } - + $parser = new MARCspecParser(); - + $parser->fieldToArray($fieldspec); - + if (array_key_exists('subfields', $parser->parsed)) { throw new InvalidMARCspecException( InvalidMARCspecException::FS. @@ -110,31 +107,30 @@ public function __construct($fieldspec = null) $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, "#"); + $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]); } } /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function setTag($arg) { if ($this->validateTag($arg)) { @@ -143,20 +139,16 @@ public function setTag($arg) } /** - * - * {@inheritdoc} - * - */ + * {@inheritdoc} + */ public function getTag() { return $this->tag; } - /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function setIndicators($arg) { $this->checkIfString($arg); @@ -177,9 +169,8 @@ public function setIndicators($arg) } /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function setIndicator1($arg) { if ($this->validateIndicators($arg)) { @@ -188,18 +179,16 @@ public function setIndicator1($arg) } /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getIndicator1() { return (isset($this->indicator1)) ? $this->indicator1 : null; } /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function setIndicator2($arg) { if ($this->validateIndicators($arg)) { @@ -208,28 +197,24 @@ public function setIndicator2($arg) } /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getIndicator2() { return (isset($this->indicator2)) ? $this->indicator2 : null; } - /** - * validate a field tag - * - * @internal - * - * @access private - * - * @param string $tag The MARC spec as string field tag - * - * @throws InvalidMARCspecException - * - * @return true if string is a valid field tag - */ + * validate a field tag. + * + * @internal + * + * @param string $tag The MARC spec as string field tag + * + * @throws InvalidMARCspecException + * + * @return true if string is a valid field tag + */ private function validateTag($tag) { if (!preg_match('/[.0-9a-z]{3,3}|[.0-9A-Z]{3,3}/', $tag)) { @@ -239,16 +224,15 @@ private function validateTag($tag) $tag ); } + return true; } /** - * validate indicators + * validate indicators. * * @internal * - * @access private - * * @param string $indicators The MARC spec as string indicators * * @throws InvalidMARCspecException @@ -265,7 +249,7 @@ private function validateIndicators($indicators) $indicators ); } - + if (preg_match('/[^a-z0-9_]/', $indicators)) { throw new InvalidMARCspecException( InvalidMARCspecException::FS. @@ -273,12 +257,13 @@ private function validateIndicators($indicators) $indicators ); } + return true; } + /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function addSubSpec($SubSpec) { if ($SubSpec instanceof SubSpecInterface) { @@ -299,17 +284,16 @@ public function addSubSpec($SubSpec) } /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getSubSpecs() { return (0 < count($this->subSpecs)) ? $this->subSpecs : null; } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function jsonSerialize() { $_fieldSpec['tag'] = $this->getTag(); @@ -346,12 +330,12 @@ public function jsonSerialize() foreach ($subSpec as $altSubSpec) { $_fieldSpec['subSpecs'][$key][] = $altSubSpec->jsonSerialize(); } - } else { $_fieldSpec['subSpecs'][$key] = $subSpec->jsonSerialize(); } } } + return $_fieldSpec; } @@ -361,42 +345,42 @@ public function jsonSerialize() public function getBaseSpec() { $fieldSpec = $this->getTag(); - + $indexStart = $this->getIndexStart(); $indexEnd = $this->getIndexEnd(); - $fieldSpec .= "[".$indexStart; - + $fieldSpec .= '['.$indexStart; + if ($indexStart !== $indexEnd) { - $fieldSpec .= "-".$indexEnd; + $fieldSpec .= '-'.$indexEnd; } - $fieldSpec .= "]"; - + $fieldSpec .= ']'; + if (($charStart = $this->getCharStart()) !== null) { $charEnd = $this->getCharEnd(); - if ($charStart === 0 && $charEnd === "#") { - // use abbreviation + if ($charStart === 0 && $charEnd === '#') { + // use abbreviation } else { - $fieldSpec .= "/".$charStart; + $fieldSpec .= '/'.$charStart; if ($charEnd !== $charStart) { - $fieldSpec .= "-".$charEnd; + $fieldSpec .= '-'.$charEnd; } } } - - $indicator1 = ($this->getIndicator1() !== null) ? $this->indicator1 : "_"; - $indicator2 = ($this->getIndicator2() !== null) ? $this->indicator2 : "_"; + + $indicator1 = ($this->getIndicator1() !== null) ? $this->indicator1 : '_'; + $indicator2 = ($this->getIndicator2() !== null) ? $this->indicator2 : '_'; $indicators = $indicator1.$indicator2; - if ($indicators != "__") { - $fieldSpec .= "_".$indicators; + if ($indicators != '__') { + $fieldSpec .= '_'.$indicators; } - + return $fieldSpec; } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function __toString() { $fieldSpec = $this->getBaseSpec(); @@ -413,16 +397,17 @@ public function __toString() } } } + return $fieldSpec; } /** - * Access object like an associative array + * Access object like an associative array. * * @api * * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength - * |indicator1|indicator2|subSpecs + * |indicator1|indicator2|subSpecs */ public function offsetExists($offset) { @@ -463,12 +448,12 @@ public function offsetExists($offset) } /** - * Access object like an associative array + * Access object like an associative array. * * @api * * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength - * |indicator1|indicator2|subSpecs + * |indicator1|indicator2|subSpecs */ public function offsetGet($offset) { @@ -509,12 +494,12 @@ public function offsetGet($offset) } /** - * Access object like an associative array + * Access object like an associative array. * * @api * * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength - * |indicator1|indicator2|subSpecs + * |indicator1|indicator2|subSpecs */ public function offsetSet($offset, $value) { @@ -522,7 +507,7 @@ public function offsetSet($offset, $value) case 'tag': $this->setTag($value); break; - + case 'indexStart': $this->setIndexStartEnd($value); break; @@ -536,7 +521,7 @@ public function offsetSet($offset, $value) break; case 'indexLength': - throw new \UnexpectedValueException("indexLength is always calculated."); + throw new \UnexpectedValueException('indexLength is always calculated.'); break; case 'charStart': @@ -552,7 +537,7 @@ public function offsetSet($offset, $value) break; case 'charLength': - throw new \UnexpectedValueException("CharLength is always calculated."); + throw new \UnexpectedValueException('CharLength is always calculated.'); break; case 'indicator1': @@ -573,7 +558,7 @@ public function offsetSet($offset, $value) } /** - * Access object like an associative array + * Access object like an associative array. */ public function offsetUnset($offset) { diff --git a/FieldInterface.php b/FieldInterface.php index dca22c5..5724936 100755 --- a/FieldInterface.php +++ b/FieldInterface.php @@ -3,122 +3,101 @@ * MARCspec is the specification of a reference, encoded as string, to a set of data from within a MARC record. * * @author Carsten Klee -* @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace CK\MARCspec; /** -* MARCspec field interface -*/ + * MARCspec field interface. + */ interface FieldInterface { - /** - * Constructor for MARCspec field + * Constructor for MARCspec field. * * @api * * @param string $fieldspec The field spec */ public function __construct($fieldspec); - - + /** - * - * Set the field tag - * - * Provided param gets validated - * - * @api - * - * @param string $arg The field tag - */ + * Set the field tag. + * + * Provided param gets validated + * + * @api + * + * @param string $arg The field tag + */ public function setTag($arg); - + /** - * - * Get the field tag - * - * @api - * - * @return string $fieldTag The field tag - */ + * Get the field tag. + * + * @api + * + * @return string $fieldTag The field tag + */ public function getTag(); - + /** - * - * Set indicators - * - * @api - * - * @access public - * - * @param string $arg - */ + * Set indicators. + * + * @api + * + * @param string $arg + */ public function setIndicators($arg); - + /** - * - * Set indicator 1 - * - * @api - * - * @access public - * - * @param string $arg - */ + * Set indicator 1. + * + * @api + * + * @param string $arg + */ public function setIndicator1($arg); - + /** - * - * Get indicator 1 - * - * @api - * - * @access public - * - * @return null|string $indicator1 The indicator 1 - */ + * Get indicator 1. + * + * @api + * + * @return null|string $indicator1 The indicator 1 + */ public function getIndicator1(); - + /** - * - * Set indicator 2 - * - * @api - * - * @access public - * - * @param string $arg - */ + * Set indicator 2. + * + * @api + * + * @param string $arg + */ public function setIndicator2($arg); - + /** - * - * Get indicator 2 - * - * @api - * - * @access public - * - * @return null|string $indicator2 The indicator 2 - */ + * Get indicator 2. + * + * @api + * + * @return null|string $indicator2 The indicator 2 + */ public function getIndicator2(); - + /** - * get array of subspecs + * get array of subspecs. * * @api * * @return null|array */ public function getSubSpecs(); - - /** - * add a subspec to the array of subspecs + + /** + * add a subspec to the array of subspecs. * * @api * @@ -127,27 +106,27 @@ public function getSubSpecs(); * @return null|array */ public function addSubSpec($SubSpec); - - /** - * Get the basic spec without subspecs - * - * @api - * - * @return string - */ + + /** + * Get the basic spec without subspecs. + * + * @api + * + * @return string + */ public function getBaseSpec(); - + /** - * encodes Field as string + * encodes Field as string. * * @api * * @return string */ public function __toString(); - + /** - * Serialize Field as JSON + * Serialize Field as JSON. * * @api * diff --git a/MARCspec.php b/MARCspec.php index 01b8279..bd42207 100644 --- a/MARCspec.php +++ b/MARCspec.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee -* @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,29 +12,27 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** -* Class to decode, validate and encode MARC spec as string. -* For Specification of MARC spec as string see -* -*/ + * Class to decode, validate and encode MARC spec as string. + * For Specification of MARC spec as string see + * . + */ class MARCspec implements MARCspecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate { - /** - * @var Field The field object - */ + * @var Field The field object + */ private $field; /** - * @var array Array of subfields - */ + * @var array Array of subfields + */ private $subfields = []; - /** - * {@inheritdoc} - * - * @throws InvalidMARCspecException - */ + * {@inheritdoc} + * + * @throws InvalidMARCspecException + */ public function __construct($spec) { if ($spec instanceof FieldInterface) { @@ -60,40 +57,40 @@ public function __construct($spec) $spec ); } - + $parser = new MARCspecParser($spec); $this->field = new Field(); - + $this->field->setTag($parser->field['tag']); - + if (array_key_exists('index', $parser->field)) { $_pos = $this->validatePos($parser->field['index']); - + $this->field->setIndexStartEnd($_pos[0], $_pos[1]); } else { // as of MARCspec 3.2.2 spec without index is always an abbreviation - $this->field->setIndexStartEnd(0, "#"); + $this->field->setIndexStartEnd(0, '#'); } - + if (array_key_exists('indicators', $parser->field)) { $this->field->setIndicators($parser->field['indicators']); } elseif (array_key_exists('charpos', $parser->field)) { $_chars = $this->validatePos($parser->field['charpos']); - + $this->field->setCharStartEnd($_chars[0], $_chars[1]); } - + if (array_key_exists('subspecs', $parser->field)) { foreach ($parser->field['subspecs'] as $subspec) { if (!array_key_exists('operator', $subspec)) { foreach ($subspec as $orSubSpec) { $_subSpecs[] = $this->createSubSpec($orSubSpec); } - $this->field->addSubSpec($_subSpecs); + $this->field->addSubSpec($_subSpecs); } else { $Subspec = $this->createSubSpec($subspec); - + $this->field->addSubSpec($Subspec); } } @@ -106,15 +103,15 @@ public function __construct($spec) } } } - + /** * {@inheritdoc} */ public static function setField(FieldInterface $field) { - return new MARCspec($field); + return new self($field); } - + /** * {@inheritdoc} */ @@ -122,7 +119,7 @@ public function getField() { return $this->field; } - + /** * {@inheritdoc} */ @@ -130,7 +127,7 @@ public function getSubfields() { return (0 < count($this->subfields)) ? $this->subfields : null; } - + /** * {@inheritdoc} * @@ -140,25 +137,28 @@ public function getSubfield($arg) { if (1 < strlen($arg)) { throw new \UnexpectedValueException( - 'Method only allows argument to be 1 character long. Got '. strlen($arg) + 'Method only allows argument to be 1 character long. Got '.strlen($arg) ); } - + if (0 < count($this->subfields)) { foreach ($this->subfields as $subfield) { if ($subfield->getTag() == $arg) { $_subfields[] = $subfield; } } + return $_subfields; } - - return null; + + return; } + /** - * Creates and adds a single subfield from the MARCspecParser result - * @param array $_subfield The MARCspecParser result array - */ + * Creates and adds a single subfield from the MARCspecParser result. + * + * @param array $_subfield The MARCspecParser result array + */ private function addSubfield($_subfield) { if (array_key_exists('subfieldtagrange', $_subfield)) { @@ -166,30 +166,30 @@ private function addSubfield($_subfield) } else { $_subfieldRange[] = $_subfield['subfieldtag']; } - + foreach ($_subfieldRange as $subfieldTag) { $Subfield = new Subfield(); - + $Subfield->setTag($subfieldTag); - + if (array_key_exists('index', $_subfield)) { $_pos = $this->validatePos($_subfield['index']); - + $Subfield->setIndexStartEnd($_pos[0], $_pos[1]); } else { // as of MARCspec 3.2.2 spec without index is always an abbreviation - $Subfield->setIndexStartEnd(0, "#"); + $Subfield->setIndexStartEnd(0, '#'); } - + if (array_key_exists('charpos', $_subfield)) { $_chars = $this->validatePos($_subfield['charpos']); - + $Subfield->setCharStartEnd($_chars[0], $_chars[1]); } - + if (array_key_exists('subspecs', $_subfield)) { $_subSpecs = []; - + foreach ($_subfield['subspecs'] as $subspec) { if (!array_key_exists('operator', $subspec)) { foreach ($subspec as $orSubSpec) { @@ -198,20 +198,20 @@ private function addSubfield($_subfield) $Subfield->addSubSpec($_subSpecs); } else { $Subspec = $this->createSubSpec($subspec, $Subfield); - + $Subfield->addSubSpec($Subspec); } } } - + $this->addSubfields($Subfield); } } + /** * {@inheritdoc} * * @throws InvalidMARCspecException - * */ public function addSubfields($subfields) { @@ -234,9 +234,9 @@ public function addSubfields($subfields) $subfields ); } - + $parser = new MARCspecParser(); - + $_subfieldSpecs = $parser->matchSubfields($subfields); foreach ($_subfieldSpecs as $subfieldSpec) { @@ -244,20 +244,18 @@ public function addSubfields($subfields) } } } - - /** - * Parses subfield ranges into single subfields - * - * @internal - * - * @param string $arg The assumed subfield range - * - * @return array $_range[string] An array of subfield tags - * - * @throws InvalidMARCspecException - */ + * Parses subfield ranges into single subfields. + * + * @internal + * + * @param string $arg The assumed subfield range + * + * @throws InvalidMARCspecException + * + * @return array $_range[string] An array of subfield tags + */ private function handleSubfieldRanges($arg) { if (strlen($arg) < 3) { @@ -299,13 +297,9 @@ private function handleSubfieldRanges($arg) return range($arg[0], $arg[2]); } - - - - /** - * checks if argument is a string + * checks if argument is a string. * * @internal * @@ -316,27 +310,26 @@ private function handleSubfieldRanges($arg) private function checkIfString($arg) { if (!is_string($arg)) { - throw new \InvalidArgumentException("Method only accepts string as argument. " . - gettype($arg)." given."); + throw new \InvalidArgumentException('Method only accepts string as argument. '. + gettype($arg).' given.'); } } - + /** - * validate a position or range + * validate a position or range. * - * @access protected * * @param string $pos The position or range * * @throws InvalidMARCspecException * * @return array $_pos[string] An numeric array of character or index positions. - * $_pos[1] might be empty. + * $_pos[1] might be empty. */ public static function validatePos($pos) { $posLength = strlen($pos); - + if (1 > $posLength) { throw new InvalidMARCspecException( InvalidMARCspecException::PR. @@ -344,7 +337,6 @@ public static function validatePos($pos) $pos ); } - if (preg_match('/[^0-9\-#]/', $pos)) { // alphabetic characters etc. are not valid throw new InvalidMARCspecException( @@ -353,15 +345,15 @@ public static function validatePos($pos) $pos ); } - - if (strpos($pos, '-') === $posLength-1) { // something like 123- is not valid + + if (strpos($pos, '-') === $posLength - 1) { // something like 123- is not valid throw new InvalidMARCspecException( InvalidMARCspecException::PR. InvalidMARCspecException::PR3, $pos ); } - + if (0 === strpos($pos, '-')) { // something like -123 ist not valid throw new InvalidMARCspecException( InvalidMARCspecException::PR. @@ -369,7 +361,7 @@ public static function validatePos($pos) $pos ); } - + if (strpos($pos, '-') !== strrpos($pos, '-')) { // only one - is allowed throw new InvalidMARCspecException( InvalidMARCspecException::PR. @@ -377,9 +369,9 @@ public static function validatePos($pos) $pos ); } - + $_pos = explode('-', $pos); - + if (2 < count($_pos)) { throw new InvalidMARCspecException( InvalidMARCspecException::PR. @@ -387,24 +379,25 @@ public static function validatePos($pos) $pos ); } - + if (1 == count($_pos)) { $_pos[1] = null; } + return $_pos; } /** - * Creates SubSpecs + * Creates SubSpecs. * * @internal * * @param array $_subTerms Array representation of a subspec * @param SubfieldInterface|null The subfield is optional * - * @return SubSpecInterface Instance of SubSpecInterface - * * @throws InvalidMARCspecException + * + * @return SubSpecInterface Instance of SubSpecInterface */ private function createSubSpec($_subTerms, $Subfield = null) { @@ -416,31 +409,31 @@ private function createSubSpec($_subTerms, $Subfield = null) } $handleSubTerm = function ($subTerm) use ($fieldContext, $context) { - + if ('\\' == $subTerm[0]) { // is a comparisonString return new ComparisonString(substr($subTerm, 1)); } else { - if (strpos("[/_$", $subTerm[0]) && is_null($context)) { + if (strpos('[/_$', $subTerm[0]) && is_null($context)) { throw new InvalidMARCspecException( InvalidMARCspecException::SS. InvalidMARCspecException::MISSINGFIELD, $subTerm ); } - + switch ($subTerm[0]) { case '_': if ($refPos = strpos($context, $subTerm[0])) { if ('$' !== substr($context, $refPos - 1, 1)) { return new MARCspec(substr($context, 0, $refPos).$subTerm); } - } + return new MARCspec($fieldContext.$subTerm); - + case '$': return new MARCspec($fieldContext.$subTerm); - + case '/': $refPos = strrpos($context, $subTerm[0]); if ($refPos) { @@ -448,8 +441,9 @@ private function createSubSpec($_subTerms, $Subfield = null) return new MARCspec(substr($context, 0, $refPos).$subTerm); } } + return new MARCspec($context.$subTerm); - + case '[': $refPos = strrpos($context, $subTerm[0]); if ($refPos) { @@ -459,21 +453,21 @@ private function createSubSpec($_subTerms, $Subfield = null) } else { throw new \RuntimeException('Abbreviated spec cannot resolved since context spec must have an index, which can\'t be found.'); } - + default: return new MARCspec($subTerm); } } }; - + $_subTermSet = []; if (array_key_exists('leftsubterm', $_subTerms)) { $_subTermSet['leftsubterm'] = $handleSubTerm($_subTerms['leftsubterm']); } else { - $_subTermSet['leftsubterm'] = new MARCspec($context); + $_subTermSet['leftsubterm'] = new self($context); } - + $_subTermSet['rightsubterm'] = $handleSubTerm($_subTerms['rightsubterm']); return new SubSpec($_subTermSet['leftsubterm'], $_subTerms['operator'], $_subTermSet['rightsubterm']); @@ -485,13 +479,14 @@ private function createSubSpec($_subTerms, $Subfield = null) public function jsonSerialize() { $_marcSpec['field'] = $this->field->jsonSerialize(); - + foreach ($this->subfields as $subfield) { $_marcSpec['subfields'][] = $subfield->jsonSerialize(); } + return $_marcSpec; } - + /** * {@inheritdoc} */ @@ -501,11 +496,12 @@ public function __toString() foreach ($this->subfields as $subfield) { $marcspec .= "$subfield"; } + return $marcspec; } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -524,8 +520,9 @@ public function offsetExists($offset) return false; } } + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -544,8 +541,9 @@ public function offsetGet($offset) return $this->getSubfield($offset); } } + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -561,8 +559,9 @@ public function offsetSet($offset, $value) throw new \UnexpectedValueException("Offset $offset cannot be set."); } } + /** - * Access object like an associative array + * Access object like an associative array. * * @param string $offset */ @@ -570,9 +569,9 @@ public function offsetUnset($offset) { throw new \BadMethodCallException("Offset $offset can not be unset."); } - + public function getIterator() { - return new SpecIterator(["field" => $this->field, "subfields" => $this->subfields]); + return new SpecIterator(['field' => $this->field, 'subfields' => $this->subfields]); } } // EOC diff --git a/MARCspecInterface.php b/MARCspecInterface.php index 1c7c4a0..e1767cc 100755 --- a/MARCspecInterface.php +++ b/MARCspecInterface.php @@ -4,83 +4,81 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace CK\MARCspec; /** - * MARCspec subfield interface + * MARCspec subfield interface. */ interface MARCspecInterface { /** - * Constructor for a MARCspec + * Constructor for a MARCspec. * * For a minimal MARCspec a field tag must be provided by default * * @api * * @param string|FieldInterface $spec The MARCspec as string or an instance of - * FieldInterface + * FieldInterface */ public function __construct($spec); - + /** - * Set the field + * Set the field. * * @param FieldInterface $field Instance of FieldInterface * * @return MARCspec Instance of MARCspec */ public static function setField(FieldInterface $field); - + /** - * Get the field + * Get the field. * * @return FieldInterface An instance of FieldInterface */ public function getField(); - + /** - * Add subfields + * Add subfields. * * @param string|SubfieldInterface $subfields The subfield spec or instance of - * SubfieldInterface + * SubfieldInterface */ public function addSubfields($subfields); - + /** - * Get array of subfields - * - * @return null|array[SubfieldInterface] $subfields The array of instances of - * SubfieldInterface - */ + * Get array of subfields. + * + * @return null|array[SubfieldInterface] $subfields The array of instances of + * SubfieldInterface + */ public function getSubfields(); - + /** - * Get array of subfields with a specific tag - * - * @param string $subfieldTag The subfield tag - * - * @return null|array $subfields [SubfieldInterface] $subfields The array of - * instances of SubfieldInterface with a specific tag - */ + * Get array of subfields with a specific tag. + * + * @param string $subfieldTag The subfield tag + * + * @return null|array $subfields [SubfieldInterface] $subfields The array of + * instances of SubfieldInterface with a specific tag + */ public function getSubfield($subfieldTag); - + /** - * Encodes MARCspec as string + * Encodes MARCspec as string. * * @api * * @return string The MARCspec as string */ public function __toString(); - + /** - * Serialize MARCspec as JSON + * Serialize MARCspec as JSON. * * @api * diff --git a/MARCspecParser.php b/MARCspecParser.php index 45a9163..27a83ad 100755 --- a/MARCspecParser.php +++ b/MARCspecParser.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,7 +12,7 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** - * Parses a MARCspec into an array + * Parses a MARCspec into an array. */ class MARCspecParser { @@ -81,47 +80,48 @@ class MARCspecParser * @var string Regex for subspec */ protected $SUBSPEC; - + /** * @var array The parsed MARCspec */ public $parsed = []; - + /** * @var array The parsed fieldspec */ public $field = []; - + /** * @var array The parsed subfieldspecs */ public $subfields = []; - + public function __construct($spec = null) { $this->setConstants(); - + if (is_null($spec)) { return; } - + $this->fieldToArray($spec); - + if (array_key_exists('subfields', $this->parsed)) { $this->subfields = $this->matchSubfields($this->parsed['subfields']); } } - + /** - * parses fieldspecs into array + * parses fieldspecs into array. * * @param string $fieldspec The fieldspec + * * @return array An Array of fieldspec */ public function fieldToArray($fieldspec) { - $_fieldGroups = ['field','tag','index','charpos','indicators','subfields']; - + $_fieldGroups = ['field', 'tag', 'index', 'charpos', 'indicators', 'subfields']; + if (!preg_match_all('/'.$this->FIELD.'/', $fieldspec, $_fieldMatches, PREG_SET_ORDER)) { throw new InvalidMARCspecException( InvalidMARCspecException::FS. @@ -139,7 +139,7 @@ public function fieldToArray($fieldspec) $fieldspec ); } - + if (strlen($this->parsed['field']) !== strlen($fieldspec)) { throw new InvalidMARCspecException( InvalidMARCspecException::FS. @@ -147,7 +147,7 @@ public function fieldToArray($fieldspec) $fieldspec ); } - + foreach ($_fieldGroups as $fieldgroup) { if (array_key_exists($fieldgroup, $this->parsed)) { $this->field[$fieldgroup] = $this->parsed[$fieldgroup]; @@ -162,7 +162,7 @@ public function fieldToArray($fieldspec) $fieldspec ); } - + if (array_key_exists('subfields', $this->field)) { throw new InvalidMARCspecException( InvalidMARCspecException::FS. @@ -170,9 +170,8 @@ public function fieldToArray($fieldspec) $fieldspec ); } - } - + if (array_key_exists('subspecs', $this->parsed)) { $_fieldSubSpecs = $this->matchSubSpecs($this->parsed['subspecs']); @@ -188,12 +187,12 @@ public function fieldToArray($fieldspec) } } } - + /** - * Matches subfieldspecs - * - * @param string $subfieldspec A string of one or more subfieldspecs - */ + * Matches subfieldspecs. + * + * @param string $subfieldspec A string of one or more subfieldspecs + */ public function matchSubfields($subfieldspec) { if (!preg_match_all('/'.$this->SUBFIELD.'/', $subfieldspec, $_subfieldMatches, PREG_SET_ORDER)) { @@ -203,7 +202,7 @@ public function matchSubfields($subfieldspec) $subfieldspec ); } - /** + /* * For each subfield (array) do anonymous function * - first filter empty elements * - second look for subspecs @@ -213,18 +212,18 @@ public function matchSubfields($subfieldspec) array_walk( $_subfieldMatches, function (&$_subfield) use (&$test) { - + $_subfield = array_filter($_subfield, 'strlen'); - + $test .= $_subfield['subfield']; - + if (array_key_exists('subspecs', $_subfield)) { $_ss = []; - + if (!$_subfieldSubSpecs = $this->matchSubSpecs($_subfield['subspecs'])) { - // TODO: raise error; + // TODO: raise error; } - + foreach ($_subfieldSubSpecs as $key => $_subfieldSubSpec) { if (1 < count($_subfieldSubSpec)) { foreach ($_subfieldSubSpec as $orSubSpec) { @@ -235,7 +234,7 @@ function (&$_subfield) use (&$test) { $_ss[] = $this->matchSubTerms($_subfieldSubSpec[0]); } } - + $_subfield['subspecs'] = $_ss; } } @@ -248,16 +247,17 @@ function (&$_subfield) use (&$test) { $subfieldspec ); } - + return $_subfieldMatches; } - + /** - * calls matchSubfields but makes sure only one subfield is present - * - * @param string $subfieldspec A subfieldspec - * @return array An Array of subfieldspec - */ + * calls matchSubfields but makes sure only one subfield is present. + * + * @param string $subfieldspec A subfieldspec + * + * @return array An Array of subfieldspec + */ public function subfieldToArray($subfieldspec) { if (!$_sf = $this->matchSubfields($subfieldspec)) { @@ -267,7 +267,7 @@ public function subfieldToArray($subfieldspec) $subfieldspec ); } - + if (1 < count($_sf)) { throw new InvalidMARCspecException( InvalidMARCspecException::SF. @@ -275,7 +275,7 @@ public function subfieldToArray($subfieldspec) $subfieldspec ); } - + if ($_sf[0]['subfield'] !== $subfieldspec) { throw new InvalidMARCspecException( InvalidMARCspecException::SF. @@ -283,16 +283,17 @@ public function subfieldToArray($subfieldspec) $subfieldspec ); } - + return $_sf[0]; } - + /** - * parses subspecs into an array - * - * @param string $subSpecs One or more subspecs - * @return array Array of subspecs - */ + * parses subspecs into an array. + * + * @param string $subSpecs One or more subspecs + * + * @return array Array of subspecs + */ private function matchSubSpecs($subSpecs) { $_subSpecs = []; @@ -315,15 +316,17 @@ private function matchSubSpecs($subSpecs) ); } } + return $_subSpecs; } - + /** - * Parses a single SubSpec into sunTerms - * - * @param string $subSpec A single SubSpec - * @return array subTerms as array - */ + * Parses a single SubSpec into sunTerms. + * + * @param string $subSpec A single SubSpec + * + * @return array subTerms as array + */ private function matchSubTerms($subSpec) { if (preg_match('/(?SUBTERMS.'/', $subSpec, $_subTermMatches, PREG_SET_ORDER)) { if (empty($_subTermMatches[0]['operator'])) { - $_subTermMatches[0]['operator'] = "?"; + $_subTermMatches[0]['operator'] = '?'; } if (!$_subTermMatches[0]['rightsubterm']) { throw new InvalidMARCspecException( @@ -345,6 +348,7 @@ private function matchSubTerms($subSpec) $subSpec ); } + return array_filter($_subTermMatches[0], 'strlen'); } else { throw new InvalidMARCspecException( @@ -354,10 +358,10 @@ private function matchSubTerms($subSpec) ); } } - + /** - * Set regex variables (constant) - */ + * Set regex variables (constant). + */ private function setConstants() { $this->FIELDTAG = '^(?(?:[a-z0-9\.]{3,3}|[A-Z0-9\.]{3,3}|[0-9\.]{3,3}))?'; diff --git a/PositionOrRange.php b/PositionOrRange.php index 586b476..67caddf 100755 --- a/PositionOrRange.php +++ b/PositionOrRange.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee -* @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,27 +12,26 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** -* class for index or character position or range spec -*/ + * class for index or character position or range spec. + */ class PositionOrRange implements PositionOrRangeInterface { - /** * {@inheritdoc} */ public function setIndexStartEnd($start, $end = null) { - list($this->indexStart,$this->indexEnd) = $this->validateStartEnd($start, $end); + list($this->indexStart, $this->indexEnd) = $this->validateStartEnd($start, $end); } - + /** * {@inheritdoc} */ public function setIndexStartLength($start, $length) { - list($this->indexStart,$this->indexEnd) = $this->validateStartLength($start, $length); + list($this->indexStart, $this->indexEnd) = $this->validateStartLength($start, $length); } - + /** * {@inheritdoc} */ @@ -41,7 +39,7 @@ public function getIndexStart() { return (isset($this->indexStart)) ? $this->indexStart : 0; } - + /** * {@inheritdoc} */ @@ -53,23 +51,23 @@ public function getIndexEnd() return (isset($this->indexEnd)) ? $this->indexEnd : $this->indexStart; } } - + /** * {@inheritdoc} */ public function setCharStartEnd($start, $end = null) { - list($this->charStart,$this->charEnd) = $this->validateStartEnd($start, $end); + list($this->charStart, $this->charEnd) = $this->validateStartEnd($start, $end); } - + /** * {@inheritdoc} */ public function setCharStartLength($start, $length) { - list($this->charStart,$this->charEnd) = $this->validateStartLength($start, $length); + list($this->charStart, $this->charEnd) = $this->validateStartLength($start, $length); } - + /** * {@inheritdoc} */ @@ -77,7 +75,7 @@ public function getCharStart() { return (isset($this->charStart)) ? $this->charStart : null; } - + /** * {@inheritdoc} */ @@ -85,7 +83,7 @@ public function getCharEnd() { return (isset($this->charEnd)) ? $this->charEnd : $this->charStart; } - + /** * {@inheritdoc} */ @@ -93,7 +91,7 @@ public function getCharLength() { return $this->getLength(true); } - + /** * {@inheritdoc} */ @@ -101,76 +99,74 @@ public function getIndexLength() { return $this->getLength(false); } - + /** - * Calculate the length of charrange or index range - * - * @param bool $type True for charrange and false for indexrange - * - * @return int $length - */ + * Calculate the length of charrange or index range. + * + * @param bool $type True for charrange and false for indexrange + * + * @return int $length + */ private function getLength($type = true) { if ($type) { $start = $this->getCharStart(); - $end = $this->getCharEnd(); + $end = $this->getCharEnd(); } else { $start = $this->getIndexStart(); - $end = $this->getIndexEnd(); + $end = $this->getIndexEnd(); } - + if (is_null($start) && is_null($end)) { - return null; + return; } - + if (!is_null($start) && is_null($end)) { return 1; } - + if ($start === $end) { return 1; } - + if ('#' === $start && '#' !== $end) { return $end + 1; } - + if ('#' !== $start && '#' === $end) { - return null; + return; } $length = $end - $start + 1; - + if (1 > $length) { throw new InvalidMARCspecException( InvalidMARCspecException::PR. InvalidMARCspecException::NEGATIVE ); } + return $length; } /** - * - * Validate starting and ending position - * - * @internal - * - * @access private - * - * @param int|string $start The starting position - * @param int|string $end The ending position - * - * @return null|array $_startEnd index 0 => start, index 1 => end - * - * @throws \UnexpectedValueException - */ + * Validate starting and ending position. + * + * @internal + * + * @param int|string $start The starting position + * @param int|string $end The ending position + * + * @throws \UnexpectedValueException + * + * @return null|array $_startEnd index 0 => start, index 1 => end + */ private function validateStartEnd($start, $end) { - $_startEnd = array(); - + $_startEnd = []; + if (preg_match('/[0-9]/', $start)) { - $_startEnd[0] = (int)$start; + $_startEnd[0] = (int) $start; } elseif ('#' === $start) { $_startEnd[0] = '#'; } else { @@ -185,8 +181,8 @@ private function validateStartEnd($start, $end) if ('#' === $end) { $_startEnd[1] = '#'; } elseif (preg_match('/[0-9]/', $end)) { - $_startEnd[1] = (int)$end; - + $_startEnd[1] = (int) $end; + if ($_startEnd[1] < $_startEnd[0]) { throw new InvalidMARCspecException( InvalidMARCspecException::PR. @@ -204,30 +200,27 @@ private function validateStartEnd($start, $end) } else { $_startEnd[1] = null; } + return $_startEnd; } - + /** - * - * Validate starting position and length - * - * @internal - * - * @access private - * - * @param string $start The starting position - * @param string $length $length The length count - * - * @return array $_startEnd index 0 => start, index 1 => end - * - * @throws \UnexpectedValueException - */ + * Validate starting position and length. + * + * @internal + * + * @param string $start The starting position + * @param string $length $length The length count + * + * @throws \UnexpectedValueException + * + * @return array $_startEnd index 0 => start, index 1 => end + */ private function validateStartLength($start, $length) { - - $_startEnd = array(); + $_startEnd = []; if (preg_match('/[0-9]/', $start)) { - $_startEnd[0] = (int)$start; + $_startEnd[0] = (int) $start; } elseif ('#' === $start) { $_startEnd[0] = '#'; } else { @@ -236,26 +229,24 @@ private function validateStartLength($start, $length) $start ); } - - + if (preg_match('/^[1-9]\d*/', $length)) { // only positive int without 0 - $_startEnd[1] = (int)$length - 1; + $_startEnd[1] = (int) $length - 1; } else { throw new \UnexpectedValueException( 'Second argument must be positive int without 0.', $length ); } + return $_startEnd; } - + /** - * checks if argument is a string + * checks if argument is a string. * * @internal * - * @access private - * * @param string $arg The argument to check * * @throws \InvalidArgumentException if the argument is not a string @@ -263,8 +254,8 @@ private function validateStartLength($start, $length) protected function checkIfString($arg) { if (!is_string($arg)) { - throw new \InvalidArgumentException("Method only accepts string as argument. " - .gettype($arg)." given."); + throw new \InvalidArgumentException('Method only accepts string as argument. ' + .gettype($arg).' given.'); } } } // EOC diff --git a/PositionOrRangeInterface.php b/PositionOrRangeInterface.php index 5656c42..6676786 100755 --- a/PositionOrRangeInterface.php +++ b/PositionOrRangeInterface.php @@ -4,147 +4,111 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace CK\MARCspec; /** - * MARCspec position or range interface + * MARCspec position or range interface. */ interface PositionOrRangeInterface { - /** - * - * Set the field index starting and ending position - * - * @api - * - * @access public - * - * @param int|string $start The index starting position - * @param int|string|null $end The index ending position - */ + * Set the field index starting and ending position. + * + * @api + * + * @param int|string $start The index starting position + * @param int|string|null $end The index ending position + */ public function setIndexStartEnd($start, $end = null); /** - * - * Set the field index starting and ending position via length - * - * @api - * - * @access public - * - * @param int|string $start The index starting position - * @param int $length The length count - */ + * Set the field index starting and ending position via length. + * + * @api + * + * @param int|string $start The index starting position + * @param int $length The length count + */ public function setIndexStartLength($start, $length); /** - * - * Get the character starting position as integer or '#' - * - * @api - * - * @access public - * - * @return int|string $indexStart The field index starting position - */ + * Get the character starting position as integer or '#'. + * + * @api + * + * @return int|string $indexStart The field index starting position + */ public function getIndexStart(); /** - * - * Get the field index ending position as integer or '#' - * - * @api - * - * @access public - * - * @return int|string $indexEnd The field index ending position - */ + * Get the field index ending position as integer or '#'. + * + * @api + * + * @return int|string $indexEnd The field index ending position + */ public function getIndexEnd(); /** - * - * Set character starting and ending position - * - * @api - * - * @access public - * - * @param int|string $start The character starting position - * @param int|string|null $end The character ending position - * - */ + * Set character starting and ending position. + * + * @api + * + * @param int|string $start The character starting position + * @param int|string|null $end The character ending position + */ public function setCharStartEnd($start, $end = null); /** - * - * Set character starting and ending position via start and length - * - * @api - * - * @access public - * - * @param int|string $start The character starting position - * @param int $length The character length count - * - */ + * Set character starting and ending position via start and length. + * + * @api + * + * @param int|string $start The character starting position + * @param int $length The character length count + */ public function setCharStartLength($start, $length); /** - * - * Get the character starting position as integer or '#' - * - * @api - * - * @access public - * - * @return int|string $charStart The character starting position - */ + * Get the character starting position as integer or '#'. + * + * @api + * + * @return int|string $charStart The character starting position + */ public function getCharStart(); - /** - * - * Get the character ending position as integer or '#' - * - * @api - * - * @access public - * - * @return int|string $charEnd The character ending position - */ + * Get the character ending position as integer or '#'. + * + * @api + * + * @return int|string $charEnd The character ending position + */ public function getCharEnd(); /** - * - * Get length of character range - * - * @api - * - * @access public - * - * @return null|int $length The character length - * - * @throws \InvalidArgumentException if length is less than 1 - */ + * Get length of character range. + * + * @api + * + * @throws \InvalidArgumentException if length is less than 1 + * + * @return null|int $length The character length + */ public function getCharLength(); - + /** - * - * Get length of index range - * - * @api - * - * @access public - * - * @return null|int $length The index range length - * - * @throws \InvalidArgumentException if length is less than 1 - */ + * Get length of index range. + * + * @api + * + * @throws \InvalidArgumentException if length is less than 1 + * + * @return null|int $length The index range length + */ public function getIndexLength(); } // EOI diff --git a/SpecIterator.php b/SpecIterator.php index 94d8642..2735d76 100755 --- a/SpecIterator.php +++ b/SpecIterator.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee -* @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -12,7 +11,7 @@ class SpecIterator implements \Iterator { - private $var = array(); + private $var = []; public function __construct($array) { @@ -29,24 +28,28 @@ public function rewind() public function current() { $var = current($this->var); + return $var; } public function key() { $var = key($this->var); + return $var; } public function next() { $var = next($this->var); + return $var; } public function valid() { $var = $this->current() !== false; + return $var; } } diff --git a/SubSpec.php b/SubSpec.php index e9986ef..5e0dff1 100755 --- a/SubSpec.php +++ b/SubSpec.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,26 +12,25 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** -* A MARCspec subspec class -*/ + * A MARCspec subspec class. + */ class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate { - /** * @var string Operator */ private $operator; - + /** * @var MARCspecInterface|ComparisonStringInterface The left hand subterm */ private $leftSubTerm; - + /** * @var MARCspecInterface|ComparisonStringInterface The right hand subterm */ private $rightSubTerm; - + /** * {@inheritdoc} * @@ -41,7 +39,6 @@ class SubSpec implements SubSpecInterface, \JsonSerializable, \ArrayAccess, \Ite */ public function __construct($leftSubTerm, $operator, $rightSubTerm) { - if ($leftSubTerm instanceof MARCspecInterface || $leftSubTerm instanceof ComparisonStringInterface) { $this->leftSubTerm = $leftSubTerm; @@ -51,7 +48,7 @@ public function __construct($leftSubTerm, $operator, $rightSubTerm) CK\MARCspec\ComparisonStringInterface' ); } - + if ($rightSubTerm instanceof MARCspecInterface || $rightSubTerm instanceof ComparisonStringInterface) { $this->rightSubTerm = $rightSubTerm; @@ -62,18 +59,18 @@ public function __construct($leftSubTerm, $operator, $rightSubTerm) .gettype($rightSubTerm) ); } - + $this->setOperator($operator); } - + /** - * Set operator + * Set operator. * * @throws InvalidMARCspecException */ private function setOperator($operator) { - if (!in_array($operator, ["=", "!=", "~", "!~", "!", "?"], true)) { + if (!in_array($operator, ['=', '!=', '~', '!~', '!', '?'], true)) { throw new InvalidMARCspecException( InvalidMARCspecException::SS. InvalidMARCspecException::OPERATOR, @@ -81,9 +78,8 @@ private function setOperator($operator) ); } $this->operator = $operator; - } - + /** * {@inheritdoc} */ @@ -91,7 +87,7 @@ public function getOperator() { return $this->operator; } - + /** * {@inheritdoc} */ @@ -99,7 +95,7 @@ public function getLeftSubTerm() { return (isset($this->leftSubTerm)) ? $this->leftSubTerm : null; } - + /** * {@inheritdoc} */ @@ -107,27 +103,28 @@ public function getRightSubTerm() { return $this->rightSubTerm; } - + /** * {@inheritdoc} */ public function __toString() { switch ($this->operator) { - case "!": + case '!': $subSpecString = $this->operator."$this->rightSubTerm"; break; - - case "?": + + case '?': $subSpecString = "$this->rightSubTerm"; break; - + default: $subSpecString = "$this->leftSubTerm".$this->operator."$this->rightSubTerm"; } + return $subSpecString; } - + /** * {@inheritdoc} */ @@ -138,11 +135,12 @@ public function jsonSerialize() } $_subSpec['operator'] = $this->operator; $_subSpec['rightSubTerm'] = $this->rightSubTerm->jsonSerialize(); + return $_subSpec; } /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -156,14 +154,14 @@ public function offsetExists($offset) case 'rightSubTerm': return true; break; - + default: return false; } } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -175,22 +173,22 @@ public function offsetGet($offset) case 'operator': return $this->getOperator(); break; - + case 'leftSubTerm': return $this->getLeftSubTerm(); break; - + case 'rightSubTerm': return $this->getRightSubTerm(); break; - + default: throw new \UnexpectedValueException("Offset $offset does not exist."); } } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -200,9 +198,9 @@ public function offsetSet($offset, $value) { throw new \UnexpectedValueException("Offset $offset cannot be set."); } - + /** - * Access object like an associative array + * Access object like an associative array. * * @param string $offset */ @@ -210,14 +208,14 @@ public function offsetUnset($offset) { throw new \BadMethodCallException("Offset $offset can not be unset."); } - + public function getIterator() { return new SpecIterator( [ - "leftSubTerm" => $this->leftSubTerm, - "operator" => $this->operator, - "rightSubTerm" => $this->rightSubTerm + 'leftSubTerm' => $this->leftSubTerm, + 'operator' => $this->operator, + 'rightSubTerm' => $this->rightSubTerm, ] ); } diff --git a/SubSpecInterface.php b/SubSpecInterface.php index 25e0e79..81d7a99 100755 --- a/SubSpecInterface.php +++ b/SubSpecInterface.php @@ -4,60 +4,57 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace CK\MARCspec; /** - * MARCspec subspec interface + * MARCspec subspec interface. */ interface SubSpecInterface { - /** - * Constructor for SubSpec - * - * @param null|MARCspecInterface|ComparisonString $leftSubTerm - * @param null|string $operator - * @param MARCspecInterface|ComparisonString $rightSubTerm - */ + * Constructor for SubSpec. + * + * @param null|MARCspecInterface|ComparisonString $leftSubTerm + * @param null|string $operator + * @param MARCspecInterface|ComparisonString $rightSubTerm + */ public function __construct($leftSubTerm, $operator, $rightSubTerm); /** - * Get subspecs operator + * Get subspecs operator. * * @return string The operator */ public function getOperator(); - + /** - * Get subspecs operator + * Get subspecs operator. * * @return null|MARCspecInterface|ComparisonString The left hand subterm */ public function getLeftSubTerm(); - + /** - * Get subspecs operator + * Get subspecs operator. * * @return MARCspecInterface|ComparisonString The right hand subterm */ public function getRightSubTerm(); - + /** - * encodes SubSpec as string - * - * @api - * - * @return string - */ + * encodes SubSpec as string. + * + * @api + * + * @return string + */ public function __toString(); - + /** - * Serialize SubSpec as JSON + * Serialize SubSpec as JSON. * * @api * diff --git a/Subfield.php b/Subfield.php index 86bc46c..f9e343f 100755 --- a/Subfield.php +++ b/Subfield.php @@ -4,7 +4,6 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -13,62 +12,59 @@ use CK\MARCspec\Exception\InvalidMARCspecException; /** -* A MARCspec subfield class -*/ + * A MARCspec subfield class. + */ class Subfield extends PositionOrRange implements SubfieldInterface, \JsonSerializable, \ArrayAccess, \IteratorAggregate { - /** * @var string subfield tag */ private $tag; - + /** * @var int|string indexStart */ protected $indexStart; - + /** * @var int|string indexEnd */ protected $indexEnd; - + /** * @var int|string starting position */ protected $charStart; - + /** * @var int|string ending position */ protected $charEnd; - + /** * @var array subSpec */ private $subSpecs = []; /** - * - * {@inheritdoc} - * - * @throws \InvalidArgumentException - * @throws InvalidMARCspecException - * - */ + * {@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."); + throw new \InvalidArgumentException('Method only accepts string as argument. '. + gettype($subfieldspec).' given.'); } - + $argLength = strlen($subfieldspec); - + if (0 === $argLength) { throw new InvalidMARCspecException( InvalidMARCspecException::SF. @@ -76,12 +72,12 @@ public function __construct($subfieldspec = null) $subfieldspec ); } - + if (1 == $argLength) { - $subfieldspec = "$".$subfieldspec; + $subfieldspec = '$'.$subfieldspec; } - if (1<$argLength) { + if (1 < $argLength) { if ('-' == $subfieldspec[1]) { // assuming subfield range throw new InvalidMARCspecException( InvalidMARCspecException::SF. @@ -90,7 +86,7 @@ public function __construct($subfieldspec = null) ); } } - + if (preg_match('/\{.*\}$/', $subfieldspec)) { throw new InvalidMARCspecException( InvalidMARCspecException::SF. @@ -98,33 +94,32 @@ public function __construct($subfieldspec = null) $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, "#"); + $this->setIndexStartEnd(0, '#'); } - + if (array_key_exists('charpos', $subfield)) { $_chars = MARCspec::validatePos($subfield['charpos']); - + $this->setCharStartEnd($_chars[0], $_chars[1]); } } - /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function setTag($arg) { if (!preg_match('/^[\!-\?\[-\{\}-~]$/', $arg)) { @@ -138,17 +133,16 @@ public function setTag($arg) } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getTag() { return (isset($this->tag)) ? $this->tag : null; } - + /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function addSubSpec($SubSpec) { if ($SubSpec instanceof SubSpecInterface) { @@ -167,40 +161,39 @@ public function addSubSpec($SubSpec) .gettype($subSpec).'".'); } } - + /** - * - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getSubSpecs() { return (0 < count($this->subSpecs)) ? $this->subSpecs : null; } - + /** * {@inheritdoc} */ public function jsonSerialize() { $_subfieldSpec['tag'] = $this->getTag(); - - $_subfieldSpec['indexStart'] = $this->getIndexStart(); - + + $_subfieldSpec['indexStart'] = $this->getIndexStart(); + $_subfieldSpec['indexEnd'] = $this->getIndexEnd(); - + if (($indexLength = $this->getIndexLength()) !== null) { $_subfieldSpec['indexLength'] = $indexLength; } - + if (($charStart = $this->getCharStart()) !== null) { $_subfieldSpec['charStart'] = $charStart; $_subfieldSpec['charEnd'] = $this->getCharEnd(); } - + if (($charLength = $this->getCharLength()) !== null) { $_subfieldSpec['charLength'] = $charLength; } - + if (($subSpecs = $this->getSubSpecs()) !== null) { $_subfieldSpec['subSpecs'] = []; foreach ($subSpecs as $key => $subSpec) { @@ -208,15 +201,15 @@ public function jsonSerialize() foreach ($subSpec as $altSubSpec) { $_subfieldSpec['subSpecs'][$key][] = $altSubSpec->jsonSerialize(); } - } else { $_subfieldSpec['subSpecs'][$key] = $subSpec->jsonSerialize(); } } } + return $_subfieldSpec; } - + /** * {@inheritdoc} */ @@ -226,24 +219,25 @@ public function getBaseSpec() $indexStart = $this->getIndexStart(); $indexEnd = $this->getIndexEnd(); - $subfieldSpec .= "[".$indexStart; - + $subfieldSpec .= '['.$indexStart; + if ($indexStart !== $indexEnd) { - $subfieldSpec .= "-".$indexEnd; + $subfieldSpec .= '-'.$indexEnd; } - $subfieldSpec .= "]"; - + $subfieldSpec .= ']'; + if (($charStart = $this->getCharStart()) !== null) { $charEnd = $this->getCharEnd(); - if ($charStart === 0 && $charEnd === "#") { - // use abbreviation + if ($charStart === 0 && $charEnd === '#') { + // use abbreviation } else { - $subfieldSpec .= "/".$charStart; + $subfieldSpec .= '/'.$charStart; if ($charEnd !== $charStart) { - $subfieldSpec .= "-".$charEnd; + $subfieldSpec .= '-'.$charEnd; } } } + return $subfieldSpec; } @@ -253,7 +247,7 @@ public function getBaseSpec() public function __toString() { $subfieldSpec = $this->getBaseSpec(); - + if (($subSpecs = $this->getSubSpecs()) !== null) { foreach ($subSpecs as $subSpec) { if (is_array($subSpec)) { @@ -266,12 +260,12 @@ public function __toString() } } } + return $subfieldSpec; } - - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -308,9 +302,9 @@ public function offsetExists($offset) return false; } } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -344,12 +338,12 @@ public function offsetGet($offset) return $this->getSubSpecs(); break; default: - return null; + return; } } - + /** - * Access object like an associative array + * Access object like an associative array. * * @api * @@ -361,11 +355,11 @@ public function offsetSet($offset, $value) case 'tag': $this->setTag($value); break; - + case 'indexStart': $this->setIndexStartEnd($value); break; - + case 'indexEnd': if (!isset($this['indexStart'])) { $this->setIndexStartEnd($value, $value); @@ -373,11 +367,11 @@ public function offsetSet($offset, $value) $this->setIndexStartEnd($this['indexStart'], $value); } break; - + case 'indexLength': - throw new \UnexpectedValueException("indexLength is always calculated."); + throw new \UnexpectedValueException('indexLength is always calculated.'); break; - + case 'charStart': $this->setCharStartEnd($value); break; @@ -391,7 +385,7 @@ public function offsetSet($offset, $value) break; case 'charLength': - throw new \UnexpectedValueException("CharLength is always calculated."); + throw new \UnexpectedValueException('CharLength is always calculated.'); break; case 'subSpecs': @@ -402,20 +396,21 @@ public function offsetSet($offset, $value) throw new \UnexpectedValueException("Offset $offset cannot be set."); } } - + /** - * Access object like an associative array + * Access object like an associative array. */ public function offsetUnset($offset) { throw new \BadMethodCallException("Offset $offset can not be unset."); } - + public function getIterator() { foreach ($this as $key => $value) { $_[$key] = $value; } + return new SpecIterator($_); } } // EOC diff --git a/SubfieldInterface.php b/SubfieldInterface.php index 30b76df..cb50c00 100755 --- a/SubfieldInterface.php +++ b/SubfieldInterface.php @@ -4,210 +4,168 @@ * from within a MARC record. * * @author Carsten Klee - * @package CK\MARCspec * @copyright For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace CK\MARCspec; /** - * MARCspec subfield interface + * MARCspec subfield interface. */ interface SubfieldInterface { - /** - * constructor for MARCspec field + * constructor for MARCspec field. * * @api * - * @access public - * * @param string $subfield The subfield spec */ public function __construct($subfield); - - /** - * - * Sets the subfield tag - * - * @api - * - * @access public - * - */ + + /** + * Sets the subfield tag. + * + * @api + */ public function setTag($arg); /** - * - * Get the subfield tag - * - * @api - * - * @access public - * - * @return string The subfield tag - */ + * Get the subfield tag. + * + * @api + * + * @return string The subfield tag + */ public function getTag(); /** - * - * Set the field index starting and ending position - * - * @api + * Set the field index starting and ending position. * - * @access public + * @api * - * @param int|string $start The index starting position - * @param int|string|null $end The index ending position + * @param int|string $start The index starting position + * @param int|string|null $end The index ending position */ public function setIndexStartEnd($start, $end = null); - + /** - * - * Set the field index starting and ending position via length - * + * Set the field index starting and ending position via length. + * * @api * - * @access public - * - * @param int|string $start The index starting position - * @param int $length The length count + * @param int|string $start The index starting position + * @param int $length The length count */ public function setIndexStartLength($start, $length); - + /** - * - * Get the character starting position - * - * @api + * Get the character starting position. * - * @access public + * @api * * @return null|int|string $indexStart The field index starting position */ public function getIndexStart(); - + /** - * - * Get the field index ending position - * - * @api + * Get the field index ending position. * - * @access public + * @api * * @return null|int $indexEnd The field index ending position */ public function getIndexEnd(); - + /** - * - * Set character starting and ending position - * - * @api + * Set character starting and ending position. * - * @access public + * @api * - * @param int|string $start The character starting position - * @param int|string|null $end The character ending position - * + * @param int|string $start The character starting position + * @param int|string|null $end The character ending position */ public function setCharStartEnd($start, $end = null); - + /** - * - * Set character starting and ending position via start and length + * Set character starting and ending position via start and length. * * @api * - * @access public - * - * @param int|string $start The character starting position - * @param int $length The character length count - * + * @param int|string $start The character starting position + * @param int $length The character length count */ public function setCharStartLength($start, $length); - + /** - * - * Get the character starting position - * - * @api + * Get the character starting position. * - * @access public + * @api * * @return null|int $charStart The character starting position */ public function getCharStart(); - - + /** - * - * Get the character ending position - * - * @api + * Get the character ending position. * - * @access public + * @api * * @return null|int $charEnd The character ending position */ public function getCharEnd(); - - /** - * - * Get length of character range - * - * @api - * - * @access public - * - * @return null|int $length The character length - * - * @throws \InvalidArgumentException if length is less than 1 - */ + + /** + * Get length of character range. + * + * @api + * + * @throws \InvalidArgumentException if length is less than 1 + * + * @return null|int $length The character length + */ public function getCharLength(); - - /** - * get array of subspecs - * - * @api - * - * @return null|array - */ + + /** + * get array of subspecs. + * + * @api + * + * @return null|array + */ public function getSubSpecs(); - - /** - * add a subspec to the array of subspecs - * - * @api - * - * @param SubSpecInterface|array[SubSpecInterface] - * - * @return null|array - */ + + /** + * add a subspec to the array of subspecs. + * + * @api + * + * @param SubSpecInterface|array[SubSpecInterface] + * + * @return null|array + */ public function addSubSpec($SubSpec); - + /** - * Get the basic spec without subspecs + * Get the basic spec without subspecs. * * @api * * @return string */ public function getBaseSpec(); - + /** - * encodes Field as string + * encodes Field as string. * * @api * * @return string */ public function __toString(); - + /** - * Serialize Field as JSON + * Serialize Field as JSON. * * @api * diff --git a/Test/ComparisonStringTest.php b/Test/ComparisonStringTest.php index 0e7ea32..a4309cc 100755 --- a/Test/ComparisonStringTest.php +++ b/Test/ComparisonStringTest.php @@ -5,42 +5,44 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace CK\MARCspec\Test; use CK\MARCspec\ComparisonString; -use CK\MARCspec\Exception\InvalidMARCspecException; class ComparisonStringTest extends \PHPUnit_Framework_TestCase { protected $validTests; protected $invalidTests; - + protected function setUp() { - if(0 < count($this->validTests)) return; - $validTestsJson = file_get_contents(__DIR__. '/../' ."vendor/ck/marcspec-test-suite/valid/validComparisonString.json"); - $invalidTestsJson = file_get_contents(__DIR__. '/../' ."vendor/ck/marcspec-test-suite/invalid/invalidComparisonString.json"); + if (0 < count($this->validTests)) { + return; + } + $validTestsJson = file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validComparisonString.json'); + $invalidTestsJson = file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidComparisonString.json'); $this->validTests = json_decode($validTestsJson); $this->invalidTests = json_decode($invalidTestsJson); } - + public function compare($arg) { return new ComparisonString($arg); } - + /**** * invalid data types ***/ - + /** * @expectedException InvalidArgumentException */ public function testInvalidArgument1Decode() { - $this->compare(array('a')); - } - + $this->compare(['a']); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -50,65 +52,59 @@ public function testInvalidArgument2Decode() } /** - * assert same string + * assert same string. */ public function testValidComparisonString1() { - $compare = $this->compare("."); + $compare = $this->compare('.'); $this->assertSame('.', $compare->getRaw()); $this->assertSame('.', $compare->getComparable()); $this->assertSame('\.', $compare->__toString()); } - + /** - * assert same string + * assert same string. */ public function testValidComparisonString2() { $escaped_string = '\\.'; $compare = $this->compare($escaped_string); $this->assertSame('\\.', $compare->getRaw()); - + $escaped_string = 'this\sis\sa\sTest\s\\\{\}\!\=\~\?'; $compare = $this->compare($escaped_string); $this->assertSame('this\sis\sa\sTest\s\\\{\}\!\=\~\?', $compare->getRaw()); - + $unescaped_string = 'this is a Test \{}!=~?'; $escaped_string = ComparisonString::escape($unescaped_string); $compare = $this->compare($escaped_string); $this->assertSame('this\sis\sa\sTest\s\\\{\}\!\=\~\?', $compare->getRaw()); $this->assertSame('this is a Test \{}!=~?', $compare->getComparable()); - } - + public function testInvalidFromTestSuite() { - foreach($this->invalidTests->{'tests'} as $test) - { - try - { + foreach ($this->invalidTests->{'tests'} as $test) { + try { new ComparisonString($test->{'data'}); - } - catch(\Exception $e) - { - continue; + } catch (\Exception $e) { + continue; } $this->fail('An expected exception has not been raised for '.$test->{'data'}); } } - + public function testValidFromTestSuite() { - foreach($this->validTests->{'tests'} as $test) - { + foreach ($this->validTests->{'tests'} as $test) { new ComparisonString($test->{'data'}); - $this->assertSame(1,preg_match('/'.$this->validTests->{'schema'}->{'pattern'}.'/',$test->{'data'})); + $this->assertSame(1, preg_match('/'.$this->validTests->{'schema'}->{'pattern'}.'/', $test->{'data'})); } } - + /** - * @expectedException BadMethodCallException - */ + * @expectedException BadMethodCallException + */ public function testOffsetUnset() { $escaped_string = '\\.'; diff --git a/Test/FieldTest.php b/Test/FieldTest.php index 8178334..a219e2a 100755 --- a/Test/FieldTest.php +++ b/Test/FieldTest.php @@ -5,102 +5,110 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace CK\MARCspec\Test; -use CK\MARCspec\MARCspec; use CK\MARCspec\Field; +use CK\MARCspec\MARCspec; use CK\MARCspec\SubSpec; -use CK\MARCspec\Exception\InvalidMARCspecException; class FieldTest extends \PHPUnit_Framework_TestCase { - protected $validTests = []; protected $invalidTests = []; - + protected function setUp() { - if(0 < count($this->validTests)) return; - $this->validTests[] = json_decode(file_get_contents(__DIR__. '/../' ."vendor/ck/marcspec-test-suite/valid/validFieldTag.json")); - $this->invalidTests[] = json_decode(file_get_contents(__DIR__. '/../' ."vendor/ck/marcspec-test-suite/invalid/invalidFieldTag.json")); + if (0 < count($this->validTests)) { + return; + } + $this->validTests[] = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validFieldTag.json')); + $this->invalidTests[] = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidFieldTag.json')); } - + public function fieldspec($arg) { return new Field($arg); } - + /** * @expectedException InvalidArgumentException */ public function testInvalidArgument2Decode() { - $this->fieldspec(array('245')); + $this->fieldspec(['245']); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec13() { - $this->fieldspec('007/'); + $this->fieldspec('007/'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec14() { - $this->fieldspec('007/1-2-'); + $this->fieldspec('007/1-2-'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec15() { - $this->fieldspec('24#'); + $this->fieldspec('24#'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec16() { - $this->fieldspec('007/-2'); + $this->fieldspec('007/-2'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec17() { - $this->fieldspec('245[-2]'); + $this->fieldspec('245[-2]'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec18() { - $this->fieldspec('245[1-2-]'); + $this->fieldspec('245[1-2-]'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec19() { - $this->fieldspec('245[1-2'); + $this->fieldspec('245[1-2'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec110() { - $this->fieldspec('007/1-X'); - } - + $this->fieldspec('007/1-X'); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec111() { - $this->fieldspec('007/#-'); + $this->fieldspec('007/#-'); } /** @@ -108,9 +116,9 @@ public function testInvalidFieldSpec111() */ public function testInvalidFieldSpec112() { - $this->fieldspec('245[0-2a]'); + $this->fieldspec('245[0-2a]'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -118,7 +126,7 @@ public function testInvalidFieldSpec113() { $this->fieldspec('300[1-]'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException * @covers CK\MARCspec\Field::validateTag @@ -129,12 +137,10 @@ public function testInvalidFieldSpec114() $fieldSpec = $this->fieldspec(null); $fieldSpec['tag'] = 'aA0'; } - - + /**** * invalid indicators ***/ - /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException @@ -142,45 +148,45 @@ public function testInvalidFieldSpec114() */ public function testInvalidFieldSpec30() { - $this->fieldspec('245_1+'); + $this->fieldspec('245_1+'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec31() { - $this->fieldspec('245_123'); - } + $this->fieldspec('245_123'); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec32() { - $this->fieldspec('245_$'); - } + $this->fieldspec('245_$'); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec33() { - $this->fieldspec('245_1|'); - } + $this->fieldspec('245_1|'); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec34() { - $this->fieldspec('245_10_'); - } + $this->fieldspec('245_10_'); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException - - public function testInvalidFieldSpec35() - { - $this->fieldspec('245___'); - }*/ - + }*/ + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -188,7 +194,7 @@ public function testInvalidArgument310Decode() { $this->fieldspec('245{$c=$d}$a'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -198,112 +204,107 @@ public function testInvalidArgument311Decode() } /** - * assert same field tag + * assert same field tag. */ public function testValidFieldSpec1() { - $fieldSpec = $this->fieldspec('LDR'); - $this->assertSame('LDR', $fieldSpec->getTag()); - - $fieldSpec = $this->fieldspec('245'); - $this->assertSame('245', $fieldSpec->getTag()); - - $fieldSpec = $this->fieldspec('...'); - $this->assertSame('...', $fieldSpec->getTag()); - - $fieldSpec = $this->fieldspec('245[1]'); - $this->assertSame('245', $fieldSpec->getTag()); - $this->assertSame(1, $fieldSpec->getIndexStart()); - - $fieldSpec = $this->fieldspec('245[1-3]'); - $this->assertSame(1, $fieldSpec->getIndexStart()); - $this->assertSame(3, $fieldSpec->getIndexEnd()); - - $fieldSpec = $this->fieldspec('245[1-#]'); - $this->assertSame(1, $fieldSpec->getIndexStart()); - $this->assertSame('#', $fieldSpec->getIndexEnd()); - - $fieldSpec = $this->fieldspec('245[#-3]'); - $this->assertSame('#', $fieldSpec->getIndexStart()); - $this->assertSame(3, $fieldSpec->getIndexEnd()); - } - - - + $fieldSpec = $this->fieldspec('LDR'); + $this->assertSame('LDR', $fieldSpec->getTag()); + + $fieldSpec = $this->fieldspec('245'); + $this->assertSame('245', $fieldSpec->getTag()); + + $fieldSpec = $this->fieldspec('...'); + $this->assertSame('...', $fieldSpec->getTag()); + + $fieldSpec = $this->fieldspec('245[1]'); + $this->assertSame('245', $fieldSpec->getTag()); + $this->assertSame(1, $fieldSpec->getIndexStart()); + + $fieldSpec = $this->fieldspec('245[1-3]'); + $this->assertSame(1, $fieldSpec->getIndexStart()); + $this->assertSame(3, $fieldSpec->getIndexEnd()); + + $fieldSpec = $this->fieldspec('245[1-#]'); + $this->assertSame(1, $fieldSpec->getIndexStart()); + $this->assertSame('#', $fieldSpec->getIndexEnd()); + + $fieldSpec = $this->fieldspec('245[#-3]'); + $this->assertSame('#', $fieldSpec->getIndexStart()); + $this->assertSame(3, $fieldSpec->getIndexEnd()); + } + /** - * test character position and range + * test character position and range. */ public function testValidFieldSpec2() { - $fieldSpec = $this->fieldspec('LDR/0-3'); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getCharStart()); - $this->assertSame(3, $fieldSpec->getCharEnd()); - $this->assertSame(4, $fieldSpec->getCharLength()); - - $fieldSpec = $this->fieldspec('LDR/0-#'); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getCharStart()); - $this->assertSame('#', $fieldSpec->getCharEnd()); - $this->assertSame(null, $fieldSpec->getCharLength()); - - $fieldSpec = $this->fieldspec('LDR/#-4'); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame('#', $fieldSpec->getCharStart()); - $this->assertSame(4, $fieldSpec->getCharEnd()); - $this->assertSame(5, $fieldSpec->getCharLength()); - - $fieldSpec = $this->fieldspec('LDR/#-0'); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame('#', $fieldSpec->getCharStart()); - $this->assertSame(0, $fieldSpec->getCharEnd()); - $this->assertSame(1, $fieldSpec->getCharLength()); - - } - - - /** - * test character range + $fieldSpec = $this->fieldspec('LDR/0-3'); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getCharStart()); + $this->assertSame(3, $fieldSpec->getCharEnd()); + $this->assertSame(4, $fieldSpec->getCharLength()); + + $fieldSpec = $this->fieldspec('LDR/0-#'); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getCharStart()); + $this->assertSame('#', $fieldSpec->getCharEnd()); + $this->assertSame(null, $fieldSpec->getCharLength()); + + $fieldSpec = $this->fieldspec('LDR/#-4'); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame('#', $fieldSpec->getCharStart()); + $this->assertSame(4, $fieldSpec->getCharEnd()); + $this->assertSame(5, $fieldSpec->getCharLength()); + + $fieldSpec = $this->fieldspec('LDR/#-0'); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame('#', $fieldSpec->getCharStart()); + $this->assertSame(0, $fieldSpec->getCharEnd()); + $this->assertSame(1, $fieldSpec->getCharLength()); + } + + /** + * test character range. */ public function testValidFieldSpec22() { - $fieldSpec = $this->fieldspec('245/#'); - $this->assertSame(1, $fieldSpec->getCharLength()); - $fieldSpec = $this->fieldspec('245/#-#'); - $this->assertSame(1, $fieldSpec->getCharLength()); - $fieldSpec = $this->fieldspec('245/#-0'); - $this->assertSame(1, $fieldSpec->getCharLength()); - $fieldSpec = $this->fieldspec('245/#-1'); - $this->assertSame(2, $fieldSpec->getCharLength()); - $fieldSpec = $this->fieldspec('245/0-#'); - $this->assertSame(0, $fieldSpec->getCharStart()); - $this->assertSame("#", $fieldSpec->getCharEnd()); - $this->assertSame(null, $fieldSpec->getCharLength()); - } - - /** - * test indicators + $fieldSpec = $this->fieldspec('245/#'); + $this->assertSame(1, $fieldSpec->getCharLength()); + $fieldSpec = $this->fieldspec('245/#-#'); + $this->assertSame(1, $fieldSpec->getCharLength()); + $fieldSpec = $this->fieldspec('245/#-0'); + $this->assertSame(1, $fieldSpec->getCharLength()); + $fieldSpec = $this->fieldspec('245/#-1'); + $this->assertSame(2, $fieldSpec->getCharLength()); + $fieldSpec = $this->fieldspec('245/0-#'); + $this->assertSame(0, $fieldSpec->getCharStart()); + $this->assertSame('#', $fieldSpec->getCharEnd()); + $this->assertSame(null, $fieldSpec->getCharLength()); + } + + /** + * test indicators. */ public function testValidFieldSpec23() { - $fieldSpec = $this->fieldspec('245_0'); - $this->assertSame('0', $fieldSpec->getindicator1()); - $fieldSpec = $this->fieldspec('245__0'); - $this->assertSame('0', $fieldSpec->getindicator2()); - $fieldSpec = $this->fieldspec('245_0_'); - $this->assertSame('0', $fieldSpec->getindicator1()); - $fieldSpec = $this->fieldspec('245[1]_01'); - $this->assertSame('0', $fieldSpec->getindicator1()); - $this->assertSame('1', $fieldSpec->getindicator2()); - + $fieldSpec = $this->fieldspec('245_0'); + $this->assertSame('0', $fieldSpec->getindicator1()); + $fieldSpec = $this->fieldspec('245__0'); + $this->assertSame('0', $fieldSpec->getindicator2()); + $fieldSpec = $this->fieldspec('245_0_'); + $this->assertSame('0', $fieldSpec->getindicator1()); + $fieldSpec = $this->fieldspec('245[1]_01'); + $this->assertSame('0', $fieldSpec->getindicator1()); + $this->assertSame('1', $fieldSpec->getindicator2()); } - + /** * @covers CK\MARCspec\Field::offsetSet * @covers CK\MARCspec\Field::offsetExists * @covers CK\MARCspec\Field::addSubSpec */ - public function testValidFieldSpec24() + public function testValidFieldSpec24() { $fieldSpec = $this->fieldspec(null); $fieldSpec['tag'] = '...'; @@ -311,7 +312,7 @@ public function testValidFieldSpec24() $fieldSpec['indexEnd'] = '1'; $fieldSpec['indicator1'] = '0'; $fieldSpec['indicator2'] = '1'; - $Subspec = new SubSpec(new MARCspec('245$b'),'!=',new MARCspec('245$c')); + $Subspec = new SubSpec(new MARCspec('245$b'), '!=', new MARCspec('245$c')); $fieldSpec['subSpecs'] = $Subspec; $fieldSpec->addSubSpec($Subspec); $this->assertTrue($fieldSpec->offsetExists('tag')); @@ -321,103 +322,98 @@ public function testValidFieldSpec24() $this->assertTrue($fieldSpec->offsetExists('indicator2')); $this->assertTrue($fieldSpec->offsetExists('subSpecs')); } - - - - /** - * test character position and range + * test character position and range. */ public function testSetAndGetChar() { - $fieldSpec = $this->fieldspec('LDR'); - $fieldSpec->setCharStartEnd(0,3); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getCharStart()); - $this->assertSame(3, $fieldSpec->getCharEnd()); - $this->assertSame(4, $fieldSpec->getCharLength()); - - $fieldSpec = $this->fieldspec('LDR'); - $fieldSpec->setCharStartEnd("#",3); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame("#", $fieldSpec->getCharStart()); - $this->assertSame(3, $fieldSpec->getCharEnd()); - $this->assertSame(4, $fieldSpec->getCharLength()); - - $fieldSpec = $this->fieldspec('LDR'); - $fieldSpec->setCharStartEnd(0,4); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getCharStart()); - $this->assertSame(4, $fieldSpec->getCharEnd()); - $this->assertSame(5, $fieldSpec->getCharLength()); - - $fieldSpec = $this->fieldspec('LDR'); - $fieldSpec->setCharStartLength("#",4); - $this->assertSame('LDR', $fieldSpec->getTag()); - $this->assertSame("#", $fieldSpec->getCharStart()); - $this->assertSame(3, $fieldSpec->getCharEnd()); - $this->assertSame(4, $fieldSpec->getCharLength()); - } - + $fieldSpec = $this->fieldspec('LDR'); + $fieldSpec->setCharStartEnd(0, 3); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getCharStart()); + $this->assertSame(3, $fieldSpec->getCharEnd()); + $this->assertSame(4, $fieldSpec->getCharLength()); + + $fieldSpec = $this->fieldspec('LDR'); + $fieldSpec->setCharStartEnd('#', 3); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame('#', $fieldSpec->getCharStart()); + $this->assertSame(3, $fieldSpec->getCharEnd()); + $this->assertSame(4, $fieldSpec->getCharLength()); + + $fieldSpec = $this->fieldspec('LDR'); + $fieldSpec->setCharStartEnd(0, 4); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getCharStart()); + $this->assertSame(4, $fieldSpec->getCharEnd()); + $this->assertSame(5, $fieldSpec->getCharLength()); + + $fieldSpec = $this->fieldspec('LDR'); + $fieldSpec->setCharStartLength('#', 4); + $this->assertSame('LDR', $fieldSpec->getTag()); + $this->assertSame('#', $fieldSpec->getCharStart()); + $this->assertSame(3, $fieldSpec->getCharEnd()); + $this->assertSame(4, $fieldSpec->getCharLength()); + } + /** - * test index position and range + * test index position and range. */ public function testSetAndGetIndex() { - $fieldSpec = $this->fieldspec('300'); - $fieldSpec->setIndexStartEnd(0,3); - $this->assertSame('300', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getIndexStart()); - $this->assertSame(3, $fieldSpec->getIndexEnd()); - $this->assertSame(4, $fieldSpec->getIndexLength()); - - $fieldSpec = $this->fieldspec('300'); - $fieldSpec->setIndexStartEnd("#",3); - $this->assertSame('300', $fieldSpec->getTag()); - $this->assertSame("#", $fieldSpec->getIndexStart()); - $this->assertSame(3, $fieldSpec->getIndexEnd()); - $this->assertSame(4, $fieldSpec->getIndexLength()); - - $fieldSpec = $this->fieldspec('300'); - $fieldSpec->setIndexStartEnd(0,4); - $this->assertSame('300', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getIndexStart()); - $this->assertSame(4, $fieldSpec->getIndexEnd()); - $this->assertSame(5, $fieldSpec->getIndexLength()); - - $fieldSpec = $this->fieldspec('300'); - $fieldSpec->setIndexStartLength("#",4); - $this->assertSame('300', $fieldSpec->getTag()); - $this->assertSame("#", $fieldSpec->getIndexStart()); - $this->assertSame(3, $fieldSpec->getIndexEnd()); - $this->assertSame(4, $fieldSpec->getIndexLength()); - - $fieldSpec = $this->fieldspec('300'); - $fieldSpec->setIndexStartLength(0,6); - $this->assertSame('300', $fieldSpec->getTag()); - $this->assertSame(0, $fieldSpec->getIndexStart()); - $this->assertSame(5, $fieldSpec->getIndexEnd()); - $this->assertSame(6, $fieldSpec->getIndexLength()); - } - + $fieldSpec = $this->fieldspec('300'); + $fieldSpec->setIndexStartEnd(0, 3); + $this->assertSame('300', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getIndexStart()); + $this->assertSame(3, $fieldSpec->getIndexEnd()); + $this->assertSame(4, $fieldSpec->getIndexLength()); + + $fieldSpec = $this->fieldspec('300'); + $fieldSpec->setIndexStartEnd('#', 3); + $this->assertSame('300', $fieldSpec->getTag()); + $this->assertSame('#', $fieldSpec->getIndexStart()); + $this->assertSame(3, $fieldSpec->getIndexEnd()); + $this->assertSame(4, $fieldSpec->getIndexLength()); + + $fieldSpec = $this->fieldspec('300'); + $fieldSpec->setIndexStartEnd(0, 4); + $this->assertSame('300', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getIndexStart()); + $this->assertSame(4, $fieldSpec->getIndexEnd()); + $this->assertSame(5, $fieldSpec->getIndexLength()); + + $fieldSpec = $this->fieldspec('300'); + $fieldSpec->setIndexStartLength('#', 4); + $this->assertSame('300', $fieldSpec->getTag()); + $this->assertSame('#', $fieldSpec->getIndexStart()); + $this->assertSame(3, $fieldSpec->getIndexEnd()); + $this->assertSame(4, $fieldSpec->getIndexLength()); + + $fieldSpec = $this->fieldspec('300'); + $fieldSpec->setIndexStartLength(0, 6); + $this->assertSame('300', $fieldSpec->getTag()); + $this->assertSame(0, $fieldSpec->getIndexStart()); + $this->assertSame(5, $fieldSpec->getIndexEnd()); + $this->assertSame(6, $fieldSpec->getIndexLength()); + } + /** - * test encoding + * test encoding. */ public function testEncode() { - $fieldSpec = $this->fieldspec('245'); $this->assertSame('245[0-#]', "$fieldSpec"); - + $fieldSpec = $this->fieldspec('245_1'); - $this->assertSame('245[0-#]_1_',"$fieldSpec"); - + $this->assertSame('245[0-#]_1_', "$fieldSpec"); + $fieldSpec = $this->fieldspec('245__0'); $this->assertSame('245[0-#]__0', "$fieldSpec"); - + $fieldSpec = $this->fieldspec('245_1_'); - $this->assertSame('245[0-#]_1_',"$fieldSpec"); + $this->assertSame('245[0-#]_1_', "$fieldSpec"); $fieldSpec = $this->fieldspec('007/1'); $this->assertSame('007[0-#]/1', "$fieldSpec"); @@ -426,48 +422,41 @@ public function testEncode() $fieldSpec = $this->fieldspec('007/1-3'); $this->assertSame('007[0-#]/1-3', "$fieldSpec"); $this->assertSame(3, $fieldSpec->getCharLength()); - + $fieldSpec = $this->fieldspec('300[1]'); $this->assertSame('300[1]', "$fieldSpec"); - + $fieldSpec = $this->fieldspec('300[1-3]'); - $this->assertSame('300[1-3]',"$fieldSpec"); + $this->assertSame('300[1-3]', "$fieldSpec"); } - + /** * @expectedException BadMethodCallException */ public function testOffsetUnset() { - $fieldSpec = $this->fieldspec('245'); + $fieldSpec = $this->fieldspec('245'); unset($fieldSpec['tag']); } - + public function testInvalidFromTestSuite() { - foreach($this->invalidTests as $invalid) - { - foreach($invalid->{'tests'} as $test) - { - try - { + foreach ($this->invalidTests as $invalid) { + foreach ($invalid->{'tests'} as $test) { + try { new Field($test->{'data'}); - } - catch(\Exception $e) - { + } catch (\Exception $e) { continue; } $this->fail('An expected exception has not been raised for '.$test->{'data'}); } } } - + public function testValidFromTestSuite() { - foreach($this->validTests as $valid) - { - foreach($valid->{'tests'} as $test) - { + foreach ($this->validTests as $valid) { + foreach ($valid->{'tests'} as $test) { new Field($test->{'data'}); } } diff --git a/Test/MarcSpecTest.php b/Test/MarcSpecTest.php index 3679cce..04a222e 100755 --- a/Test/MarcSpecTest.php +++ b/Test/MarcSpecTest.php @@ -5,116 +5,114 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace CK\MARCspec\Test; -use CK\MARCspec\MARCspec; use CK\MARCspec\Field; -use CK\MARCspec\Exception\InvalidMARCspecException; +use CK\MARCspec\MARCspec; + #use CK\MARCspec\Subfield; #use CK\MARCspec\SubSpec; -class MARCspecTest extends \PHPUnit_Framework_TestCase +class MarcSpecTest extends \PHPUnit_Framework_TestCase { protected $validTests = []; protected $invalidTests = []; - + protected function setUp() { - if(0 < count($this->validTests)) return; + if (0 < count($this->validTests)) { + return; + } $valid = []; $invalid = []; - $a = ['valid','invalid']; - array_walk($a, - function($v,$k) use (&$valid,&$invalid) - { - foreach (glob(__DIR__. '/../' .'vendor/ck/marcspec-test-suite/'.$v.'/wildCombination_*.json') as $filename) - { - if('valid' == $v) - { + $a = ['valid', 'invalid']; + array_walk($a, + function ($v, $k) use (&$valid, &$invalid) { + foreach (glob(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/'.$v.'/wildCombination_*.json') as $filename) { + if ('valid' == $v) { $valid[] = json_decode(file_get_contents($filename)); - } - else - { + } else { $invalid[] = json_decode(file_get_contents($filename)); } } } ); - $this->validTests = $valid; - $this->invalidTests = $invalid; + $this->validTests = $valid; + $this->invalidTests = $invalid; } - + public function marcspec($arg) { return new MARCspec($arg); } - + /**** * invalid data types ***/ - + /** * @expectedException RuntimeException - */ + */ public function testInvalidArgument01Decode() { - $this->marcspec('24'); + $this->marcspec('24'); } - + /** * @expectedException InvalidArgumentException - */ + */ public function testInvalidArgument1Decode() { - $this->marcspec((int)'245$a'); + $this->marcspec((int) '245$a'); } + /** * @expectedException InvalidArgumentException - */ + */ public function testInvalidArgument2Decode() { - $this->marcspec(array('245$a')); + $this->marcspec(['245$a']); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException - */ + */ public function testInvalidArgument3Decode() { - $this->marcspec('245/#$a'); + $this->marcspec('245/#$a'); } - /** - * assert same subfields - */ + * assert same subfields. + */ public function testValidMarcSpec1() { - $marcSpec = $this->marcspec('245$a-c'); $this->assertSame(3, count($marcSpec['subfields'])); } + /** - * assert same subfields - */ + * assert same subfields. + */ public function testValidMarcSpec2() { $marcSpec = $this->marcspec('245'); $marcSpec['subfields'] = '$d-f'; $this->assertSame(3, count($marcSpec['subfields'])); } - + /** - * assert same specs - */ + * assert same specs. + */ public function testValidMarcSpec4() { $marcSpec = $this->marcspec('...[#]/1-3'); $this->assertSame('...[#]/1-3', "$marcSpec"); } - /** - * assert same specs + * assert same specs. */ public function testValidMarcSpec3() { @@ -124,81 +122,79 @@ public function testValidMarcSpec3() $_subfields = $marcSpec['subfields']; $this->assertSame(1, count($_subfields)); $leftFieldTag = $marcSpec['d'][0]['subSpecs'][0]['leftSubTerm']['field']['tag']; - $this->assertSame('245',$leftFieldTag); + $this->assertSame('245', $leftFieldTag); $rightSubfieldTag = $marcSpec['d'][0]['subSpecs'][1]['rightSubTerm']['subfields'][0]['tag']; - $this->assertSame('.',$marcSpec['d'][0]['subSpecs'][0]['rightSubTerm']['comparable']); + $this->assertSame('.', $marcSpec['d'][0]['subSpecs'][0]['rightSubTerm']['comparable']); } - + /** - * assert same specs + * assert same specs. */ public function testValidMarcSpec5() { $ms = $this->marcspec('245[0]{$a!=$b|300_01$a!~\abc}{\!\=!=\!}$a{$c|!$d}'); - + // field - $this->assertSame('245',$ms['field']['tag']); - $this->assertSame(0,$ms['field']['indexStart']); - $this->assertSame(2,count($ms['field']['subSpecs'])); - + $this->assertSame('245', $ms['field']['tag']); + $this->assertSame(0, $ms['field']['indexStart']); + $this->assertSame(2, count($ms['field']['subSpecs'])); + // field subspecs - + // subspec 00 - $this->assertSame('245',$ms['field']['subSpecs'][0][0]['leftSubTerm']['field']['tag']); - $this->assertSame(0,$ms['field']['subSpecs'][0][0]['leftSubTerm']['field']['indexStart']); - $this->assertSame('a',$ms['field']['subSpecs'][0][0]['leftSubTerm']['subfields'][0]['tag']); - - $this->assertSame('!=',$ms['field']['subSpecs'][0][0]['operator']); - - $this->assertSame('245',$ms['field']['subSpecs'][0][0]['rightSubTerm']['field']['tag']); - $this->assertSame(0,$ms['field']['subSpecs'][0][0]['rightSubTerm']['field']['indexStart']); - $this->assertSame('b',$ms['field']['subSpecs'][0][0]['rightSubTerm']['subfields'][0]['tag']); - + $this->assertSame('245', $ms['field']['subSpecs'][0][0]['leftSubTerm']['field']['tag']); + $this->assertSame(0, $ms['field']['subSpecs'][0][0]['leftSubTerm']['field']['indexStart']); + $this->assertSame('a', $ms['field']['subSpecs'][0][0]['leftSubTerm']['subfields'][0]['tag']); + + $this->assertSame('!=', $ms['field']['subSpecs'][0][0]['operator']); + + $this->assertSame('245', $ms['field']['subSpecs'][0][0]['rightSubTerm']['field']['tag']); + $this->assertSame(0, $ms['field']['subSpecs'][0][0]['rightSubTerm']['field']['indexStart']); + $this->assertSame('b', $ms['field']['subSpecs'][0][0]['rightSubTerm']['subfields'][0]['tag']); + // subspec 01 - $this->assertSame('300',$ms['field']['subSpecs'][0][1]['leftSubTerm']['field']['tag']); - $this->assertSame('a',$ms['field']['subSpecs'][0][1]['leftSubTerm']['subfields'][0]['tag']); - $this->assertSame('0',$ms['field']['subSpecs'][0][1]['leftSubTerm']['field']['indicator1']); - $this->assertSame('1',$ms['field']['subSpecs'][0][1]['leftSubTerm']['field']['indicator2']); - - $this->assertSame('!~',$ms['field']['subSpecs'][0][1]['operator']); - - $this->assertSame('abc',$ms['field']['subSpecs'][0][1]['rightSubTerm']['comparable']); - + $this->assertSame('300', $ms['field']['subSpecs'][0][1]['leftSubTerm']['field']['tag']); + $this->assertSame('a', $ms['field']['subSpecs'][0][1]['leftSubTerm']['subfields'][0]['tag']); + $this->assertSame('0', $ms['field']['subSpecs'][0][1]['leftSubTerm']['field']['indicator1']); + $this->assertSame('1', $ms['field']['subSpecs'][0][1]['leftSubTerm']['field']['indicator2']); + + $this->assertSame('!~', $ms['field']['subSpecs'][0][1]['operator']); + + $this->assertSame('abc', $ms['field']['subSpecs'][0][1]['rightSubTerm']['comparable']); + // subspec 1 - $this->assertSame('!=',$ms['field']['subSpecs'][1]['leftSubTerm']['comparable']); - - $this->assertSame('!=',$ms['field']['subSpecs'][1]['operator']); - - $this->assertSame('!',$ms['field']['subSpecs'][1]['rightSubTerm']['comparable']); + $this->assertSame('!=', $ms['field']['subSpecs'][1]['leftSubTerm']['comparable']); + + $this->assertSame('!=', $ms['field']['subSpecs'][1]['operator']); + + $this->assertSame('!', $ms['field']['subSpecs'][1]['rightSubTerm']['comparable']); // subfields - $this->assertSame('a',$ms['subfields'][0]['tag']); - + $this->assertSame('a', $ms['subfields'][0]['tag']); + // subfield subspec 00 - $this->assertSame('245',$ms['a'][0]['subSpecs'][0][0]['leftSubTerm']['field']['tag']); - $this->assertSame(0,$ms['a'][0]['subSpecs'][0][0]['leftSubTerm']['field']['indexStart']); - $this->assertSame('a',$ms['a'][0]['subSpecs'][0][0]['leftSubTerm']['subfields'][0]['tag']); - - $this->assertSame('?',$ms['a'][0]['subSpecs'][0][0]['operator']); - - $this->assertSame('245',$ms['a'][0]['subSpecs'][0][0]['rightSubTerm']['field']['tag']); - $this->assertSame(0,$ms['a'][0]['subSpecs'][0][0]['rightSubTerm']['field']['indexStart']); - $this->assertSame('c',$ms['a'][0]['subSpecs'][0][0]['rightSubTerm']['subfields'][0]['tag']); - - + $this->assertSame('245', $ms['a'][0]['subSpecs'][0][0]['leftSubTerm']['field']['tag']); + $this->assertSame(0, $ms['a'][0]['subSpecs'][0][0]['leftSubTerm']['field']['indexStart']); + $this->assertSame('a', $ms['a'][0]['subSpecs'][0][0]['leftSubTerm']['subfields'][0]['tag']); + + $this->assertSame('?', $ms['a'][0]['subSpecs'][0][0]['operator']); + + $this->assertSame('245', $ms['a'][0]['subSpecs'][0][0]['rightSubTerm']['field']['tag']); + $this->assertSame(0, $ms['a'][0]['subSpecs'][0][0]['rightSubTerm']['field']['indexStart']); + $this->assertSame('c', $ms['a'][0]['subSpecs'][0][0]['rightSubTerm']['subfields'][0]['tag']); + // subfield subspec 01 - $this->assertSame('245',$ms['a'][0]['subSpecs'][0][1]['leftSubTerm']['field']['tag']); - $this->assertSame(0,$ms['a'][0]['subSpecs'][0][1]['leftSubTerm']['field']['indexStart']); - $this->assertSame('a',$ms['a'][0]['subSpecs'][0][1]['leftSubTerm']['subfields'][0]['tag']); - - $this->assertSame('!',$ms['a'][0]['subSpecs'][0][1]['operator']); - - $this->assertSame('245',$ms['a'][0]['subSpecs'][0][1]['rightSubTerm']['field']['tag']); - $this->assertSame(0,$ms['a'][0]['subSpecs'][0][1]['rightSubTerm']['field']['indexStart']); - $this->assertSame('d',$ms['a'][0]['subSpecs'][0][1]['rightSubTerm']['subfields'][0]['tag']); + $this->assertSame('245', $ms['a'][0]['subSpecs'][0][1]['leftSubTerm']['field']['tag']); + $this->assertSame(0, $ms['a'][0]['subSpecs'][0][1]['leftSubTerm']['field']['indexStart']); + $this->assertSame('a', $ms['a'][0]['subSpecs'][0][1]['leftSubTerm']['subfields'][0]['tag']); + + $this->assertSame('!', $ms['a'][0]['subSpecs'][0][1]['operator']); + $this->assertSame('245', $ms['a'][0]['subSpecs'][0][1]['rightSubTerm']['field']['tag']); + $this->assertSame(0, $ms['a'][0]['subSpecs'][0][1]['rightSubTerm']['field']['indexStart']); + $this->assertSame('d', $ms['a'][0]['subSpecs'][0][1]['rightSubTerm']['subfields'][0]['tag']); } - + /** * @covers CK\MARCspec\Subfield::getIterator * @covers CK\MARCspec\SubSpec::getIterator @@ -214,45 +210,34 @@ public function testIteration() $ms = $this->marcspec('245$a-c{$b|$c}{$e}'); $count = 0; - foreach($ms as $key => $value) - { + foreach ($ms as $key => $value) { $count++; } $this->assertSame(2, $count); - + $count = 0; - foreach($ms['subfields'] as $key => $value) - { + foreach ($ms['subfields'] as $key => $value) { $count++; } $this->assertSame(3, $count); - - - foreach($ms['subfields'] as $subfield) - { + + foreach ($ms['subfields'] as $subfield) { $count = 0; - foreach($subfield['subSpecs'] as $subSpec) - { - if(is_array($subSpec)) - { + foreach ($subfield['subSpecs'] as $subSpec) { + if (is_array($subSpec)) { $this->assertSame(2, count($subSpec)); - } - else - { - foreach($subSpec as $key => $prop) - { - $this->assertTrue(in_array($key,["leftSubTerm","operator","rightSubTerm"])); + } else { + foreach ($subSpec as $key => $prop) { + $this->assertTrue(in_array($key, ['leftSubTerm', 'operator', 'rightSubTerm'])); } } - + $count++; } $this->assertSame(2, $count); } - - } - + /** * @covers CK\MARCspec\Field::offsetExists * @covers CK\MARCspec\Subfield::offsetExists @@ -261,35 +246,35 @@ public function testOffsets() { $ms = $this->marcspec('LDR/0-3'); $this->assertTrue($ms['field']->offsetExists('charLength')); - + $ms = $this->marcspec('LDR/0-#'); $this->assertFalse($ms['field']->offsetExists('charLength')); - + $ms = $this->marcspec('245$a/0-3'); $this->assertTrue($ms['a'][0]->offsetExists('charLength')); - + $ms = $this->marcspec('245$a/#-3'); $this->assertTrue($ms['a'][0]->offsetExists('charLength')); - + $ms = $this->marcspec('245$a/0-#'); $this->assertFalse($ms['a'][0]->offsetExists('charLength')); } - + /** - * @covers CK\MARCspec\Field::jsonSerialize - * @covers CK\MARCspec\Subfield::jsonSerialize - * @covers CK\MARCspec\SubSpec::jsonSerialize - * @covers CK\MARCspec\ComparisonString::jsonSerialize - */ + * @covers CK\MARCspec\Field::jsonSerialize + * @covers CK\MARCspec\Subfield::jsonSerialize + * @covers CK\MARCspec\SubSpec::jsonSerialize + * @covers CK\MARCspec\ComparisonString::jsonSerialize + */ public function testJsonSerialize() { $ms = $this->marcspec('...[0-3]_01{$a|$b!=$c}$a{300/1-3=\abc}{245$a!~\test}'); $encode = json_encode($ms); $test = '{"field":{"tag":"...","indexStart":0,"indexEnd":3,"indexLength":4,"indicator1":"0","indicator2":"1","subSpecs":[[{"leftSubTerm":{"field":{"tag":"...","indexStart":0,"indexEnd":3,"indexLength":4,"indicator1":"0","indicator2":"1"}},"operator":"?","rightSubTerm":{"field":{"tag":"...","indexStart":0,"indexEnd":3,"indexLength":4,"indicator1":"0","indicator2":"1"},"subfields":[{"tag":"a","indexStart":0,"indexEnd":"#"}]}},{"leftSubTerm":{"field":{"tag":"...","indexStart":0,"indexEnd":3,"indexLength":4,"indicator1":"0","indicator2":"1"},"subfields":[{"tag":"b","indexStart":0,"indexEnd":"#"}]},"operator":"!=","rightSubTerm":{"field":{"tag":"...","indexStart":0,"indexEnd":3,"indexLength":4,"indicator1":"0","indicator2":"1"},"subfields":[{"tag":"c","indexStart":0,"indexEnd":"#"}]}}]]},"subfields":[{"tag":"a","indexStart":0,"indexEnd":"#","subSpecs":[{"leftSubTerm":{"field":{"tag":"300","indexStart":0,"indexEnd":"#","charStart":1,"charEnd":3,"charLength":3}},"operator":"=","rightSubTerm":{"comparisonString":"abc"}},{"leftSubTerm":{"field":{"tag":"245","indexStart":0,"indexEnd":"#"},"subfields":[{"tag":"a","indexStart":0,"indexEnd":"#"}]},"operator":"!~","rightSubTerm":{"comparisonString":"test"}}]}]}'; - - $this->assertsame($encode,$test); + + $this->assertsame($encode, $test); } - + /** * @covers CK\MARCspec\Field::__toString * @covers CK\MARCspec\Subfield::__toString @@ -300,49 +285,39 @@ public function testToString() { $ms = $this->marcspec('...[0-3]_01{$a|$b!=$c}$a{300/1-3=\abc}{245$a!~\test}'); $test = '...[0-3]_01{...[0-3]_01$a[0-#]|...[0-3]_01$b[0-#]!=...[0-3]_01$c[0-#]}$a[0-#]{300[0-#]/1-3=\abc}{245[0-#]$a[0-#]!~\test}'; - $this->assertsame($ms->__toString(),$test); + $this->assertsame($ms->__toString(), $test); } - + /** * @expectedException BadMethodCallException */ public function testOffsetUnset() { - $ms = $this->marcspec('245'); + $ms = $this->marcspec('245'); unset($ms['field']); } - + public function testInvalidFromTestSuite() { - foreach($this->invalidTests as $invalid) - { - foreach($invalid->{'tests'} as $test) - { - try - { + foreach ($this->invalidTests as $invalid) { + foreach ($invalid->{'tests'} as $test) { + try { new MARCspec($test->{'data'}); - } - catch(\Exception $e) - { + } catch (\Exception $e) { continue; } $this->fail('An expected exception has not been raised for '.$test->{'data'}); } } } - + public function testValidFromTestSuite() { - foreach($this->validTests as $valid) - { - foreach($valid->{'tests'} as $test) - { - try - { + foreach ($this->validTests as $valid) { + foreach ($valid->{'tests'} as $test) { + try { new MARCspec($test->{'data'}); - } - catch(\Exception $e) - { + } catch (\Exception $e) { $this->fail('An unexpected exception has been raised for '.$test->{'data'}.': '.$e->getMessage()); } } diff --git a/Test/SingleTest.php b/Test/SingleTest.php index 682bc2b..e3a6a75 100755 --- a/Test/SingleTest.php +++ b/Test/SingleTest.php @@ -5,31 +5,28 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace CK\MARCspec\Test; -use CK\MARCspec\MARCspec; use CK\MARCspec\Field; -use CK\MARCspec\Exception\InvalidMARCspecException; +use CK\MARCspec\MARCspec; + #use CK\MARCspec\Subfield; #use CK\MARCspec\SubSpec; class SingleTest extends \PHPUnit_Framework_TestCase { - - public function marcspec($arg) { return new MARCspec($arg); } - public function testSingle() { - $marcSpec = $this->marcspec('245_1{[1]}'); - $marcSpec = $this->marcspec('245_1$[{[1]}'); - $this->assertSame(1, $marcSpec['subfields'][0]['subSpecs'][0]['rightSubTerm']['subfields'][0]['indexStart']); - $marcSpec = $this->marcspec('245{/0}'); - $this->assertSame(0, $marcSpec['field']['subSpecs'][0]['rightSubTerm']['field']['charStart']); + $marcSpec = $this->marcspec('245_1{[1]}'); + $marcSpec = $this->marcspec('245_1$[{[1]}'); + $this->assertSame(1, $marcSpec['subfields'][0]['subSpecs'][0]['rightSubTerm']['subfields'][0]['indexStart']); + $marcSpec = $this->marcspec('245{/0}'); + $this->assertSame(0, $marcSpec['field']['subSpecs'][0]['rightSubTerm']['field']['charStart']); } - } diff --git a/Test/SubSpecTest.php b/Test/SubSpecTest.php index ab94166..f15fbd4 100755 --- a/Test/SubSpecTest.php +++ b/Test/SubSpecTest.php @@ -5,46 +5,46 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace CK\MARCspec\Test; -use CK\MARCspec\MARCspec; -use CK\MARCspec\SubSpec; use CK\MARCspec\Field; +use CK\MARCspec\MARCspec; use CK\MARCspec\Subfield; -use CK\MARCspec\Exception\InvalidMARCspecException; +use CK\MARCspec\SubSpec; class SubSpecTest extends \PHPUnit_Framework_TestCase { - - public function subspec($arg1,$arg2,$arg3) + public function subspec($arg1, $arg2, $arg3) { - return new SubSpec($arg1,$arg2,$arg3); - } + return new SubSpec($arg1, $arg2, $arg3); + } + public function marcspec($arg) { return new MARCspec($arg); } - + /**** * invalid data types ***/ - + /** * @expectedException InvalidArgumentException */ public function testInvalidSubSpec1Decode() { - $this->subspec('245','=','300'); + $this->subspec('245', '=', '300'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubSpec2Decode() { - $this->subspec(new Field('245'),'=',new Subfield('245')); + $this->subspec(new Field('245'), '=', new Subfield('245')); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -53,24 +53,21 @@ public function testInvalidSubSpec3Decode() $this->marcspec('...{$a{$b}}'); } - /** - * assert true + * assert true. */ public function testValidSubSpec1() { $marcspec1 = $this->marcspec('245$a'); $marcspec2 = $this->marcspec('245$b'); - $subspec = $this->subspec($marcspec1,'=',$marcspec2); + $subspec = $this->subspec($marcspec1, '=', $marcspec2); $left = $subspec->getLeftSubTerm(); $right = $subspec->getRightSubTerm(); $operator = $subspec->getOperator(); - $field = $left->getField(); + $field = $left->getField(); $this->assertSame('245', $field->getTag()); $subfields = $right->getSubfields(); - $this->assertInstanceOf('CK\MARCspec\Subfield',$subfields[0] ); + $this->assertInstanceOf('CK\MARCspec\Subfield', $subfields[0]); $this->assertSame('=', $operator); } - - } diff --git a/Test/SubfieldTest.php b/Test/SubfieldTest.php index 1a00249..822344f 100755 --- a/Test/SubfieldTest.php +++ b/Test/SubfieldTest.php @@ -5,53 +5,56 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ + namespace CK\MARCspec\Test; +use CK\MARCspec\MARCspec; use CK\MARCspec\Subfield; use CK\MARCspec\SubSpec; -use CK\MARCspec\MARCspec; -use CK\MARCspec\Exception\InvalidMARCspecException; class SubfieldTest extends \PHPUnit_Framework_TestCase { protected $validTests = []; protected $invalidTests = []; - + protected function setUp() { - if(0 < count($this->validTests)) return; - $this->validTests[] = json_decode(file_get_contents(__DIR__. '/../' ."vendor/ck/marcspec-test-suite/valid/validSubfieldTag.json")); - $this->invalidTests[] = json_decode(file_get_contents(__DIR__. '/../' ."vendor/ck/marcspec-test-suite/invalid/invalidSubfieldTag.json")); + if (0 < count($this->validTests)) { + return; + } + $this->validTests[] = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validSubfieldTag.json')); + $this->invalidTests[] = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidSubfieldTag.json')); } - + public function subfieldspec($arg) { return new Subfield($arg); } - + /**** * invalid data types ***/ - + /** * @expectedException InvalidArgumentException */ public function testInvalidArgument1Decode() { - $this->subfieldspec((int)'$a'); + $this->subfieldspec((int) '$a'); } + /** * @expectedException InvalidArgumentException */ public function testInvalidArgument2Decode() { - $this->subfieldspec(array('$a')); + $this->subfieldspec(['$a']); } - + /**** * invalid subfield tags ***/ - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -59,84 +62,93 @@ public function testInvalidSubfieldSpec1() { $this->subfieldspec(' $a '); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec11() { - $this->subfieldspec('$a/'); + $this->subfieldspec('$a/'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec12() { - $this->subfieldspec('$a$b'); + $this->subfieldspec('$a$b'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec13() { - $this->subfieldspec('|'); + $this->subfieldspec('|'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec14() { - $this->subfieldspec('$a/1-2-'); + $this->subfieldspec('$a/1-2-'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec15() { - $this->subfieldspec('$|'); + $this->subfieldspec('$|'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec16() { - $this->subfieldspec('$a/-2'); + $this->subfieldspec('$a/-2'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec17() { - $this->subfieldspec('$a[-2]'); + $this->subfieldspec('$a[-2]'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec18() { - $this->subfieldspec('$a[1-2-]'); + $this->subfieldspec('$a[1-2-]'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec19() { - $this->subfieldspec('$a[1-2'); + $this->subfieldspec('$a[1-2'); } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec110() { - $this->subfieldspec('$a/1-X'); - } - + $this->subfieldspec('$a/1-X'); + } + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec111() { - $this->subfieldspec('$a/#-'); + $this->subfieldspec('$a/#-'); } /** @@ -144,9 +156,9 @@ public function testInvalidSubfieldSpec111() */ public function testInvalidSubfieldSpec112() { - $this->subfieldspec('$a[0-2a]'); + $this->subfieldspec('$a[0-2a]'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -154,7 +166,7 @@ public function testInvalidSubfieldSpec113() { $this->subfieldspec('$[1-]'); } - + /** * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ @@ -163,25 +175,24 @@ public function testInvalidSubfieldSpec114() $this->subfieldspec('$a{$b}'); } - /** - * assert same properties + * assert same properties. */ public function testValidSubfieldSpec001() { $Subfield = $this->subfieldspec('$a'); $this->assertSame('a', $Subfield->getTag()); } - + /** - * assert same properties + * assert same properties. */ public function testValidSubfieldSpec01() { $Subfield = $this->subfieldspec(null); $Subfield->setTag('a'); $this->assertSame('a', $Subfield->getTag()); - $Subspec = new SubSpec(new MARCspec('245$b'),'!=',new MARCspec('245$c')); + $Subspec = new SubSpec(new MARCspec('245$b'), '!=', new MARCspec('245$c')); $Subfield->addSubSpec($Subspec); $this->assertSame(1, count($Subfield->getSubSpecs())); } @@ -192,175 +203,169 @@ public function testValidSubfieldSpec01() public function testInvalidSubSpecAdd1() { $Subfield = $this->subfieldspec('a'); - $Subspec = new SubSpec('245$b','!','245$b'); + $Subspec = new SubSpec('245$b', '!', '245$b'); $Subfield->addSubSpec($Subspec); } /** - * assert same subfield tag + * assert same subfield tag. */ public function testValidSubfieldSpec1() { - $Subfield = $this->subfieldspec('$a'); - $this->assertSame('a', $Subfield->getTag()); - - $Subfield = $this->subfieldspec('$a[1]'); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame(1, $Subfield->getIndexStart()); - - $Subfield = $this->subfieldspec('$a[1-3]'); - $this->assertSame(1, $Subfield->getIndexStart()); - $this->assertSame(3, $Subfield->getIndexEnd()); - - $Subfield = $this->subfieldspec('$a[1-#]'); - $this->assertSame(1, $Subfield->getIndexStart()); - $this->assertSame('#', $Subfield->getIndexEnd()); - - $Subfield = $this->subfieldspec('$a[#-3]'); - $this->assertSame('#', $Subfield->getIndexStart()); - $this->assertSame(3, $Subfield->getIndexEnd()); + $Subfield = $this->subfieldspec('$a'); + $this->assertSame('a', $Subfield->getTag()); + + $Subfield = $this->subfieldspec('$a[1]'); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame(1, $Subfield->getIndexStart()); + + $Subfield = $this->subfieldspec('$a[1-3]'); + $this->assertSame(1, $Subfield->getIndexStart()); + $this->assertSame(3, $Subfield->getIndexEnd()); + + $Subfield = $this->subfieldspec('$a[1-#]'); + $this->assertSame(1, $Subfield->getIndexStart()); + $this->assertSame('#', $Subfield->getIndexEnd()); + + $Subfield = $this->subfieldspec('$a[#-3]'); + $this->assertSame('#', $Subfield->getIndexStart()); + $this->assertSame(3, $Subfield->getIndexEnd()); } - - /** - * test character position and range + * test character position and range. */ public function testValidSubfieldSpec2() { - $Subfield = $this->subfieldspec('$a/0-3'); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame(0, $Subfield->getCharStart()); - $this->assertSame(3, $Subfield->getCharEnd()); - $this->assertSame(4, $Subfield->getCharLength()); - - $Subfield = $this->subfieldspec('$a/0-#'); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame(0, $Subfield->getCharStart()); - $this->assertSame('#', $Subfield->getCharEnd()); - $this->assertSame(null, $Subfield->getCharLength()); - - $Subfield = $this->subfieldspec('$a/#-4'); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame('#', $Subfield->getCharStart()); - $this->assertSame(4, $Subfield->getCharEnd()); - $this->assertSame(5, $Subfield->getCharLength()); - + $Subfield = $this->subfieldspec('$a/0-3'); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame(0, $Subfield->getCharStart()); + $this->assertSame(3, $Subfield->getCharEnd()); + $this->assertSame(4, $Subfield->getCharLength()); + + $Subfield = $this->subfieldspec('$a/0-#'); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame(0, $Subfield->getCharStart()); + $this->assertSame('#', $Subfield->getCharEnd()); + $this->assertSame(null, $Subfield->getCharLength()); + + $Subfield = $this->subfieldspec('$a/#-4'); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame('#', $Subfield->getCharStart()); + $this->assertSame(4, $Subfield->getCharEnd()); + $this->assertSame(5, $Subfield->getCharLength()); } - - /** - * test character range + /** + * test character range. */ public function testValidSubfieldSpec22() { - $Subfield = $this->subfieldspec('$a/#'); - $this->assertSame(1, $Subfield->getCharLength()); - $Subfield = $this->subfieldspec('$a/#-#'); - $this->assertSame(1, $Subfield->getCharLength()); - $Subfield = $this->subfieldspec('$a/#-0'); - $this->assertSame(1, $Subfield->getCharLength()); - $Subfield = $this->subfieldspec('$a/#-1'); - $this->assertSame(2, $Subfield->getCharLength()); - $Subfield = $this->subfieldspec('$a/0-#'); - $this->assertSame(0, $Subfield->getCharStart()); - $this->assertSame("#", $Subfield->getCharEnd()); - $this->assertSame(null, $Subfield->getCharLength()); + $Subfield = $this->subfieldspec('$a/#'); + $this->assertSame(1, $Subfield->getCharLength()); + $Subfield = $this->subfieldspec('$a/#-#'); + $this->assertSame(1, $Subfield->getCharLength()); + $Subfield = $this->subfieldspec('$a/#-0'); + $this->assertSame(1, $Subfield->getCharLength()); + $Subfield = $this->subfieldspec('$a/#-1'); + $this->assertSame(2, $Subfield->getCharLength()); + $Subfield = $this->subfieldspec('$a/0-#'); + $this->assertSame(0, $Subfield->getCharStart()); + $this->assertSame('#', $Subfield->getCharEnd()); + $this->assertSame(null, $Subfield->getCharLength()); } - - /** - * test character position and range + * test character position and range. */ public function testSetAndGetChar() { - $Subfield = $this->subfieldspec('$a'); - $Subfield->setCharStartEnd('0','3'); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame(0, $Subfield->getCharStart()); - $this->assertSame(3, $Subfield->getCharEnd()); - $this->assertSame(4, $Subfield->getCharLength()); - - $Subfield = $this->subfieldspec('$a'); - $Subfield->setCharStartEnd("#",3); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame("#", $Subfield->getCharStart()); - $this->assertSame(3, $Subfield->getCharEnd()); - $this->assertSame(4, $Subfield->getCharLength()); - - $Subfield = $this->subfieldspec('$a'); - $Subfield->setCharStartEnd(0,4); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame(0, $Subfield->getCharStart()); - $this->assertSame(4, $Subfield->getCharEnd()); - $this->assertSame(5, $Subfield->getCharLength()); - - $Subfield = $this->subfieldspec('$a'); - $Subfield->setCharStartLength("#",4); - $this->assertSame('a', $Subfield->getTag()); - $this->assertSame("#", $Subfield->getCharStart()); - $this->assertSame(3, $Subfield->getCharEnd()); - $this->assertSame(4, $Subfield->getCharLength()); + $Subfield = $this->subfieldspec('$a'); + $Subfield->setCharStartEnd('0', '3'); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame(0, $Subfield->getCharStart()); + $this->assertSame(3, $Subfield->getCharEnd()); + $this->assertSame(4, $Subfield->getCharLength()); + + $Subfield = $this->subfieldspec('$a'); + $Subfield->setCharStartEnd('#', 3); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame('#', $Subfield->getCharStart()); + $this->assertSame(3, $Subfield->getCharEnd()); + $this->assertSame(4, $Subfield->getCharLength()); + + $Subfield = $this->subfieldspec('$a'); + $Subfield->setCharStartEnd(0, 4); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame(0, $Subfield->getCharStart()); + $this->assertSame(4, $Subfield->getCharEnd()); + $this->assertSame(5, $Subfield->getCharLength()); + + $Subfield = $this->subfieldspec('$a'); + $Subfield->setCharStartLength('#', 4); + $this->assertSame('a', $Subfield->getTag()); + $this->assertSame('#', $Subfield->getCharStart()); + $this->assertSame(3, $Subfield->getCharEnd()); + $this->assertSame(4, $Subfield->getCharLength()); } /** - * test index position and range + * test index position and range. */ public function testSetAndGetIndex() { $Subfield = $this->subfieldspec('$a'); - $Subfield->setIndexStartEnd('0','3'); + $Subfield->setIndexStartEnd('0', '3'); $this->assertSame('a', $Subfield->getTag()); $this->assertSame(0, $Subfield->getIndexStart()); $this->assertSame(3, $Subfield->getIndexEnd()); $this->assertSame(4, $Subfield->getIndexLength()); - + $Subfield = $this->subfieldspec('$a'); - $Subfield->setIndexStartEnd("#",3); + $Subfield->setIndexStartEnd('#', 3); $this->assertSame('a', $Subfield->getTag()); - $this->assertSame("#", $Subfield->getIndexStart()); + $this->assertSame('#', $Subfield->getIndexStart()); $this->assertSame(3, $Subfield->getIndexEnd()); $this->assertSame(4, $Subfield->getIndexLength()); - + $Subfield = $this->subfieldspec('$a'); - $Subfield->setIndexStartEnd(0,4); + $Subfield->setIndexStartEnd(0, 4); $this->assertSame('a', $Subfield->getTag()); $this->assertSame(0, $Subfield->getIndexStart()); $this->assertSame(4, $Subfield->getIndexEnd()); $this->assertSame(5, $Subfield->getIndexLength()); - + $Subfield = $this->subfieldspec('$a'); - $Subfield->setIndexStartLength("#",4); + $Subfield->setIndexStartLength('#', 4); $this->assertSame('a', $Subfield->getTag()); - $this->assertSame("#", $Subfield->getIndexStart()); + $this->assertSame('#', $Subfield->getIndexStart()); $this->assertSame(3, $Subfield->getIndexEnd()); $this->assertSame(4, $Subfield->getIndexLength()); } + /** - * test encoding + * test encoding. */ public function testEncode() { - $Subfield = $this->subfieldspec('$a'); $this->assertSame('$a[0-#]', "$Subfield"); - + $Subfield = $this->subfieldspec('$a/1'); - $this->assertSame('$a[0-#]/1',"$Subfield"); + $this->assertSame('$a[0-#]/1', "$Subfield"); $this->assertSame(1, $Subfield->getCharLength()); - + $Subfield = $this->subfieldspec('$a/1-3'); - $this->assertSame('$a[0-#]/1-3',"$Subfield"); + $this->assertSame('$a[0-#]/1-3', "$Subfield"); $this->assertSame(3, $Subfield->getCharLength()); - + $Subfield = $this->subfieldspec('$a[1]'); - $this->assertSame('$a[1]',"$Subfield"); - + $this->assertSame('$a[1]', "$Subfield"); + $Subfield = $this->subfieldspec('$a[1-3]'); - $this->assertSame('$a[1-3]',"$Subfield"); + $this->assertSame('$a[1-3]', "$Subfield"); } - + /** * @expectedException BadMethodCallException */ @@ -369,38 +374,28 @@ public function testOffsetUnset() $Subfield = $this->subfieldspec('$a'); unset($Subfield['tag']); } - + public function testInvalidFromTestSuite() { - foreach($this->invalidTests as $invalid) - { - foreach($invalid->{'tests'} as $test) - { - try - { + foreach ($this->invalidTests as $invalid) { + foreach ($invalid->{'tests'} as $test) { + try { new Subfield($test->{'data'}); - } - catch(\Exception $e) - { + } catch (\Exception $e) { continue; } $this->fail('An expected exception has not been raised for '.$test->{'data'}); } } } - + public function testValidFromTestSuite() { - foreach($this->validTests as $valid) - { - foreach($valid->{'tests'} as $test) - { - try - { + foreach ($this->validTests as $valid) { + foreach ($valid->{'tests'} as $test) { + try { new Subfield($test->{'data'}); - } - catch(\Exception $e) - { + } catch (\Exception $e) { $this->fail('An unexpected exception has been raised for '.$test->{'data'}.': '.$e->getMessage()); } } diff --git a/Test/autoload.php b/Test/autoload.php index ac4c779..aacfa18 100755 --- a/Test/autoload.php +++ b/Test/autoload.php @@ -1,13 +1,14 @@