Skip to content

Commit 1fac5c0

Browse files
committed
Update for PHP 8 and add more types
1 parent 6850aed commit 1fac5c0

28 files changed

+235
-210
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=5.6",
14+
"php": ">=8.0",
1515
"ext-xml": "*",
1616
"ext-json": "*",
17+
"ext-simplexml": "*",
1718
"ck/file_marc_reference": "^1.2",
1819
"pear/file_marc": "^1.4.1"
1920
},
2021
"require-dev": {
21-
"phpunit/phpunit": "^5.7 | ^6.0 | ^7.0",
22+
"phpunit/phpunit": "^8.0 | ^9.0",
2223
"squizlabs/php_codesniffer": "^3.3"
2324
},
2425
"autoload": {

phpunit.xml

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<phpunit bootstrap="vendor/autoload.php" colors="true" stopOnError="true" stopOnFailure="false">
2-
<testsuites>
3-
<testsuite name="TestSuite">
4-
<directory>tests/</directory>
5-
</testsuite>
6-
</testsuites>
7-
<filter>
8-
<whitelist processUncoveredFilesFromWhitelist="true">
9-
<directory suffix=".php">src/</directory>
10-
</whitelist>
11-
</filter>
2+
<testsuites>
3+
<testsuite name="unit">
4+
<directory>tests/</directory>
5+
</testsuite>
6+
</testsuites>
7+
<coverage ignoreDeprecatedCodeUnits="true">
8+
<include>
9+
<directory suffix=".php">src</directory>
10+
</include>
11+
<report>
12+
<clover outputFile="clover.xml" />
13+
</report>
14+
</coverage>
1215
</phpunit>

src/BibliographicRecord.php

+9-14
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
use Scriptotek\Marc\Fields\Person;
1010
use Scriptotek\Marc\Fields\Publisher;
1111
use Scriptotek\Marc\Fields\Subject;
12-
use Scriptotek\Marc\Fields\SubjectInterface;
12+
use Scriptotek\Marc\Fields\SubjectFieldInterface;
1313
use Scriptotek\Marc\Fields\Title;
1414

1515
class BibliographicRecord extends Record
1616
{
1717
/**
18-
* @var array List of properties to be included when serializing the record using the `toArray()` method.
18+
* @var string[] List of properties to be included when serializing the record using the `toArray()` method.
1919
*/
20-
public $properties = [
20+
public array $properties = [
2121
'id', 'isbns', 'title', 'publisher', 'pub_year', 'edition', 'creators',
2222
'subjects', 'classifications', 'toc', 'summary', 'part_of'
2323
];
@@ -28,14 +28,9 @@ class BibliographicRecord extends Record
2828
* Marc21::ISBD_PUNCTUATION_INCLUDED, Marc21::NON_ISBD_PUNCTUATION_OMITTED
2929
* or Marc21::UNKNOWN_CATALOGING_FORM.
3030
*
31-
* @property Isbn[] isbns
32-
* @property string title
33-
* @property SubjectInterface[] subjects
34-
*
3531
* @return string
36-
* @throws UnknownRecordType
3732
*/
38-
public function getCatalogingForm()
33+
public function getCatalogingForm(): string
3934
{
4035
$leader = $this->record->getLeader();
4136
return substr($leader, 18, 1);
@@ -51,7 +46,7 @@ public function getCatalogingForm()
5146
*
5247
* @return Isbn[]
5348
*/
54-
public function getIsbns()
49+
public function getIsbns(): array
5550
{
5651
return Isbn::get($this);
5752
}
@@ -61,7 +56,7 @@ public function getIsbns()
6156
*
6257
* @return Title
6358
*/
64-
public function getTitle()
59+
public function getTitle(): Title
6560
{
6661
return Title::get($this);
6762
}
@@ -142,18 +137,18 @@ public function getSummary()
142137
}
143138

144139
/**
145-
* Get an array of the 6XX fields as `SubjectInterface` objects, optionally
140+
* Get an array of the 6XX fields as `SubjectFieldInterface` objects, optionally
146141
* filtered by vocabulary and/or tag.
147142
*
148143
* @param string $vocabulary
149144
* @param string|string[] $tag
150-
* @return SubjectInterface[]
145+
* @return SubjectFieldInterface[]
151146
*/
152147
public function getSubjects($vocabulary = null, $tag = null)
153148
{
154149
$tag = is_null($tag) ? [] : (is_array($tag) ? $tag : [$tag]);
155150

156-
return array_values(array_filter(Subject::get($this), function (SubjectInterface $subject) use ($vocabulary, $tag) {
151+
return array_values(array_filter(Subject::get($this), function (SubjectFieldInterface $subject) use ($vocabulary, $tag) {
157152
$a = is_null($vocabulary) || $vocabulary == $subject->getVocabulary();
158153
$b = empty($tag) || in_array($subject->getType(), $tag);
159154

src/Collection.php

+30-32
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Scriptotek\Marc;
44

5+
use File_MARC;
56
use File_MARC_Record;
7+
use File_MARCXML;
68
use Scriptotek\Marc\Exceptions\RecordNotFound;
79
use Scriptotek\Marc\Exceptions\UnknownRecordType;
810
use Scriptotek\Marc\Importers\Importer;
@@ -11,18 +13,18 @@
1113

1214
class Collection implements \Iterator
1315
{
14-
protected $parser;
15-
protected $_records;
16-
protected $useCache = false;
17-
protected $position = 0;
18-
protected $_current;
16+
protected File_MARCXML|File_MARC|null $parser;
17+
protected ?array $_records = null;
18+
protected bool $useCache = false;
19+
protected int $position = 0;
20+
protected Record|HoldingsRecord|BibliographicRecord|AuthorityRecord|null $_current = null;
1921

2022
/**
2123
* Collection constructor.
2224
*
23-
* @param \File_MARC|\File_MARCXML $parser
25+
* @param File_MARCXML|File_MARC|null $parser
2426
*/
25-
public function __construct($parser = null)
27+
public function __construct(File_MARCXML|File_MARC $parser = null)
2628
{
2729
$this->parser = $parser;
2830
}
@@ -33,7 +35,7 @@ public function __construct($parser = null)
3335
* @param string $filename
3436
* @return Collection
3537
*/
36-
public static function fromFile($filename)
38+
public static function fromFile(string $filename): Collection
3739
{
3840
$importer = new Importer();
3941

@@ -46,7 +48,7 @@ public static function fromFile($filename)
4648
* @param string $data
4749
* @return Collection
4850
*/
49-
public static function fromString($data)
51+
public static function fromString(string $data): Collection
5052
{
5153
$importer = new Importer();
5254

@@ -59,7 +61,7 @@ public static function fromString($data)
5961
* @param SimpleXMLElement $element
6062
* @return Collection
6163
*/
62-
public static function fromSimpleXMLElement(SimpleXMLElement $element)
64+
public static function fromSimpleXMLElement(SimpleXMLElement $element): Collection
6365
{
6466
$importer = new XmlImporter($element);
6567

@@ -72,7 +74,7 @@ public static function fromSimpleXMLElement(SimpleXMLElement $element)
7274
* @param File_MARC_Record $record
7375
* @return string
7476
*/
75-
public static function getRecordType(File_MARC_Record $record)
77+
public static function getRecordType(File_MARC_Record $record): string
7678
{
7779
$leader = $record->getLeader();
7880
$recordType = substr($leader, 6, 1);
@@ -110,18 +112,18 @@ public static function getRecordType(File_MARC_Record $record)
110112
*
111113
* @return Collection[]
112114
*/
113-
public function toArray()
115+
public function toArray(): array
114116
{
115117
return iterator_to_array($this);
116118
}
117119

118120
/**
119121
* Return the first record in the collection.
120122
*
121-
* @return BibliographicRecord|HoldingsRecord|AuthorityRecord
123+
* @return Record|HoldingsRecord|BibliographicRecord|AuthorityRecord|null
122124
* @throws RecordNotFound if the collection is empty
123125
*/
124-
public function first()
126+
public function first(): Record|HoldingsRecord|BibliographicRecord|AuthorityRecord|null
125127
{
126128
$this->rewind();
127129
if (is_null($this->current())) {
@@ -134,51 +136,47 @@ public function first()
134136
* Creates a Record object from a File_MARC_Record object.
135137
*
136138
* @param File_MARC_Record $record
137-
* @return AuthorityRecord|BibliographicRecord|HoldingsRecord
139+
* @return Record|HoldingsRecord|BibliographicRecord|AuthorityRecord|null
138140
*/
139-
public function recordFactory(File_MARC_Record $record)
141+
public function recordFactory(File_MARC_Record $record): Record|HoldingsRecord|BibliographicRecord|AuthorityRecord|null
140142
{
141143
try {
142144
$recordType = self::getRecordType($record);
143145
} catch (UnknownRecordType $e) {
144146
return new Record($record);
145147
}
146-
switch ($recordType) {
147-
case Marc21::BIBLIOGRAPHIC:
148-
return new BibliographicRecord($record);
149-
150-
case Marc21::HOLDINGS:
151-
return new HoldingsRecord($record);
152-
153-
case Marc21::AUTHORITY:
154-
return new AuthorityRecord($record);
155-
}
148+
return match ($recordType) {
149+
Marc21::BIBLIOGRAPHIC => new BibliographicRecord($record),
150+
Marc21::HOLDINGS => new HoldingsRecord($record),
151+
Marc21::AUTHORITY => new AuthorityRecord($record),
152+
default => null,
153+
};
156154
}
157155

158156
/*********************************************************
159157
* Iterator
160158
*********************************************************/
161159

162-
public function valid()
160+
public function valid(): bool
163161
{
164162
return !is_null($this->_current);
165163
}
166164

167-
public function current()
165+
public function current(): Record|HoldingsRecord|BibliographicRecord|AuthorityRecord|null
168166
{
169167
return $this->_current;
170168
}
171169

172-
public function key()
170+
public function key(): int
173171
{
174172
return $this->position;
175173
}
176174

177-
public function next()
175+
public function next(): void
178176
{
179177
++$this->position;
180178
if ($this->useCache) {
181-
$rec = isset($this->_records[$this->position]) ? $this->_records[$this->position] : false;
179+
$rec = $this->_records[$this->position] ?? false;
182180
} else {
183181
$rec = isset($this->parser) ? $this->parser->next() : null;
184182
if ($rec) {
@@ -189,7 +187,7 @@ public function next()
189187
$this->_current = $rec ?: null;
190188
}
191189

192-
public function rewind()
190+
public function rewind(): void
193191
{
194192
$this->position = -1;
195193
if (is_null($this->_records)) {

src/Factory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
class Factory
66
{
7-
protected function genMake($className, $args)
7+
protected function genMake($className, $args): mixed
88
{
99
$reflectionClass = new \ReflectionClass($className);
1010
$instance = $reflectionClass->newInstanceArgs($args);
1111

1212
return $instance;
1313
}
1414

15-
public function make()
15+
public function make(): mixed
1616
{
1717
$args = func_get_args();
1818
$className = array_shift($args);

src/Fields/Classification.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Classification extends Subfield implements \JsonSerializable
1717
* @var array List of properties to be included when serializing the record using the `toArray()` method.
1818
*/
1919

20-
public $properties = ['scheme', 'number', 'heading', 'edition', 'assigning_agency', 'id'];
20+
public array $properties = ['scheme', 'number', 'heading', 'edition', 'assigning_agency', 'id'];
2121

22-
public static function get(Record $record)
22+
public static function get(Record $record): array
2323
{
2424
$out = [];
2525
foreach ($record->getFields('08[0234]', true) as $field) {
@@ -30,17 +30,17 @@ public static function get(Record $record)
3030
return $out;
3131
}
3232

33-
public function getType()
33+
public function getType(): string
3434
{
3535
return $this->getTag();
3636
}
3737

38-
public function getTag()
38+
public function getTag(): string
3939
{
4040
return $this->field->getTag();
4141
}
4242

43-
public function getScheme()
43+
public function getScheme(): string
4444
{
4545
$typeMap = [
4646
'080' => 'udc',
@@ -57,31 +57,32 @@ public function getScheme()
5757
return $typeMap[$tag];
5858
}
5959

60-
public function getEdition()
60+
public function getEdition(): ?string
6161
{
6262
if (in_array($this->field->getTag(), ['080', '082', '083'])) {
6363
return $this->field->sf('2');
6464
}
65+
return null;
6566
}
6667

67-
public function getNumber()
68+
public function getNumber(): string
6869
{
6970
return $this->subfield->getData();
7071
}
7172

72-
public function getAssigningVocabulary()
73+
public function getAssigningVocabulary(): ?string
7374
{
7475
return $this->field->sf('q');
7576
}
7677

77-
public function getId()
78+
public function getId(): ?string
7879
{
7980
// NOTE: Both $a and $0 are repeatable, but there's no examples of how that would look like.
8081
// I'm guessing that they would alternate: $a ... $0 ... $a ... $0 ... , but not sure.
8182
return $this->field->sf('0');
8283
}
8384

84-
public function __toString()
85+
public function __toString(): string
8586
{
8687
return $this->getNumber();
8788
}

src/Fields/ControlField.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
class ControlField extends Field implements FieldInterface
88
{
9-
public static function get(Record $record, $tag)
9+
public static function get(Record $record, $tag): static
1010
{
11-
return parent::makeFieldObject($record, $tag);
11+
return static::makeFieldObject($record, $tag);
1212
}
1313

14-
public function __toString()
14+
public function __toString(): string
1515
{
1616
return $this->field->getData();
1717
}

0 commit comments

Comments
 (0)