Created
January 9, 2012 23:14
-
-
Save mark-cooper/1585549 to your computer and use it in GitHub Desktop.
PHP File_MARC equality oddness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // 5.3 | |
// From: http://pear.php.net/package/File_MARC | |
require 'File/MARC.php'; | |
// From: https://github.com/abelperez/php-commons | |
require '../lib/collections/SimpleIterator.php'; | |
require '../lib/collections/Set.php'; | |
require '../lib/collections/ArraySet.php'; | |
// Create a datafield | |
$a = new File_MARC_Data_Field('245', null, 1, 0); | |
$a->appendSubfield(new File_MARC_Subfield('a', 'Hello, World! /')); | |
$a->appendSubfield(new File_MARC_Subfield('c', 'by Mark Cooper.')); | |
echo $a . '<br/>'; | |
echo var_dump($a) . '<br/>'; | |
// Create a datafield - like the API provided example | |
$b_subs = array(); # just to be extra cautious ... | |
$b_subs[] = new File_MARC_Subfield('a', 'Hello, World! /'); | |
$b_subs[] = new File_MARC_Subfield('c', 'by Mark Cooper.'); | |
$b = new File_MARC_Data_Field('245', $b_subs, 1, 0); | |
echo $b . '<br/>'; | |
echo var_dump($b) . '<br/>'; | |
# Equality testing has issues ... | |
// echo ($a == $b) ? 'The fields are equal<br/>' : 'The fields are unequal<br/>'; # breaks | |
echo ($a === $b) ? 'The fields are equal<br/>' : 'The fields are unequal<br/>'; # works | |
// Let's try the identical approach | |
$c = new File_MARC_Data_Field('245', null, 1, 0); | |
$c->appendSubfield(new File_MARC_Subfield('a', 'Hello, World! /')); | |
$c->appendSubfield(new File_MARC_Subfield('c', 'by Mark Cooper.')); | |
echo $c . '<br/>'; | |
echo var_dump($c) . '<br/>'; | |
// echo ($a == $c) ? 'The fields are equal<br/>' : 'The fields are unequal<br/>'; # still breaks ... | |
$records = new File_MARC('sample.mrc'); | |
$all_subjects = array(); | |
$unique_subjects = new ArraySet(); | |
while($record = $records->next()) { | |
foreach ($record->getFields('650') as $subject) { | |
$all_subjects[] = $subject; | |
// $unique_subjects->add($subject); # fails at in_array | |
$unique_subjects->add($subject->__toString()); # works | |
} | |
} | |
echo 'ALL: ' . count($all_subjects); | |
echo '<br/>'; | |
echo 'UNQ: ' . $unique_subjects->size(); | |
echo '<br/>'; | |
$i = $unique_subjects->iterator(); | |
$i->sort(); | |
while($s = $i->next()) { | |
echo $s . '<br/>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment