From 39503e3f131d87dc4a846e1fa49c7962f1874493 Mon Sep 17 00:00:00 2001 From: Horacio Degiorgi Date: Thu, 11 Jul 2024 23:55:09 -0300 Subject: [PATCH] add support for php8.1 ant strict types in SplDoublyLinkedList specially for key method (only support integers) --- File/MARC.php | 12 +++++++++--- File/MARC/List.php | 4 +++- File/MARC/Subfield.php | 3 ++- tests/marc_007.phpt | 5 +++-- tests/marc_list_001.phpt | 5 +++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/File/MARC.php b/File/MARC.php index 19a85a8..f2ca3a3 100644 --- a/File/MARC.php +++ b/File/MARC.php @@ -167,9 +167,13 @@ function __construct($source, $type = self::SOURCE_FILE, $record_class = null) } break; - case self::SOURCE_STRING: + case self::SOURCE_STRING : $this->type = self::SOURCE_STRING; + if ($source===null) + $this->source=[]; + else $this->source = explode(File_MARC::END_OF_RECORD, $source); + break; default: @@ -196,7 +200,9 @@ function nextRaw() $record = preg_replace('/^[\\x0a\\x0d\\x00]+/', "", $record); } elseif ($this->type == self::SOURCE_STRING) { + $record = array_shift($this->source); + } // Exit if we are at the end of the file @@ -318,9 +324,9 @@ private function _decode($text) if (!is_numeric($offset)) { $offset = 0; } - + if ($tag===null) $tag=''; // Check directory validity - if (!preg_match("/^[0-9A-Za-z]{3}$/", $tag)) { + if ( !preg_match("/^[0-9A-Za-z]{3}$/", $tag)) { $marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_DIRECTORY_TAG], array("tag" => $tag))); } if (!preg_match("/^\d{4}$/", $len)) { diff --git a/File/MARC/List.php b/File/MARC/List.php index 1bc9930..b9c0146 100644 --- a/File/MARC/List.php +++ b/File/MARC/List.php @@ -98,7 +98,9 @@ function key(): int if ($this->current() instanceof File_MARC_Field) { return $this->current()->getTag(); } elseif ($this->current() instanceof File_MARC_Subfield) { - return $this->current()->getCode(); + if (!is_int( $this->current()->getCode())) return ord($this->current()->getcode()); + else + return $this->current()->getCode(); } return false; } diff --git a/File/MARC/Subfield.php b/File/MARC/Subfield.php index 227d4af..9f43f49 100644 --- a/File/MARC/Subfield.php +++ b/File/MARC/Subfield.php @@ -237,7 +237,8 @@ function setPosition($pos) function isEmpty() { // There is data - if (strlen($this->data)) { + if ($this->data===null) return true; + if ( strlen($this->data)) { return false; } diff --git a/tests/marc_007.phpt b/tests/marc_007.phpt index 5c2172b..d8eb62f 100644 --- a/tests/marc_007.phpt +++ b/tests/marc_007.phpt @@ -10,13 +10,14 @@ $marc_file = new File_MARC($dir . '/' . 'example.mrc'); if ($marc_record = $marc_file->next()) { foreach ($marc_record->getFields() as $tag=>$value) { - print "$tag: "; + //print "$tag: "; + print $value->gettag().': '; if ($value instanceof File_MARC_Control_Field) { print $value->getData(); } else { foreach ($value->getSubfields() as $code=>$subdata) { - print "_$code"; + print "_".$subdata->getcode(); } } print "\n"; diff --git a/tests/marc_list_001.phpt b/tests/marc_list_001.phpt index 84361e6..c1ce419 100644 --- a/tests/marc_list_001.phpt +++ b/tests/marc_list_001.phpt @@ -50,12 +50,13 @@ echo $list->count() . " nodes\n"; // Check key method echo "---\n"; $list->rewind(); -echo 'Key (field): ' . $list->key() . "\n"; +echo 'Key (field): ' . str_pad((string)$list->key(),3,'0',STR_PAD_LEFT) . "\n"; $subfield = new File_MARC_Subfield( 'a', 'test' ); $list->prependNode( $subfield ); $list->rewind(); -echo 'Key (subfield): ' . $list->key(). "\n"; +// when we call key() method directly we must to use chr to get the int index as char +echo 'Key (subfield): ' . chr($list->key()). "\n"; ?> --EXPECT-- 0: 002 00002