Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor
.phpunit.result.cache
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
## Fork (redcuillin)

Maintained for PHP 8.4 support. Upstream: [scriptotek/php-marc](https://github.com/scriptotek/php-marc).

### 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"
}
],
"require": {
"scriptotek/marc": "dev-main as 4.999.0"
}
```

-----

## Upstream readme:

[![Coverage](https://img.shields.io/codecov/c/github/scriptotek/php-marc)](https://codecov.io/gh/scriptotek/php-marc)
[![StyleCI](https://github.styleci.io/repos/41363199/shield?branch=main)](https://styleci.io/repos/41363199)
[![Code Climate](https://img.shields.io/codeclimate/maintainability/scriptotek/php-marc)](https://codeclimate.com/github/scriptotek/php-marc)
Expand Down Expand Up @@ -84,6 +108,19 @@ Records can be edited using the editing capabilities of File_MARC
See [an example](https://github.com/scriptotek/php-marc/issues/13#issuecomment-522036879)
to get started.

### Sorting field order (`Record::sortFieldsByTag`)

To reorder the fields inside a single record by MARC tag (so `020` comes before `264`,
for example), call `sortFieldsByTag()` on the record. Any field whose tag is `LDR`
(case-insensitive) is moved to the front; all other fields are sorted by their
three-digit tag (`001`–`999`). When several fields share the same tag, their
relative order is preserved. The leader string from `getLeader()` is not part of
the field list and is left unchanged.

```php
$record->sortFieldsByTag(); // returns $record for chaining
```

## Querying with MARCspec

Use the `Record::query()` method to query a record using the
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "scriptotek/marc",
"type": "library",
"description": "Simple interface to parsing MARC records using File_MARC",
"keywords": ["marc"],
"keywords": [
"marc"
],
"license": "MIT",
"authors": [
{
Expand All @@ -11,7 +13,7 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.4",
"ext-xml": "*",
"ext-json": "*",
"ext-simplexml": "*",
Expand All @@ -31,4 +33,4 @@
"scripts": {
"test": "phpunit"
}
}
}
26 changes: 18 additions & 8 deletions src/BibliographicRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ class BibliographicRecord extends Record
* @var string[] List of properties to be included when serializing the record using the `toArray()` method.
*/
public array $properties = [
'id', 'isbns', 'title', 'publisher', 'pub_year', 'edition', 'creators',
'subjects', 'classifications', 'toc', 'summary', 'part_of'
'id',
'isbns',
'title',
'publisher',
'pub_year',
'edition',
'creators',
'subjects',
'classifications',
'toc',
'summary',
'part_of'
];

/**
Expand Down Expand Up @@ -114,7 +124,7 @@ public function getToc(): ?array
} else {
// Basic
return $field->mapSubFields([
'a' => 'text',
'a' => 'text',
]);
}
}
Expand All @@ -131,8 +141,8 @@ public function getSummary(): array|null
$field = $this->getField('520');
if ($field) {
return $field->mapSubFields([
'a' => 'text',
'c' => 'assigning_source',
'a' => 'text',
'c' => 'assigning_source',
]);
}
return null;
Expand All @@ -146,7 +156,7 @@ public function getSummary(): array|null
* @param string|string[]|null $tag
* @return SubjectInterface[]
*/
public function getSubjects(string $vocabulary = null, array|string $tag = null): array
public function getSubjects(?string $vocabulary = null, array|string|null $tag = null): array
{
$tag = is_null($tag) ? [] : (is_array($tag) ? $tag : [$tag]);

Expand All @@ -165,7 +175,7 @@ public function getSubjects(string $vocabulary = null, array|string $tag = null)
* @param string|null $scheme
* @return Classification[]
*/
public function getClassifications(string $scheme = null): array
public function getClassifications(?string $scheme = null): array
{
return array_values(array_filter(Classification::get($this), function ($classifications) use ($scheme) {
$a = is_null($scheme) || $scheme == $classifications->getScheme();
Expand All @@ -181,7 +191,7 @@ public function getClassifications(string $scheme = null): array
* @param string|string[]|null $tag
* @return Person[]
*/
public function getCreators(array|string $tag = null): array
public function getCreators(array|string|null $tag = null): array
{
$tag = is_null($tag) ? [] : (is_array($tag) ? $tag : [$tag]);

Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Collection implements \Iterator
*
* @param File_MARCXML|File_MARC|null $parser
*/
public function __construct(File_MARCXML|File_MARC $parser = null)
public function __construct(File_MARCXML|File_MARC|null $parser = null)
{
$this->parser = $parser;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public function rewind(): void
* Magic
*********************************************************/

public function __call($name, $arguments)
public function __call(string $name, array $arguments): mixed
{
return call_user_func_array([$this->parser, $name], $arguments);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function getField(): File_MARC_Field
*
* @return mixed
*/
public function __call(string $name, array $args)
public function __call(string $name, array $args): mixed
{
return call_user_func_array([$this->field, $name], $args);
}
Expand All @@ -140,7 +140,7 @@ public function __toString(): string
*
* @return string
*/
protected function clean(string $value = null, array $options = []): string
protected function clean(?string $value = null, array $options = []): string
{
if (is_null($value)) {
return "";
Expand Down Expand Up @@ -223,7 +223,7 @@ public function asLineMarc(string $sep = '$', string $blank = ' '): ?string
$ind2 = $blank;
}

return "${tag} ${ind1}${ind2} " . implode(' ', $subfields);
return "{$tag} {$ind1}{$ind2} " . implode(' ', $subfields);
}

/**
Expand All @@ -235,7 +235,7 @@ public function asLineMarc(string $sep = '$', string $blank = ' '): ?string
* The fallback value to return if the subfield does not exist.
* @return string|null
*/
public function sf(string $code, string $default = null): ?string
public function sf(string $code, ?string $default = null): ?string
{
// In PHP, ("a" == 0) will evaluate to TRUE, so it's actually very important that we ensure type here!
$code = (string) $code;
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/SerializableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

trait SerializableField
{
public function jsonSerialize(): array|string
public function jsonSerialize(): mixed
{
if (count($this->properties)) {
$o = [];
Expand Down
6 changes: 3 additions & 3 deletions src/Fields/Subfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function delete()
$this->__destruct();
}

public function jsonSerialize(): string|array
public function jsonSerialize(): mixed
{
return (string) $this;
}
Expand All @@ -38,12 +38,12 @@ public function __toString(): string
return $this->subfield->getData();
}

public function __call($name, $args)
public function __call(string $name, array $args): mixed
{
return call_user_func_array([$this->subfield, $name], $args);
}

public function __get($key)
public function __get(string $key): mixed
{
$method = 'get' . ucfirst($key);
if (method_exists($this, $method)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Importers/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Importer
{
private Factory $factory;

public function __construct(Factory $factory = null)
public function __construct(?Factory $factory = null)
{
$this->factory = $factory ?? new Factory();
}
Expand Down
18 changes: 9 additions & 9 deletions src/Importers/XmlImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@

class XmlImporter
{
protected $factory;
protected Factory $factory;

/* var SimpleXMLElement */
protected $source;
protected SimpleXMLElement $source;

/**
* XmlImporter constructor.
*
* @param string|SimpleXMLElement $data Filename, XML string or SimpleXMLElement object
* @param string $ns URI or prefix of the namespace
* @param bool $isPrefix TRUE if $ns is a prefix, FALSE if it's a URI; defaults to FALSE
* @param string $factory (optional) Object factory, probably no need to set this outside testing.
* @param Factory|null $factory (optional) Object factory, probably no need to set this outside testing.
*/
public function __construct($data, $ns = '', $isPrefix = false, $factory = null)
public function __construct($data, string $ns = '', bool $isPrefix = false, ?Factory $factory = null)
{
$this->factory = isset($factory) ? $factory : new Factory();
$this->factory = $factory ?? new Factory();

if (is_a($data, SimpleXMLElement::class)) {
$this->source = $data;
Expand All @@ -40,10 +39,11 @@ public function __construct($data, $ns = '', $isPrefix = false, $factory = null)
// Store errors internally so that we can fetch them with libxml_get_errors() later
libxml_use_internal_errors(true);

$this->source = simplexml_load_string($data, 'SimpleXMLElement', 0, $ns, $isPrefix);
if (false === $this->source) {
$source = simplexml_load_string($data, 'SimpleXMLElement', 0, $ns, $isPrefix);
if (false === $source) {
throw new XmlException(libxml_get_errors());
}
$this->source = $source;
}

public function getMarcNamespace($namespaces)
Expand Down Expand Up @@ -98,7 +98,7 @@ public function getFirstRecord()

$parser = $this->factory->make('File_MARCXML', $record, File_MARCXML::SOURCE_SIMPLEXMLELEMENT, $ns);

return (new Collection($parser))->$this->getFirstRecord();
return (new Collection($parser))->first();
}

public function getCollection(): Collection
Expand Down
4 changes: 3 additions & 1 deletion src/MagicAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
trait MagicAccess
{
public function __get($key)
public function __get(string $key): mixed
{
// Convert key from underscore_case to camelCase.
$key_uc = preg_replace_callback(
Expand All @@ -24,5 +24,7 @@ function ($matches) {
if (method_exists($this, $method)) {
return call_user_func([$this, $method]);
}

return null;
}
}
10 changes: 5 additions & 5 deletions src/QueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(File_MARC_Reference $ref)
$this->data = $ref->data;
$this->content = $ref->content;

for ($i=0; $i < count($this->data); $i++) {
for ($i = 0; $i < count($this->data); $i++) {
if (is_a($this->data[$i], File_MARC_Field::class)) {
$this->data[$i] = new Field($this->data[$i]);
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getIterator(): Traversable|ArrayIterator
* @param mixed $offset An offset to check for.
* @return boolean true on success or false on failure.
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return isset($this->data[$offset]);
}
Expand All @@ -88,7 +88,7 @@ public function offsetExists($offset): bool
* @param mixed $offset The offset to retrieve.
* @return Field|File_MARC_Subfield|null
*/
public function offsetGet($offset): Field|File_MARC_Subfield|null
public function offsetGet(mixed $offset): Field|File_MARC_Subfield|null
{
return $this->data[$offset];
}
Expand All @@ -99,7 +99,7 @@ public function offsetGet($offset): Field|File_MARC_Subfield|null
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
$this->data[$offset] = $value;
}
Expand All @@ -109,7 +109,7 @@ public function offsetSet($offset, $value): void
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset The offset to unset.
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
unset($this->data[$offset]);
}
Expand Down
Loading