diff --git a/.gitignore b/.gitignore index b269b40..1c115db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor/ composer.lock .phpunit.result.cache +.phpunit.cache/ build/ diff --git a/.travis.yml b/.travis.yml index c327c45..b549d7d 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,8 @@ language: php php: - - 7.4 - - 8.0 - - 8.1 - 8.2 - 8.3 + - 8.4 fast_finish: true diff --git a/README.md b/README.md index 41b88ef..32a87f5 100755 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ +## Fork (redcuillin) + +Fork of [MARCspec/php-marc-spec](https://github.com/MARCspec/php-marc-spec) for PHP 8.4 and 8.5 support. + +### Using this fork with Composer + +Add the following to your `composer.json` `repositories` and `require` sections (merge with existing entries as needed): + +```json +"repositories": [ + { + "type": "vcs", + "url": "https://github.com/redcuillin/php-marc-spec" + } +], +"require": { + "ck/php-marcspec": "dev-master as 2.999.0" +} +``` + +----- + +## Upstream readme: + # PHP-MARCspec [![Build Status](https://travis-ci.org/MARCspec/php-marc-spec.svg?branch=master)](https://travis-ci.org/MARCspec/php-marc-spec) @@ -14,7 +38,7 @@ Installation can be done by using [composer](https://getcomposer.org/doc/00-intr composer require ck/php-marcspec ``` -PHP-MARCspec requires PHP 7.4 or later. +PHP-MARCspec requires PHP 8.2 or later and is compatible with PHP 8.4. # Usage diff --git a/Test/ComparisonStringTest.php b/Test/ComparisonStringTest.php index 22f4f55..384ebab 100755 --- a/Test/ComparisonStringTest.php +++ b/Test/ComparisonStringTest.php @@ -9,23 +9,21 @@ namespace CK\MARCspec\Test; use CK\MARCspec\ComparisonString; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ComparisonStringTest extends TestCase { - /** - * @dataProvider invalidFromTestSuiteProvider - * - * @expectedException Exception - */ + #[DataProvider('invalidFromTestSuiteProvider')] public function testInvalidFromTestSuite($test) { + $this->expectException(\Exception::class); new ComparisonString($test); } - public function invalidFromTestSuiteProvider() + public static function invalidFromTestSuiteProvider() { - $invalidTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidComparisonString.json')); + $invalidTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/invalid/invalidComparisonString.json')); $data = []; foreach ($invalidTests->{'tests'} as $test) { $data[0][] = $test->{'data'}; @@ -36,10 +34,10 @@ public function invalidFromTestSuiteProvider() public function testValidFromTestSuite() { - $validTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validComparisonString.json')); + $validTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/valid/validComparisonString.json')); foreach ($validTests->{'tests'} as $test) { new ComparisonString($test->{'data'}); - $this->assertSame(1, preg_match('/'.$validTests->{'schema'}->{'pattern'}.'/', $test->{'data'})); + $this->assertSame(1, preg_match('/' . $validTests->{'schema'}->{'pattern'} . '/', $test->{'data'})); } } @@ -49,22 +47,22 @@ public function compare($arg) } /**** - * invalid data types - ***/ + * invalid data types + ***/ /** - * @expectedException InvalidArgumentException */ public function testInvalidArgument1Decode() { + $this->expectException(\InvalidArgumentException::class); $this->compare(['a']); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidArgument2Decode() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->compare('.{.'); } @@ -100,10 +98,10 @@ public function testValidComparisonString2() } /** - * @expectedException BadMethodCallException */ public function testOffsetUnset() { + $this->expectException(\BadMethodCallException::class); $escaped_string = '\\.'; $compare = $this->compare($escaped_string); unset($compare['raw']); diff --git a/Test/FieldTest.php b/Test/FieldTest.php index 9564d7f..6f9ad54 100755 --- a/Test/FieldTest.php +++ b/Test/FieldTest.php @@ -11,23 +11,21 @@ use CK\MARCspec\Field; use CK\MARCspec\MARCspec; use CK\MARCspec\SubSpec; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class FieldTest extends TestCase { - /** - * @dataProvider invalidFromTestSuiteProvider - * - * @expectedException Exception - */ + #[DataProvider('invalidFromTestSuiteProvider')] public function testInvalidFromTestSuite($test) { + $this->expectException(\Exception::class); new Field($test); } - public function invalidFromTestSuiteProvider() + public static function invalidFromTestSuiteProvider() { - $invalidTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidFieldTag.json')); + $invalidTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/invalid/invalidFieldTag.json')); $data = []; foreach ($invalidTests->{'tests'} as $test) { $data[0][] = $test->{'data'}; @@ -38,7 +36,7 @@ public function invalidFromTestSuiteProvider() public function testValidFromTestSuite() { - $validTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validFieldTag.json')); + $validTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/valid/validFieldTag.json')); foreach ($validTests->{'tests'} as $test) { $this->assertInstanceOf('CK\MARCspec\FieldInterface', new Field($test->{'data'})); } @@ -50,146 +48,146 @@ public function fieldspec($arg) } /** - * @expectedException InvalidArgumentException */ public function testInvalidArgument2Decode() { + $this->expectException(\InvalidArgumentException::class); $this->fieldspec(['245']); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec13() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('007/'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec14() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('007/1-2-'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec15() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('24#'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec16() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('007/-2'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec17() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245[-2]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec18() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245[1-2-]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec19() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245[1-2'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec110() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('007/1-X'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec111() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('007/#-'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec112() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245[0-2a]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec113() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('300[1-]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec114() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $fieldSpec = $this->fieldspec('aA0'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec31() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245^1'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec32() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245_$'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidFieldSpec33() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245^'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidArgument310Decode() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245{$c=$d}$a'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidArgument311Decode() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->fieldspec('245[1]/1_01'); } @@ -274,9 +272,7 @@ public function testValidFieldSpec22() } /** - * @covers CK\MARCspec\Field::offsetSet - * @covers CK\MARCspec\Field::offsetExists - * @covers CK\MARCspec\Field::addSubSpec + * Field offset API and subspecs. */ public function testValidFieldSpec24() { @@ -391,7 +387,7 @@ public function testEncode() } /** - * @covers CK\MARCspec\Field::jsonSerialize + * JSON encoding of a field spec. */ public function testJson() { @@ -407,10 +403,10 @@ public function testJson() } /** - * @expectedException BadMethodCallException */ public function testOffsetUnset() { + $this->expectException(\BadMethodCallException::class); $fieldSpec = $this->fieldspec('245'); unset($fieldSpec['tag']); } diff --git a/Test/IndicatorTest.php b/Test/IndicatorTest.php index 0a826d0..ea4e774 100644 --- a/Test/IndicatorTest.php +++ b/Test/IndicatorTest.php @@ -11,6 +11,7 @@ use CK\MARCspec\Indicator; use CK\MARCspec\MARCspec; use CK\MARCspec\SubSpec; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class IndicatorTest extends TestCase @@ -22,19 +23,16 @@ protected function indicatorPos($arg) return $ind->getPos(); } - /** - * @dataProvider invalidFromTestSuiteProvider - * - * @expectedException Exception - */ + #[DataProvider('invalidFromTestSuiteProvider')] public function testInvalidFromTestSuite($test) { + $this->expectException(\Exception::class); new Indicator($test); } - public function invalidFromTestSuiteProvider() + public static function invalidFromTestSuiteProvider() { - $invalidTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidIndicators.json')); + $invalidTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/invalid/invalidIndicators.json')); $data = []; foreach ($invalidTests->{'tests'} as $test) { $data[0][] = $test->{'data'}; @@ -45,7 +43,7 @@ public function invalidFromTestSuiteProvider() public function testValidFromTestSuite() { - $validTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validIndicators.json')); + $validTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/valid/validIndicators.json')); foreach ($validTests->{'tests'} as $test) { $pos = $this->indicatorPos($test->{'data'}); $this->assertTrue($pos === '1' or $pos === '2'); @@ -53,10 +51,10 @@ public function testValidFromTestSuite() } /** - * @expectedException InvalidArgumentException */ public function testPosFail() { + $this->expectException(\InvalidArgumentException::class); $ind = new Indicator('3'); } @@ -72,34 +70,34 @@ public function testSetPos() } /** - * @expectedException InvalidArgumentException */ public function testIndicatorFail1() { + $this->expectException(\InvalidArgumentException::class); $ind = new Indicator(3); } /** - * @expectedException InvalidArgumentException */ public function testIndicatorFail2() { + $this->expectException(\InvalidArgumentException::class); $ind = new Indicator(['3']); } /** - * @expectedException InvalidArgumentException */ public function testIndicatorFail3() { + $this->expectException(\InvalidArgumentException::class); $ind = new Indicator('12'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testIndicatorFail4() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $ind = new Indicator('1{$a}'); } @@ -115,7 +113,7 @@ public function testSubspecsAndArrayAccessAndToString() } /** - * @covers CK\MARCspec\Indicator::jsonSerialize + * JSON encoding of an indicator spec. */ public function testJson() { diff --git a/Test/MarcSpecTest.php b/Test/MarcSpecTest.php index bf15190..e0faa7b 100755 --- a/Test/MarcSpecTest.php +++ b/Test/MarcSpecTest.php @@ -10,23 +10,21 @@ use CK\MARCspec\Field; use CK\MARCspec\MARCspec; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class MarcSpecTest extends TestCase { - /** - * @dataProvider invalidFromTestSuiteProvider - * - * @expectedException Exception - */ + #[DataProvider('invalidFromTestSuiteProvider')] public function testInvalidFromTestSuite($test) { + $this->expectException(\Exception::class); new MARCspec($test); } - public function invalidFromTestSuiteProvider() + public static function invalidFromTestSuiteProvider() { - foreach (glob(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/wildCombination_*.json') as $filename) { + foreach (glob(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/invalid/wildCombination_*.json') as $filename) { $invalidTests = json_decode(file_get_contents($filename)); } $data = []; @@ -39,7 +37,7 @@ public function invalidFromTestSuiteProvider() public function testValidFromTestSuite() { - foreach (glob(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/wildCombination_*.json') as $filename) { + foreach (glob(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/valid/wildCombination_*.json') as $filename) { $validTests = json_decode(file_get_contents($filename)); } foreach ($validTests->{'tests'} as $test) { @@ -53,38 +51,38 @@ public function marcspec($arg) } /**** - * invalid data types - ***/ + * invalid data types + ***/ /** - * @expectedException RuntimeException */ public function testInvalidArgument01Decode() { + $this->expectException(\RuntimeException::class); $this->marcspec('24'); } /** - * @expectedException InvalidArgumentException */ public function testInvalidArgument1Decode() { + $this->expectException(\InvalidArgumentException::class); $this->marcspec((int) '245$a'); } /** - * @expectedException InvalidArgumentException */ public function testInvalidArgument2Decode() { + $this->expectException(\InvalidArgumentException::class); $this->marcspec(['245$a']); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidArgument3Decode() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->marcspec('245/#$a'); } @@ -268,10 +266,10 @@ public function testToString() } /** - * @expectedException BadMethodCallException */ public function testOffsetUnset() { + $this->expectException(\BadMethodCallException::class); $ms = $this->marcspec('245'); unset($ms['field']); } diff --git a/Test/SubSpecTest.php b/Test/SubSpecTest.php index e778b6c..34cc21f 100755 --- a/Test/SubSpecTest.php +++ b/Test/SubSpecTest.php @@ -27,30 +27,30 @@ protected function marcspec($arg) } /**** - * invalid data types - ***/ + * invalid data types + ***/ /** - * @expectedException InvalidArgumentException */ public function testInvalidSubSpec1Decode() { + $this->expectException(\InvalidArgumentException::class); $this->subspec('245', '=', '300'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubSpec2Decode() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subspec(new Field('245'), '=', new Subfield('245')); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubSpec3Decode() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->marcspec('...{$a{$b}}'); } diff --git a/Test/SubfieldTest.php b/Test/SubfieldTest.php index 1cc6ac6..9fdf08d 100755 --- a/Test/SubfieldTest.php +++ b/Test/SubfieldTest.php @@ -11,23 +11,21 @@ use CK\MARCspec\MARCspec; use CK\MARCspec\Subfield; use CK\MARCspec\SubSpec; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class SubfieldTest extends TestCase { - /** - * @dataProvider invalidFromTestSuiteProvider - * - * @expectedException Exception - */ + #[DataProvider('invalidFromTestSuiteProvider')] public function testInvalidFromTestSuite($test) { + $this->expectException(\Exception::class); new Subfield($test); } - public function invalidFromTestSuiteProvider() + public static function invalidFromTestSuiteProvider() { - $invalidTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidSubfieldTag.json')); + $invalidTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/invalid/invalidSubfieldTag.json')); $data = []; foreach ($invalidTests->{'tests'} as $test) { $data[0][] = $test->{'data'}; @@ -38,7 +36,7 @@ public function invalidFromTestSuiteProvider() public function testValidFromTestSuite() { - $validTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validSubfieldTag.json')); + $validTests = json_decode(file_get_contents(__DIR__ . '/../' . 'vendor/ck/marcspec-test-suite/valid/validSubfieldTag.json')); foreach ($validTests->{'tests'} as $test) { $this->assertInstanceOf('CK\MARCspec\SubfieldInterface', new Subfield($test->{'data'})); } @@ -50,146 +48,146 @@ public function subfieldspec($arg) } /**** - * invalid data types - ***/ + * invalid data types + ***/ /** - * @expectedException InvalidArgumentException */ public function testInvalidArgument1Decode() { + $this->expectException(\InvalidArgumentException::class); $this->subfieldspec((int) '$a'); } /** - * @expectedException InvalidArgumentException */ public function testInvalidArgument2Decode() { + $this->expectException(\InvalidArgumentException::class); $this->subfieldspec(['$a']); } /**** - * invalid subfield tags - ***/ + * invalid subfield tags + ***/ /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec1() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec(' $a '); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec11() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a/'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec12() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a$b'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec13() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('|'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec14() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a/1-2-'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec15() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$|'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec16() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a/-2'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec17() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a[-2]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec18() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a[1-2-]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec19() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a[1-2'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec110() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a/1-X'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec111() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a/#-'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec112() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a[0-2a]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec113() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$[1-]'); } /** - * @expectedException CK\MARCspec\Exception\InvalidMARCspecException */ public function testInvalidSubfieldSpec114() { + $this->expectException(\CK\MARCspec\Exception\InvalidMARCspecException::class); $this->subfieldspec('$a{$b}'); } @@ -215,10 +213,10 @@ public function testValidSubfieldSpec01() } /** - * @expectedException InvalidArgumentException */ public function testInvalidSubSpecAdd1() { + $this->expectException(\InvalidArgumentException::class); $Subfield = $this->subfieldspec('a'); $Subspec = new SubSpec('245$b', '!', '245$b'); $Subfield->addSubSpec($Subspec); @@ -384,10 +382,10 @@ public function testEncode() } /** - * @expectedException BadMethodCallException */ public function testOffsetUnset() { + $this->expectException(\BadMethodCallException::class); $Subfield = $this->subfieldspec('$a'); unset($Subfield['tag']); } diff --git a/composer.json b/composer.json index b8b395d..ef6ad78 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,10 @@ "name": "ck/php-marcspec", "type": "library", "description": "PHP based MARCspec parser and validator", - "keywords": [ "MARCspec", "MARC 21" ], + "keywords": [ + "MARCspec", + "MARC 21" + ], "license": "MIT", "authors": [ { @@ -12,11 +15,11 @@ } ], "require": { - "php": ">=7.4" + "php": ">=8.2" }, "require-dev": { - "phpunit/phpunit": "~8.5.52", - "ck/marcspec-test-suite": "@dev" + "phpunit/phpunit": "^11.5", + "ck/marcspec-test-suite": "@dev" }, "autoload": { "psr-4": { @@ -24,4 +27,4 @@ "CK\\MARCspec\\Exception\\": "src/Exception/" } } -} +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 90adbb8..d58fe9b 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,33 +1,16 @@ - - - - ./Test/ - - - - - ./ - - ./Test - ./vendor - - - - - - - - - - - \ No newline at end of file + + + + Test + + + + + + + + src + + + diff --git a/src/ComparisonString.php b/src/ComparisonString.php index 9092cf4..75f499c 100755 --- a/src/ComparisonString.php +++ b/src/ComparisonString.php @@ -1,4 +1,5 @@ raw; + return '\\' . $this->raw; } /** * {@inheritdoc} */ - public function jsonSerialize() + public function jsonSerialize(): mixed { return ['comparisonString' => $this->raw]; } @@ -109,13 +110,13 @@ public function jsonSerialize() * * @param string $offset Key raw|comparable */ - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { switch ($offset) { case 'raw': case 'comparable': return true; - break; + break; default: return false; } @@ -128,15 +129,15 @@ public function offsetExists($offset) * * @param string $offset Key operator|leftSubTerm|rightSubTerm */ - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { switch ($offset) { case 'raw': return $this->getRaw(); - break; + break; case 'comparable': return $this->getComparable(); - break; + break; default: throw new \UnexpectedValueException("Offset $offset does not exist."); } @@ -149,7 +150,7 @@ public function offsetGet($offset) * * @param string $offset */ - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { throw new \UnexpectedValueException("Offset $offset cannot be set."); } @@ -159,7 +160,7 @@ public function offsetSet($offset, $value) * * @param string $offset */ - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { throw new \BadMethodCallException("Offset $offset can not be unset."); } diff --git a/src/Field.php b/src/Field.php index b27603d..dc428ca 100755 --- a/src/Field.php +++ b/src/Field.php @@ -1,4 +1,5 @@ $specLength) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::MINIMUM3, + InvalidMARCspecException::FS . + InvalidMARCspecException::MINIMUM3, $fieldspec ); } if (preg_match('/\s/', $fieldspec)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::SPACE, + InvalidMARCspecException::FS . + InvalidMARCspecException::SPACE, $fieldspec ); } if ($strpos = strpos($fieldspec, '{')) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::DETECTEDSS, + InvalidMARCspecException::FS . + InvalidMARCspecException::DETECTEDSS, $fieldspec ); } @@ -89,16 +90,16 @@ public function __construct($fieldspec) if (array_key_exists('subfields', $parser->parsed)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::DETECTEDSF, + InvalidMARCspecException::FS . + InvalidMARCspecException::DETECTEDSF, $fieldspec ); } if (array_key_exists('indicatorpos', $parser->parsed)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::DETECTEDIN, + InvalidMARCspecException::FS . + InvalidMARCspecException::DETECTEDIN, $fieldspec ); } @@ -146,8 +147,8 @@ private function validateTag($tag) { if (!preg_match('/[.0-9a-z]{3,3}|[.0-9A-Z]{3,3}/', $tag)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::FTAG, + InvalidMARCspecException::FS . + InvalidMARCspecException::FTAG, $tag ); } @@ -173,7 +174,7 @@ public function addSubSpec($SubSpec) } else { throw new \InvalidArgumentException('Param 1 must be instance of SubSpecInterface or array with instances of SubSpecInterface. Got "' - .gettype($subSpec).'".'); + . gettype($SubSpec) . '".'); } } @@ -188,7 +189,7 @@ public function getSubSpecs() /** * {@inheritdoc} */ - public function jsonSerialize() + public function jsonSerialize(): mixed { $_fieldSpec['tag'] = $this->getTag(); @@ -238,9 +239,9 @@ public function getBaseSpec() if ($indexStart === 0 && $indexEnd === '#') { // use abbreviation } else { - $fieldSpec .= '['.$indexStart; + $fieldSpec .= '[' . $indexStart; if ($indexStart !== $indexEnd) { - $fieldSpec .= '-'.$indexEnd; + $fieldSpec .= '-' . $indexEnd; } $fieldSpec .= ']'; } @@ -250,9 +251,9 @@ public function getBaseSpec() if ($charStart === 0 && $charEnd === '#') { // use abbreviation } else { - $fieldSpec .= '/'.$charStart; + $fieldSpec .= '/' . $charStart; if ($charEnd !== $charStart) { - $fieldSpec .= '-'.$charEnd; + $fieldSpec .= '-' . $charEnd; } } } @@ -273,9 +274,9 @@ public function __toString() foreach ($subSpec as $orKey => $orSubSpec) { $subSpec[$orKey] = $orSubSpec->__toString(); } - $fieldSpec .= '{'.implode('|', $subSpec).'}'; + $fieldSpec .= '{' . implode('|', $subSpec) . '}'; } else { - $fieldSpec .= '{'.$subSpec->__toString().'}'; + $fieldSpec .= '{' . $subSpec->__toString() . '}'; } } } @@ -291,32 +292,33 @@ public function __toString() * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength * |indicator1|indicator2|subSpecs */ - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { switch ($offset) { case 'tag': return isset($this->tag); - break; + break; case 'indexStart': return isset($this->indexStart); - break; + break; case 'indexEnd': return isset($this->indexEnd); - break; + break; case 'indexLength': return !is_null($this->getIndexLength()); - break; + break; case 'charStart': return isset($this->charStart); - break; + break; case 'charEnd': return isset($this->charEnd); - break; + break; case 'charLength': return !is_null($this->getCharLength()); - break; - case 'subSpecs': return (0 < count($this->subSpecs)) ? true : false; - break; + break; + case 'subSpecs': + return (0 < count($this->subSpecs)) ? true : false; + break; default: return false; } @@ -330,32 +332,33 @@ public function offsetExists($offset) * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength * |indicator1|indicator2|subSpecs */ - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { switch ($offset) { case 'tag': return $this->getTag(); - break; + break; case 'indexStart': return $this->getIndexStart(); - break; + break; case 'indexEnd': return $this->getIndexEnd(); - break; + break; case 'indexLength': return $this->getIndexLength(); - break; + break; case 'charStart': return $this->getCharStart(); - break; + break; case 'charEnd': return $this->getCharEnd(); - break; + break; case 'charLength': return $this->getCharLength(); - break; - case 'subSpecs': return $this->getSubSpecs(); - break; + break; + case 'subSpecs': + return $this->getSubSpecs(); + break; default: throw new \UnexpectedValueException("Offset $offset does not exist."); } @@ -369,7 +372,7 @@ public function offsetGet($offset) * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength * |indicator1|indicator2|subSpecs */ - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { switch ($offset) { case 'indexStart': @@ -386,7 +389,7 @@ public function offsetSet($offset, $value) case 'indexLength': throw new \UnexpectedValueException('indexLength is always calculated.'); - break; + break; case 'charStart': $this->setCharStartEnd($value); @@ -402,10 +405,11 @@ public function offsetSet($offset, $value) case 'charLength': throw new \UnexpectedValueException('CharLength is always calculated.'); - break; + break; - case 'subSpecs': $this->addSubSpec($value); - break; + case 'subSpecs': + $this->addSubSpec($value); + break; default: throw new \UnexpectedValueException("Offset $offset cannot be set."); @@ -415,7 +419,7 @@ public function offsetSet($offset, $value) /** * Access object like an associative array. */ - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { throw new \BadMethodCallException("Offset $offset can not be unset."); } diff --git a/src/Indicator.php b/src/Indicator.php index 89ebb73..600aa6e 100644 --- a/src/Indicator.php +++ b/src/Indicator.php @@ -1,4 +1,5 @@ subSpecs[] = $SubSpec; } else { - throw new \InvalidArgumentException('Param 1 must be instance of + throw new \InvalidArgumentException( + 'Param 1 must be instance of SubSpecInterface or array with instances of SubSpecInterface. Got "' - .gettype($subSpec).'".' + . gettype($SubSpec) . '".' ); } } @@ -101,7 +105,7 @@ public function addSubSpec($SubSpec) */ public function getBaseSpec() { - return '^'.$this->position; + return '^' . $this->position; } /** @@ -117,9 +121,9 @@ public function __toString() foreach ($subSpec as $orKey => $orSubSpec) { $subSpec[$orKey] = $orSubSpec->__toString(); } - $indicatorSpec .= '{'.implode('|', $subSpec).'}'; + $indicatorSpec .= '{' . implode('|', $subSpec) . '}'; } else { - $indicatorSpec .= '{'.$subSpec->__toString().'}'; + $indicatorSpec .= '{' . $subSpec->__toString() . '}'; } } } @@ -135,14 +139,17 @@ public function __toString() * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength * |indicator1|indicator2|subSpecs */ - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { switch ($offset) { - case 'position': return isset($this->position); - break; - case 'subSpecs': return (0 < count($this->subSpecs)) ? true : false; - break; - default: return false; + case 'position': + return isset($this->position); + break; + case 'subSpecs': + return (0 < count($this->subSpecs)) ? true : false; + break; + default: + return false; } } @@ -154,14 +161,17 @@ public function offsetExists($offset) * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength * |indicator1|indicator2|subSpecs */ - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { switch ($offset) { - case 'position': return $this->getPos(); - break; - case 'subSpecs': return $this->getSubSpecs(); - break; - default: throw new \UnexpectedValueException("Offset $offset does not exist."); + case 'position': + return $this->getPos(); + break; + case 'subSpecs': + return $this->getSubSpecs(); + break; + default: + throw new \UnexpectedValueException("Offset $offset does not exist."); } } @@ -173,19 +183,21 @@ public function offsetGet($offset) * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength * |indicator1|indicator2|subSpecs */ - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { switch ($offset) { - case 'subSpecs': $this->addSubSpec($value); - break; - default: throw new \UnexpectedValueException("Offset $offset cannot be set."); + case 'subSpecs': + $this->addSubSpec($value); + break; + default: + throw new \UnexpectedValueException("Offset $offset cannot be set."); } } /** * Access object like an associative array. */ - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { throw new \BadMethodCallException("Offset $offset can not be unset."); } @@ -193,7 +205,7 @@ public function offsetUnset($offset) /** * {@inheritdoc} */ - public function jsonSerialize() + public function jsonSerialize(): mixed { $_indicatorSpec['position'] = $this->getPos(); diff --git a/src/MARCspec.php b/src/MARCspec.php index 546daf3..8e97cb8 100644 --- a/src/MARCspec.php +++ b/src/MARCspec.php @@ -1,4 +1,5 @@ $specLength) { throw new InvalidMARCspecException( - InvalidMARCspecException::MS. - InvalidMARCspecException::LENGTH. - InvalidMARCspecException::MINIMUM3, + InvalidMARCspecException::MS . + InvalidMARCspecException::LENGTH . + InvalidMARCspecException::MINIMUM3, $spec ); } if (preg_match('/\s/', $spec)) { throw new InvalidMARCspecException( - InvalidMARCspecException::MS. - InvalidMARCspecException::SPACE, + InvalidMARCspecException::MS . + InvalidMARCspecException::SPACE, $spec ); } @@ -149,7 +150,7 @@ 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) ); } @@ -181,16 +182,16 @@ public function setIndicator(IndicatorInterface $indicator) { if (!is_null($this->getSubfields())) { throw new InvalidMARCspecException( - InvalidMARCspecException::MS. - InvalidMARCspecException::SFEX, + InvalidMARCspecException::MS . + InvalidMARCspecException::SFEX, $this->__toString() ); } if (!is_null($this->field->getCharStart())) { throw new InvalidMARCspecException( - InvalidMARCspecException::MS. - InvalidMARCspecException::CSEX, + InvalidMARCspecException::MS . + InvalidMARCspecException::CSEX, $this->__toString() ); } @@ -259,16 +260,16 @@ public function addSubfields($subfields) { if (!is_null($this->getIndicator())) { throw new InvalidMARCspecException( - InvalidMARCspecException::MS. - InvalidMARCspecException::INEX, + InvalidMARCspecException::MS . + InvalidMARCspecException::INEX, $this->__toString() ); } if (!is_null($this->field->getCharStart())) { throw new InvalidMARCspecException( - InvalidMARCspecException::MS. - InvalidMARCspecException::CSEX, + InvalidMARCspecException::MS . + InvalidMARCspecException::CSEX, $this->__toString() ); } @@ -279,16 +280,16 @@ public function addSubfields($subfields) $this->checkIfString($subfields); if (2 > strlen($subfields)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::LENGTH. - InvalidSubfieldspecException::MINIMUM2, + InvalidMARCspecException::SF . + InvalidMARCspecException::LENGTH . + InvalidSubfieldspecException::MINIMUM2, $subfields ); } if ('$' !== $subfields[0]) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidSubfieldspecException::PREFIX, + InvalidMARCspecException::SF . + InvalidSubfieldspecException::PREFIX, $subfields ); } @@ -318,37 +319,37 @@ private function handleSubfieldRanges($arg) { if (strlen($arg) < 3) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::LENGTH3, + InvalidMARCspecException::SF . + InvalidMARCspecException::LENGTH3, $arg ); } if (preg_match('/[a-z]/', $arg[0]) && !preg_match('/[a-z]/', $arg[2])) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::RANGE, + InvalidMARCspecException::SF . + InvalidMARCspecException::RANGE, $arg ); } if (preg_match('/[A-Z]/', $arg[0]) && !preg_match('/[A-Z]/', $arg[2])) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::RANGE, + InvalidMARCspecException::SF . + InvalidMARCspecException::RANGE, $arg ); } if (preg_match('/[0-9]/', $arg[0]) && !preg_match('/[0-9]/', $arg[2])) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::RANGE, + InvalidMARCspecException::SF . + InvalidMARCspecException::RANGE, $arg ); } if ($arg[0] > $arg[2]) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::RANGE, + InvalidMARCspecException::SF . + InvalidMARCspecException::RANGE, $arg ); } @@ -368,8 +369,8 @@ 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.'); } } @@ -390,40 +391,40 @@ public static function validatePos($pos) if (1 > $posLength) { throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR1, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR1, $pos ); } if (preg_match('/[^0-9\-#]/', $pos)) { // alphabetic characters etc. are not valid throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR2, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR2, $pos ); } if (strpos($pos, '-') === $posLength - 1) { // something like 123- is not valid throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR3, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR3, $pos ); } if (0 === strpos($pos, '-')) { // something like -123 ist not valid throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR4, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR4, $pos ); } if (strpos($pos, '-') !== strrpos($pos, '-')) { // only one - is allowed throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR5, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR5, $pos ); } @@ -432,8 +433,8 @@ public static function validatePos($pos) if (2 < count($_pos)) { throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR6, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR6, $pos ); } @@ -462,7 +463,7 @@ private function createSubSpec($_subTerms, $Subfield = null) $fieldContext = $this->field->getBaseSpec(); $context = $fieldContext; if (!is_null($Subfield)) { - $context = $fieldContext.$Subfield->getBaseSpec(); + $context = $fieldContext . $Subfield->getBaseSpec(); $subfieldContext = $Subfield->getBaseSpec(); } @@ -472,8 +473,8 @@ private function createSubSpec($_subTerms, $Subfield = null) } else { if (strpos('[/_$', $subTerm[0]) && is_null($context)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SS. - InvalidMARCspecException::MISSINGFIELD, + InvalidMARCspecException::SS . + InvalidMARCspecException::MISSINGFIELD, $subTerm ); } @@ -482,35 +483,35 @@ private function createSubSpec($_subTerms, $Subfield = null) case '_': if ($refPos = strpos($context, $subTerm[0])) { if ('$' !== substr($context, $refPos - 1, 1)) { - return new MARCspec(substr($context, 0, $refPos).$subTerm); + return new MARCspec(substr($context, 0, $refPos) . $subTerm); } } - return new MARCspec($fieldContext.$subTerm); + return new MARCspec($fieldContext . $subTerm); case '$': - return new MARCspec($fieldContext.$subTerm); + return new MARCspec($fieldContext . $subTerm); case '/': $refPos = strrpos($context, $subTerm[0]); if ($refPos) { if ('$' !== substr($context, $refPos - 1, 1)) { - return new MARCspec(substr($context, 0, $refPos).$subTerm); + return new MARCspec(substr($context, 0, $refPos) . $subTerm); } } - return new MARCspec($context.$subTerm); + return new MARCspec($context . $subTerm); case '[': $refPos = strrpos($context, $subTerm[0]); if ($refPos) { if ('$' !== substr($context, $refPos - 1, 1)) { - return new MARCspec(substr($context, 0, $refPos).$subTerm); + return new MARCspec(substr($context, 0, $refPos) . $subTerm); } else { - return new MARCspec($context.$subTerm); + return new MARCspec($context . $subTerm); } } else { - return new MARCspec(substr($context, 0, 3).$subTerm); + return new MARCspec(substr($context, 0, 3) . $subTerm); } default: @@ -535,7 +536,7 @@ private function createSubSpec($_subTerms, $Subfield = null) /** * {@inheritdoc} */ - public function jsonSerialize() + public function jsonSerialize(): mixed { $_marcSpec['field'] = $this->field->jsonSerialize(); @@ -574,16 +575,18 @@ public function __toString() * * @param string $offset Key field|subfield */ - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { switch ($offset) { case 'field': return isset($this->field); - break; - case 'indicator': return isset($this->indicator); - break; - case 'subfields': return (0 < count($this->subfields)) ? true : false; - break; + break; + case 'indicator': + return isset($this->indicator); + break; + case 'subfields': + return (0 < count($this->subfields)) ? true : false; + break; default: return false; } @@ -596,16 +599,18 @@ public function offsetExists($offset) * * @param string $offset Key field|subfield */ - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { switch ($offset) { case 'field': return $this->getField(); - break; - case 'indicator': return $this->getIndicator(); - break; - case 'subfields': return $this->getSubfields(); - break; + break; + case 'indicator': + return $this->getIndicator(); + break; + case 'subfields': + return $this->getSubfields(); + break; default: return $this->getSubfield($offset); } @@ -618,14 +623,17 @@ public function offsetGet($offset) * * @param string $offset Key subfield */ - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { switch ($offset) { - case 'indicator': $this->addIndicator($value); - break; - case 'subfields': $this->addSubfields($value); - break; - default: throw new \UnexpectedValueException("Offset $offset cannot be set."); + case 'indicator': + $this->addIndicator($value); + break; + case 'subfields': + $this->addSubfields($value); + break; + default: + throw new \UnexpectedValueException("Offset $offset cannot be set."); } } @@ -634,12 +642,12 @@ public function offsetSet($offset, $value) * * @param string $offset */ - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { throw new \BadMethodCallException("Offset $offset can not be unset."); } - public function getIterator() + public function getIterator(): \Traversable { return new SpecIterator(['field' => $this->field, 'subfields' => $this->subfields, 'indicator' => $this->indicator]); } diff --git a/src/MARCspecParser.php b/src/MARCspecParser.php index 49fcb60..6dd704b 100755 --- a/src/MARCspecParser.php +++ b/src/MARCspecParser.php @@ -1,4 +1,5 @@ MARCSPEC.'/', $marcspec, $this->parsed, PREG_SET_ORDER)) { + if (!preg_match_all('/' . $this->MARCSPEC . '/', $marcspec, $this->parsed, PREG_SET_ORDER)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::MISSINGFIELD, + InvalidMARCspecException::FS . + InvalidMARCspecException::MISSINGFIELD, $marcspec ); } @@ -137,16 +143,16 @@ public function parse($marcspec) if (!array_key_exists('field', $this->parsed)) { // TODO: check if 'tag' is the required key throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::FTAG, + InvalidMARCspecException::FS . + InvalidMARCspecException::FTAG, $marcspec ); } if (strlen($this->parsed[0]) !== strlen($marcspec)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::USELESS, + InvalidMARCspecException::FS . + InvalidMARCspecException::USELESS, $marcspec ); } @@ -154,16 +160,16 @@ public function parse($marcspec) if (array_key_exists('charpos', $this->parsed)) { if (array_key_exists('indicatorpos', $this->parsed)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::CHARORIND, + InvalidMARCspecException::FS . + InvalidMARCspecException::CHARORIND, $marcspec ); } if (array_key_exists('subfields', $this->parsed)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::CHARORSF, + InvalidMARCspecException::FS . + InvalidMARCspecException::CHARORSF, $marcspec ); } @@ -172,8 +178,8 @@ public function parse($marcspec) if (array_key_exists('subfields', $this->parsed)) { if (array_key_exists('indicatorpos', $this->parsed)) { throw new InvalidMARCspecException( - InvalidMARCspecException::FS. - InvalidMARCspecException::INDORSF, + InvalidMARCspecException::FS . + InvalidMARCspecException::INDORSF, $marcspec ); } @@ -204,10 +210,10 @@ public function parse($marcspec) */ public function parseSubfields($subfieldspec) { - if (!preg_match_all('/'.$this->SUBFIELD.'/', $subfieldspec, $_subfieldMatches, PREG_SET_ORDER)) { + if (!preg_match_all('/' . $this->SUBFIELD . '/', $subfieldspec, $_subfieldMatches, PREG_SET_ORDER)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::SFCHAR, + InvalidMARCspecException::SF . + InvalidMARCspecException::SFCHAR, $subfieldspec ); } @@ -250,8 +256,8 @@ function (&$_subfield) use (&$test) { if ($test !== $subfieldspec) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::USELESS, + InvalidMARCspecException::SF . + InvalidMARCspecException::USELESS, $subfieldspec ); } @@ -270,24 +276,24 @@ public function subfieldToArray($subfieldspec) { if (!$_sf = $this->parseSubfields($subfieldspec)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::UNKNOWN, + InvalidMARCspecException::SF . + InvalidMARCspecException::UNKNOWN, $subfieldspec ); } if (1 < count($_sf)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::MULTISF, + InvalidMARCspecException::SF . + InvalidMARCspecException::MULTISF, $subfieldspec ); } if ($_sf[0]['subfield'] !== $subfieldspec) { throw new InvalidMARCspecException( - InvalidMARCspecException::SF. - InvalidMARCspecException::USELESS, + InvalidMARCspecException::SF . + InvalidMARCspecException::USELESS, $subfieldspec ); } @@ -305,10 +311,10 @@ public function subfieldToArray($subfieldspec) private function matchSubSpecs($subSpecs) { $_subSpecs = []; - if (!preg_match_all('/'.$this->SUBSPEC.'/U', $subSpecs, $_subSpecMatches, PREG_SET_ORDER)) { + if (!preg_match_all('/' . $this->SUBSPEC . '/U', $subSpecs, $_subSpecMatches, PREG_SET_ORDER)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SS. - InvalidMARCspecException::UNKNOWN, + InvalidMARCspecException::SS . + InvalidMARCspecException::UNKNOWN, $subSpecs ); } @@ -318,8 +324,8 @@ private function matchSubSpecs($subSpecs) $_subSpecs[$key] = preg_split('/(?SUBTERMS.'/', $subSpec, $_subTermMatches, PREG_SET_ORDER)) { + if (preg_match_all('/' . $this->SUBTERMS . '/', $subSpec, $_subTermMatches, PREG_SET_ORDER)) { if (empty($_subTermMatches[0]['operator'])) { $_subTermMatches[0]['operator'] = '?'; } if (!$_subTermMatches[0]['rightsubterm']) { throw new InvalidMARCspecException( - InvalidMARCspecException::SS. - InvalidMARCspecException::MISSINGRIGHT, + InvalidMARCspecException::SS . + InvalidMARCspecException::MISSINGRIGHT, $subSpec ); } @@ -360,8 +366,8 @@ private function matchSubTerms($subSpec) return array_filter($_subTermMatches[0], 'strlen'); } else { throw new InvalidMARCspecException( - InvalidMARCspecException::SS. - InvalidMARCspecException::UNKNOWN, + InvalidMARCspecException::SS . + InvalidMARCspecException::UNKNOWN, $subSpec ); } @@ -374,19 +380,19 @@ private function setConstants() { $this->FIELDTAG = '(?(?:[a-z0-9\.]{3,3}|[A-Z0-9\.]{3,3}|[0-9\.]{3,3}))'; $this->POSITIONORRANGE = '(?:(?:(?:[0-9]+|#)\-(?:[0-9]+|#))|(?:[0-9]+|#))'; - $this->INDEX = '(?:\[(?'.$this->POSITIONORRANGE.')\])?'; - $this->CHARPOS = '(?:\/(?'.$this->POSITIONORRANGE.'))?'; + $this->INDEX = '(?:\[(?' . $this->POSITIONORRANGE . ')\])?'; + $this->CHARPOS = '(?:\/(?' . $this->POSITIONORRANGE . '))?'; $this->INDICATORPOS = '(?:\^(?[12]))'; $this->SUBSPECS = '(?(?:\{.+?(?SUBFIELDS = '(?\$.+)'; - $this->FIELD = '(?'.$this->FIELDTAG.$this->INDEX.')'; - $this->MARCSPEC = '^'.$this->FIELD.'(?:'.$this->SUBFIELDS.'|(?:'.$this->INDICATORPOS.'|'.$this->CHARPOS.')'.$this->SUBSPECS.')$'; + $this->FIELD = '(?' . $this->FIELDTAG . $this->INDEX . ')'; + $this->MARCSPEC = '^' . $this->FIELD . '(?:' . $this->SUBFIELDS . '|(?:' . $this->INDICATORPOS . '|' . $this->CHARPOS . ')' . $this->SUBSPECS . ')$'; $this->SUBFIELDTAGRANGE = '(?(?:[0-9a-z]\-[0-9a-z]))'; $this->SUBFIELDTAG = '(?[\!-\?\[-\{\}-~])'; - $this->SUBFIELD = '(?\$(?:'.$this->SUBFIELDTAGRANGE.'|'.$this->SUBFIELDTAG.')'.$this->INDEX.$this->CHARPOS.$this->SUBSPECS.')'; + $this->SUBFIELD = '(?\$(?:' . $this->SUBFIELDTAGRANGE . '|' . $this->SUBFIELDTAG . ')' . $this->INDEX . $this->CHARPOS . $this->SUBSPECS . ')'; $this->LEFTSUBTERM = '^(?(?:\\\(?:(?<=\\\)[\!\=\~\?]|[^\!\=\~\?])+)|(?:(?<=\$)[\!\=\~\?]|[^\!\=\~\?])+)?'; $this->OPERATOR = '(?\!\=|\!\~|\=|\~|\!|\?)'; - $this->SUBTERMS = '(?:'.$this->LEFTSUBTERM.$this->OPERATOR.')?(?.+)$'; + $this->SUBTERMS = '(?:' . $this->LEFTSUBTERM . $this->OPERATOR . ')?(?.+)$'; $this->SUBSPEC = '(?:\{(.+)\})'; } } diff --git a/src/PositionOrRange.php b/src/PositionOrRange.php index 5dc3f75..4fd53c8 100755 --- a/src/PositionOrRange.php +++ b/src/PositionOrRange.php @@ -1,4 +1,5 @@ $length) { throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::NEGATIVE + InvalidMARCspecException::PR . + InvalidMARCspecException::NEGATIVE ); } @@ -174,13 +175,15 @@ private function validateStartEnd($start, $end) $_startEnd[0] = '#'; } else { throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR7, + InvalidMARCspecException::PR . + InvalidMARCspecException::PR7, $start ); } - if (preg_match('/[0-9#]/', $end)) { + if (null === $end) { + $_startEnd[1] = null; + } elseif (preg_match('/[0-9#]/', $end)) { if ('#' === $end) { $_startEnd[1] = '#'; } elseif (preg_match('/[0-9]/', $end)) { @@ -188,16 +191,16 @@ private function validateStartEnd($start, $end) if ($_startEnd[1] < $_startEnd[0]) { throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR8, - $start.'-'.$end + InvalidMARCspecException::PR . + InvalidMARCspecException::PR8, + $start . '-' . $end ); } } else { throw new InvalidMARCspecException( - InvalidMARCspecException::PR. - InvalidMARCspecException::PR8, - $start.'-'.$end + InvalidMARCspecException::PR . + InvalidMARCspecException::PR8, + $start . '-' . $end ); } } else { @@ -258,7 +261,7 @@ protected function checkIfString($arg) { if (!is_string($arg)) { throw new \InvalidArgumentException('Method only accepts string as argument. ' - .gettype($arg).' given.'); + . gettype($arg) . ' given.'); } } } // EOC diff --git a/src/SpecIterator.php b/src/SpecIterator.php index a185fad..a423fe0 100755 --- a/src/SpecIterator.php +++ b/src/SpecIterator.php @@ -1,4 +1,5 @@ var); } - public function current() + public function current(): mixed { - $var = current($this->var); - - return $var; + return current($this->var); } - public function key() + public function key(): mixed { - $var = key($this->var); - - return $var; + return key($this->var); } - public function next() + public function next(): void { - $var = next($this->var); - - return $var; + next($this->var); } - public function valid() + public function valid(): bool { - $var = $this->current() !== false; - - return $var; + return key($this->var) !== null; } } diff --git a/src/SubSpec.php b/src/SubSpec.php index 047b96f..612f49c 100755 --- a/src/SubSpec.php +++ b/src/SubSpec.php @@ -1,4 +1,5 @@ leftSubTerm = $leftSubTerm; } else { throw new \InvalidArgumentException( @@ -50,14 +53,16 @@ public function __construct($leftSubTerm, $operator, $rightSubTerm) ); } - if ($rightSubTerm instanceof MARCspecInterface - || $rightSubTerm instanceof ComparisonStringInterface) { + if ( + $rightSubTerm instanceof MARCspecInterface + || $rightSubTerm instanceof ComparisonStringInterface + ) { $this->rightSubTerm = $rightSubTerm; } else { throw new \InvalidArgumentException( 'Argument 3 must be instance of CK\MARCspec\MARCspecInterface or CK\MARCspec\ComparisonStringInterface. Got ' - .gettype($rightSubTerm) + . gettype($rightSubTerm) ); } @@ -73,8 +78,8 @@ private function setOperator($operator) { if (!in_array($operator, ['=', '!=', '~', '!~', '!', '?'], true)) { throw new InvalidMARCspecException( - InvalidMARCspecException::SS. - InvalidMARCspecException::OPERATOR, + InvalidMARCspecException::SS . + InvalidMARCspecException::OPERATOR, $operator ); } @@ -112,7 +117,7 @@ public function __toString() { switch ($this->operator) { case '!': - $subSpecString = $this->operator."$this->rightSubTerm"; + $subSpecString = $this->operator . "$this->rightSubTerm"; break; case '?': @@ -120,7 +125,7 @@ public function __toString() break; default: - $subSpecString = "$this->leftSubTerm".$this->operator."$this->rightSubTerm"; + $subSpecString = "$this->leftSubTerm" . $this->operator . "$this->rightSubTerm"; } return $subSpecString; @@ -129,7 +134,7 @@ public function __toString() /** * {@inheritdoc} */ - public function jsonSerialize() + public function jsonSerialize(): mixed { if (!is_null($this->leftSubTerm)) { $_subSpec['leftSubTerm'] = $this->leftSubTerm->jsonSerialize(); @@ -147,7 +152,7 @@ public function jsonSerialize() * * @param string $offset Key operator|leftSubTerm|rightSubTerm */ - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { switch ($offset) { case 'operator': @@ -168,7 +173,7 @@ public function offsetExists($offset) * * @param string $offset Key operator|leftSubTerm|rightSubTerm */ - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { switch ($offset) { case 'operator': @@ -195,7 +200,7 @@ public function offsetGet($offset) * * @param string $offset */ - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { throw new \UnexpectedValueException("Offset $offset cannot be set."); } @@ -205,12 +210,12 @@ public function offsetSet($offset, $value) * * @param string $offset */ - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { throw new \BadMethodCallException("Offset $offset can not be unset."); } - public function getIterator() + public function getIterator(): \Traversable { return new SpecIterator( [ diff --git a/src/Subfield.php b/src/Subfield.php index 43934a1..4f2d449 100755 --- a/src/Subfield.php +++ b/src/Subfield.php @@ -1,4 +1,5 @@ getTag(); @@ -211,16 +213,16 @@ public function jsonSerialize() */ public function getBaseSpec() { - $subfieldSpec = '$'.$this->getTag(); + $subfieldSpec = '$' . $this->getTag(); $indexStart = $this->getIndexStart(); $indexEnd = $this->getIndexEnd(); if ($indexStart === 0 && $indexEnd === '#') { // use abbreviation } else { - $subfieldSpec .= '['.$indexStart; + $subfieldSpec .= '[' . $indexStart; if ($indexStart !== $indexEnd) { - $subfieldSpec .= '-'.$indexEnd; + $subfieldSpec .= '-' . $indexEnd; } $subfieldSpec .= ']'; } @@ -230,9 +232,9 @@ public function getBaseSpec() if ($charStart === 0 && $charEnd === '#') { // use abbreviation } else { - $subfieldSpec .= '/'.$charStart; + $subfieldSpec .= '/' . $charStart; if ($charEnd !== $charStart) { - $subfieldSpec .= '-'.$charEnd; + $subfieldSpec .= '-' . $charEnd; } } } @@ -253,9 +255,9 @@ public function __toString() foreach ($subSpec as $orKey => $orSubSpec) { $subSpec[$orKey] = $orSubSpec->__toString(); } - $subfieldSpec .= '{'.implode('|', $subSpec).'}'; + $subfieldSpec .= '{' . implode('|', $subSpec) . '}'; } else { - $subfieldSpec .= '{'.$subSpec->__toString().'}'; + $subfieldSpec .= '{' . $subSpec->__toString() . '}'; } } } @@ -270,34 +272,35 @@ public function __toString() * * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength|subSpecs */ - public function offsetExists($offset) + public function offsetExists(mixed $offset): bool { switch ($offset) { case 'code': - case 'tag': return isset($this->tag); - break; + case 'tag': + return isset($this->tag); + break; case 'indexStart': return isset($this->indexStart); - break; + break; case 'indexEnd': return isset($this->indexEnd); - break; + break; case 'indexLength': return !is_null($this->getIndexLength()); - break; + break; case 'charStart': return isset($this->charStart); - break; + break; case 'charEnd': return isset($this->charEnd); - break; + break; case 'charLength': return !is_null($this->getCharLength()); - break; + break; case 'subSpecs': return (0 < count($this->subSpecs)) ? true : false; - break; + break; default: return false; } @@ -310,35 +313,36 @@ public function offsetExists($offset) * * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength|subSpecs */ - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { switch ($offset) { case 'tag': - case 'code': return $this->getTag(); - break; + case 'code': + return $this->getTag(); + break; case 'indexStart': return $this->getIndexStart(); - break; + break; case 'indexEnd': return $this->getIndexEnd(); - break; + break; case 'indexLength': return $this->getIndexLength(); - break; + break; case 'charStart': return $this->getCharStart(); - break; + break; case 'charEnd': return $this->getCharEnd(); - break; + break; case 'charLength': return $this->getCharLength(); - break; + break; case 'subSpecs': return $this->getSubSpecs(); - break; + break; default: - return; + return null; } } @@ -349,12 +353,13 @@ public function offsetGet($offset) * * @param string $offset Key indexStart|indexEnd|charStart|charEnd|charLength|subSpecs */ - public function offsetSet($offset, $value) + public function offsetSet(mixed $offset, mixed $value): void { switch ($offset) { - case 'indexStart': $this->setIndexStartEnd($value); - break; + case 'indexStart': + $this->setIndexStartEnd($value); + break; case 'indexEnd': if (!isset($this['indexStart'])) { @@ -366,7 +371,7 @@ public function offsetSet($offset, $value) case 'indexLength': throw new \UnexpectedValueException('indexLength is always calculated.'); - break; + break; case 'charStart': $this->setCharStartEnd($value); @@ -382,7 +387,7 @@ public function offsetSet($offset, $value) case 'charLength': throw new \UnexpectedValueException('CharLength is always calculated.'); - break; + break; case 'subSpecs': $this->addSubSpec($value); @@ -396,12 +401,12 @@ public function offsetSet($offset, $value) /** * Access object like an associative array. */ - public function offsetUnset($offset) + public function offsetUnset(mixed $offset): void { throw new \BadMethodCallException("Offset $offset can not be unset."); } - public function getIterator() + public function getIterator(): \Traversable { foreach ($this as $key => $value) { $_[$key] = $value;