diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/AbstractBackend.php b/module/VuFindSearch/src/VuFindSearch/Backend/AbstractBackend.php index dff9533c693b39fb267c011a44cc1b6cbd5f5bda..758ac6db9e88a2d8a44d661af225956a33973fa6 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/AbstractBackend.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/AbstractBackend.php @@ -110,14 +110,11 @@ abstract class AbstractBackend implements BackendInterface, LoggerAwareInterface * * @param ResponseInterface $response Response * - * @return void + * @return ResponseInterface */ protected function injectSourceIdentifier(RecordCollectionInterface $response) { $response->setSourceIdentifier($this->identifier); - foreach ($response as $record) { - $record->setSourceIdentifier($this->identifier); - } return $response; } } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollectionFactory.php index d9a55f933b4f3dbb28d2618ffdef1f92fbdc86ee..6de40da175cd3d62bd2717215503393dfbbdd3d3 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollectionFactory.php @@ -100,7 +100,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface } $collection = new $this->collectionClass($response); foreach ($response['data'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php index 577e8558e53988b8a1be6773987af5a1ccaf9fa7..6fab79eebced0d4f291da1fff669eeaa4872131c 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php @@ -98,7 +98,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface ?? $response['Records'] ?? []; foreach ($records as $record) { - $collection->add(call_user_func($this->recordFactory, $record)); + $collection->add(call_user_func($this->recordFactory, $record), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollectionFactory.php index 9c237a60adb076b7c418bc7d2266b66e03a2d535..170774149e2961a80fed4f9cf5a7e4ce0a706477 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollectionFactory.php @@ -96,7 +96,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface } $collection = new $this->collectionClass($response); foreach ($response['docs'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollectionFactory.php index ede139d3023dd96bdb063e9c21bbbd768c82cbf5..fada0c1063468f1a763dd280de0278793adada7c 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollectionFactory.php @@ -100,7 +100,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface } $collection = new $this->collectionClass($response); foreach ($response['documents'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Response/RecordCollectionFactory.php index c8acc2dac29c2ceef86315815042106bd4e4651e..e624040d54e61a64662fcd16f7ec0e4424abaad1 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Response/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Response/RecordCollectionFactory.php @@ -93,7 +93,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface $response['total'], $response['offset'] ); foreach ($response['records'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollectionFactory.php index 210ca51b7b82e2b5f2b2cd3793d81a9b6bacb8d1..7837856a72526aec571b862d19b3ca4644e4b665 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollectionFactory.php @@ -100,7 +100,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface } $collection = new $this->collectionClass($response); foreach ($response['documents'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollectionFactory.php index 6ffd4eff6c4cf0642d7be5bd6febdf5757871927..3618d941906b97158f4d79fdc08a7cec6805e254 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollectionFactory.php @@ -97,7 +97,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface $collection = new $this->collectionClass($response); if (isset($response['response']['docs'])) { foreach ($response['response']['docs'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } } return $collection; diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollectionFactory.php index fbfb17f124b3ac8d33be1d525b2973027ae6dadc..aa319b2d92f932414694d2ed8320f2ebf227346a 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollectionFactory.php @@ -100,7 +100,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface } $collection = new $this->collectionClass($response); foreach ($response['documents'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Response/XML/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Response/XML/RecordCollectionFactory.php index 38a35c4f428f5bd9f72c9f59b21875f07a7d814e..29c727c73c120f839570aed6ba5ddc7591fab996 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Response/XML/RecordCollectionFactory.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Response/XML/RecordCollectionFactory.php @@ -100,7 +100,7 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface } $collection = new $this->collectionClass($response); foreach ($response['docs'] as $doc) { - $collection->add(call_user_func($this->recordFactory, $doc)); + $collection->add(call_user_func($this->recordFactory, $doc), false); } return $collection; } diff --git a/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php index 9320e7f226ca3b7ddfab284ebb31778d8e5bf434..196744af486e26b4e4c7d15c560d9ecf90685dae 100644 --- a/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php +++ b/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php @@ -127,6 +127,9 @@ abstract class AbstractRecordCollection implements RecordCollectionInterface public function setSourceIdentifier($identifier) { $this->source = $identifier; + foreach ($this->records as $record) { + $record->setSourceIdentifier($identifier); + } } /** @@ -142,18 +145,32 @@ abstract class AbstractRecordCollection implements RecordCollectionInterface /** * Add a record to the collection. * - * @param RecordInterface $record Record to add + * @param RecordInterface $record Record to add + * @param bool $checkExisting Whether to check for existing record in + * the collection (slower, but makes sure there are no duplicates) * * @return void */ - public function add(RecordInterface $record) + public function add(RecordInterface $record, $checkExisting = true) { - if (!in_array($record, $this->records, true)) { + if (!$checkExisting || !$this->has($record)) { $this->records[$this->pointer] = $record; $this->next(); } } + /** + * Check if the collection contains the given record + * + * @param RecordInterface $record Record to check + * + * @return bool + */ + public function has(RecordInterface $record) + { + return in_array($record, $this->records, true); + } + /** * Replace a record in the collection. * diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php index d02b6d932caf20b4277dd77b3e925943fecbd3e4..3c4357babe17b7fe5567c3fe9c11d6856487ee4b 100644 --- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php +++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/Response/Json/RecordCollectionTest.php @@ -192,4 +192,28 @@ class RecordCollectionTest extends TestCase $this->assertTrue(in_array($r2, $final)); $this->assertTrue(in_array($r3, $final)); } + + /** + * Test that the object handles offsets properly. + * + * @return void + */ + public function testAdd() + { + $coll = new RecordCollection( + [ + 'response' => ['numFound' => 10, 'start' => 5] + ] + ); + $record = $this->createMock(\VuFindSearch\Response\RecordInterface::class); + $coll->add($record); + for ($i = 0; $i < 4; $i++) { + $coll->add($this->createMock(\VuFindSearch\Response\RecordInterface::class)); + } + $this->assertEquals(5, $coll->count()); + $coll->add($record); + $this->assertEquals(5, $coll->count()); + $coll->add($record, false); + $this->assertEquals(6, $coll->count()); + } }