diff --git a/docs/en/customising_add_search_service.md b/docs/en/customising_add_search_service.md index 5027733..319f28e 100644 --- a/docs/en/customising_add_search_service.md +++ b/docs/en/customising_add_search_service.md @@ -33,7 +33,7 @@ in all these implementations that the array of documents is appropriately sized. All methods that rely on API calls should throw `IndexingServiceException` on error. -### addDocument(DocumentInterface $item): self +### addDocument(DocumentInterface $document): self This method is responsible for adding a single document to the indexes. Keep in mind, the `DocumentInterface` object that is passed to this function is self-aware of the indexes @@ -42,14 +42,14 @@ it is assigned to. Be sure to check each item's `shouldIndex()` method, as well. Return value should be the unique ID of the document. ```php -public function addDocument(DocumentInterface $item): ?string +public function addDocument(DocumentInterface $document): ?string { - if (!$item->shouldIndex()) { + if (!$document->shouldIndex()) { return null; } - $fields = DocumentBuilder::singleton()->toArray($item); - $indexes = IndexConfiguration::singleton()->getIndexesForDocument($item); + $fields = DocumentBuilder::singleton()->toArray($document); + $indexes = IndexConfiguration::singleton()->getIndexesForDocument($document); foreach (array_keys($indexes) as $indexName) { // your custom API call here @@ -59,7 +59,7 @@ public function addDocument(DocumentInterface $item): ?string ); } - return $item->getIdentifier(); + return $document->getIdentifier(); } ``` @@ -67,11 +67,11 @@ public function addDocument(DocumentInterface $item): ?string **Tip**: Consider passing `DocumentBuilder` and `IndexConfiguration` as a constructor arguments to your indexing service. -### addDocuments(array $items): self +### addDocuments(array $documents): self Same as `addDocument()`, but accepts an array of `DocumentInterface` objects. It is recommended that the `addDocument()` method works as a proxy for `addDocuments()`, e.g. -`$this->addDocuments([$item])`. +`$this->addDocuments([$document])`. **Tip**: Build a map of index names to documents to minimise calls to your API. @@ -89,27 +89,27 @@ Removes a document from its indexes. Return value should be the unique ID of the document. ```php -public function removeDocument(DocumentInterface $doc): ?string +public function removeDocument(DocumentInterface $document): ?string { - $indexes = IndexConfiguration::singleton()->getIndexesForDocument($doc); + $indexes = IndexConfiguration::singleton()->getIndexesForDocument($document); foreach (array_keys($indexes) as $indexName) { // your custom API call here $myAPI->removeDocumentFromIndex( static::environmentizeIndex($indexName), - $item->getIdentifier() + $document->getIdentifier() ); } - return $item->getIdentifier(); + return $document->getIdentifier(); } ``` -### removeDocuments(array $items): array +### removeDocuments(array $documents): array Same as `removeDocument()`, but accepts an array of `DocumentInterface` objects. It is recommended that the `removeDocument()` method works as a proxy for `removeDocuments()`, e.g. -`$this->removeDocuments([$item])`. +`$this->removeDocuments([$document])`. Return value should be an array of the Document IDs that were removed diff --git a/src/Interfaces/BatchDocumentInterface.php b/src/Interfaces/BatchDocumentInterface.php index cca42c9..fc64a5f 100644 --- a/src/Interfaces/BatchDocumentInterface.php +++ b/src/Interfaces/BatchDocumentInterface.php @@ -12,9 +12,9 @@ interface BatchDocumentInterface public function addDocuments(array $documents): array; /** + * @param DocumentInterface[] $documents * @return array Array of IDs of the Documents removed - * @param DocumentInterface[] $items */ - public function removeDocuments(array $items): array; + public function removeDocuments(array $documents): array; } diff --git a/src/Interfaces/IndexingInterface.php b/src/Interfaces/IndexingInterface.php index 64edf16..403792e 100644 --- a/src/Interfaces/IndexingInterface.php +++ b/src/Interfaces/IndexingInterface.php @@ -12,13 +12,13 @@ interface IndexingInterface extends BatchDocumentInterface * @return string|null ID of the Document added * @throws IndexingServiceException */ - public function addDocument(DocumentInterface $item): ?string; + public function addDocument(DocumentInterface $document): ?string; /** * @return string|null ID of the Document removed * @throws IndexingServiceException */ - public function removeDocument(DocumentInterface $doc): ?string; + public function removeDocument(DocumentInterface $document): ?string; /** * @throws IndexingServiceException diff --git a/src/Service/DocumentChunkFetcher.php b/src/Service/DocumentChunkFetcher.php index 419e325..c5f9982 100644 --- a/src/Service/DocumentChunkFetcher.php +++ b/src/Service/DocumentChunkFetcher.php @@ -33,12 +33,12 @@ public function chunk(int $chunkSize = 100): iterable $currentChunk = 0; - while ($chunk = $this->fetcher->fetch($chunkSize, $chunkSize * $currentChunk)) { - foreach ($chunk as $item) { - yield $item; + while ($chunks = $this->fetcher->fetch($chunkSize, $chunkSize * $currentChunk)) { + foreach ($chunks as $chunk) { + yield $chunk; } - if (sizeof($chunk) < $chunkSize) { + if (sizeof($chunks) < $chunkSize) { break; } diff --git a/src/Service/EnterpriseSearch/EnterpriseSearchService.php b/src/Service/EnterpriseSearch/EnterpriseSearchService.php index 0d20b22..1e737a3 100644 --- a/src/Service/EnterpriseSearch/EnterpriseSearchService.php +++ b/src/Service/EnterpriseSearch/EnterpriseSearchService.php @@ -83,9 +83,9 @@ public function getDocumentationURL(): ?string * @throws IndexingServiceException * @throws NotFoundExceptionInterface */ - public function addDocument(DocumentInterface $item): ?string + public function addDocument(DocumentInterface $document): ?string { - $processedIds = $this->addDocuments([$item]); + $processedIds = $this->addDocuments([$document]); return array_shift($processedIds); } @@ -116,27 +116,27 @@ public function addDocuments(array $documents): array } /** - * @param DocumentInterface $doc + * @param DocumentInterface $document * @throws Exception */ - public function removeDocument(DocumentInterface $doc): ?string + public function removeDocument(DocumentInterface $document): ?string { - $processedIds = $this->removeDocuments([$doc]); + $processedIds = $this->removeDocuments([$document]); return array_shift($processedIds); } /** - * @param DocumentInterface[] $items + * @param DocumentInterface[] $documents * @throws Exception */ - public function removeDocuments(array $items): array + public function removeDocuments(array $documents): array { $documentMap = []; $processedIds = []; - foreach ($items as $item) { - if (!$item instanceof DocumentInterface) { + foreach ($documents as $document) { + if (!$document instanceof DocumentInterface) { throw new InvalidArgumentException(sprintf( '%s not passed an instance of %s', __FUNCTION__, @@ -144,14 +144,14 @@ public function removeDocuments(array $items): array )); } - $indexes = $this->getConfiguration()->getIndexesForDocument($item); + $indexes = $this->getConfiguration()->getIndexesForDocument($document); foreach (array_keys($indexes) as $indexName) { if (!isset($documentMap[$indexName])) { $documentMap[$indexName] = []; } - $documentMap[$indexName][] = $item->getIdentifier(); + $documentMap[$indexName][] = $document->getIdentifier(); } } @@ -590,8 +590,8 @@ private function getContentMapForDocuments(array $documents): array { $documentMap = []; - foreach ($documents as $item) { - if (!$item instanceof DocumentInterface) { + foreach ($documents as $document) { + if (!$document instanceof DocumentInterface) { throw new InvalidArgumentException(sprintf( '%s not passed an instance of %s', __FUNCTION__, @@ -599,12 +599,12 @@ private function getContentMapForDocuments(array $documents): array )); } - if (!$item->shouldIndex()) { + if (!$document->shouldIndex()) { continue; } try { - $fields = $this->getBuilder()->toArray($item); + $fields = $this->getBuilder()->toArray($document); } catch (IndexConfigurationException $e) { Injector::inst()->get(LoggerInterface::class)->warning( sprintf('Failed to convert document to array: %s', $e->getMessage()) @@ -613,11 +613,11 @@ private function getContentMapForDocuments(array $documents): array continue; } - $indexes = $this->getConfiguration()->getIndexesForDocument($item); + $indexes = $this->getConfiguration()->getIndexesForDocument($document); if (!$indexes) { Injector::inst()->get(LoggerInterface::class)->warn( - sprintf('No valid indexes found for document %s, skipping...', $item->getIdentifier()) + sprintf('No valid indexes found for document %s, skipping...', $document->getIdentifier()) ); continue; diff --git a/src/Service/Naive/NaiveSearchService.php b/src/Service/Naive/NaiveSearchService.php index d7d5c6d..0a74507 100644 --- a/src/Service/Naive/NaiveSearchService.php +++ b/src/Service/Naive/NaiveSearchService.php @@ -9,7 +9,7 @@ class NaiveSearchService implements IndexingInterface, BatchDocumentRemovalInterface { - public function addDocument(DocumentInterface $item): ?string + public function addDocument(DocumentInterface $document): ?string { return null; } @@ -19,7 +19,7 @@ public function addDocuments(array $documents): array return []; } - public function removeDocuments(array $items): array + public function removeDocuments(array $documents): array { return []; } @@ -59,7 +59,7 @@ public function getDocumentTotal(string $indexName): int return 0; } - public function removeDocument(DocumentInterface $doc): ?string + public function removeDocument(DocumentInterface $document): ?string { return null; } diff --git a/src/Service/PageCrawler.php b/src/Service/PageCrawler.php index c669f2a..475d83b 100644 --- a/src/Service/PageCrawler.php +++ b/src/Service/PageCrawler.php @@ -29,16 +29,16 @@ class PageCrawler */ private static string $content_xpath_selector = '//main'; - public function getMainContent(DataObject $item): string + public function getMainContent(DataObject $dataObject): string { - if (!$item->hasMethod('Link')) { + if (!$dataObject->hasMethod('Link')) { return ''; } $page = null; try { - $response = Director::test($item->AbsoluteLink()); + $response = Director::test($dataObject->AbsoluteLink()); $page = $response->getBody(); } catch (Throwable $e) { Injector::inst()->create(LoggerInterface::class)->error($e); diff --git a/tests/DataObject/DataObjectDocumentTest.php b/tests/DataObject/DataObjectDocumentTest.php index a7ea8a8..9a3fd0b 100644 --- a/tests/DataObject/DataObjectDocumentTest.php +++ b/tests/DataObject/DataObjectDocumentTest.php @@ -366,29 +366,29 @@ public function testGetDependentDocuments(): void $this->assertCount(4, $dependencies); - $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $item) { + $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $document) { $obj = $this->objFromFixture(DataObjectFake::class, 'one'); - return $item->getSourceClass() === DataObjectFake::class && - $item->getDataObject()->ID === $obj->ID; + return $document->getSourceClass() === DataObjectFake::class && + $document->getDataObject()->ID === $obj->ID; }); - $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $item) { + $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $document) { $obj = $this->objFromFixture(DataObjectFake::class, 'two'); - return $item->getSourceClass() === DataObjectFake::class && - $item->getDataObject()->ID === $obj->ID; + return $document->getSourceClass() === DataObjectFake::class && + $document->getDataObject()->ID === $obj->ID; }); - $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $item) { + $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $document) { $obj = $this->objFromFixture(DataObjectFake::class, 'three'); - return $item->getSourceClass() === DataObjectFake::class && - $item->getDataObject()->ID === $obj->ID; + return $document->getSourceClass() === DataObjectFake::class && + $document->getDataObject()->ID === $obj->ID; }); - $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $item) { + $this->assertArrayContainsCallback($dependencies, function (DataObjectDocument $document) { $obj = $this->objFromFixture(ImageFake::class, 'two'); - return $item->getSourceClass() === ImageFake::class && - $item->getDataObject()->ID === $obj->ID; + return $document->getSourceClass() === ImageFake::class && + $document->getDataObject()->ID === $obj->ID; }); } diff --git a/tests/Fake/ServiceFake.php b/tests/Fake/ServiceFake.php index efe5c41..daf436c 100644 --- a/tests/Fake/ServiceFake.php +++ b/tests/Fake/ServiceFake.php @@ -16,32 +16,32 @@ class ServiceFake implements IndexingInterface, BatchDocumentRemovalInterface public int $maxDocSize = 1000; - public function addDocument(DocumentInterface $item): ?string + public function addDocument(DocumentInterface $document): ?string { - $this->documents[$item->getIdentifier()] = DocumentBuilder::singleton()->toArray($item); + $this->documents[$document->getIdentifier()] = DocumentBuilder::singleton()->toArray($document); - return $item->getIdentifier(); + return $document->getIdentifier(); } public function addDocuments(array $documents): array { $ids = []; - foreach ($documents as $item) { - $this->addDocument($item); - $ids[] = $item->getIdentifier(); + foreach ($documents as $document) { + $this->addDocument($document); + $ids[] = $document->getIdentifier(); } return $ids; } - public function removeDocuments(array $items): array + public function removeDocuments(array $documents): array { $ids = []; - foreach ($items as $item) { - $this->removeDocument($item); - $ids[] = $item->getIdentifier(); + foreach ($documents as $document) { + $this->removeDocument($document); + $ids[] = $document->getIdentifier(); } return $ids; @@ -59,11 +59,11 @@ public function removeAllDocuments(string $indexName): int return $numDocs; } - public function removeDocument(DocumentInterface $doc): ?string + public function removeDocument(DocumentInterface $document): ?string { - unset($this->documents[$doc->getIdentifier()]); + unset($this->documents[$document->getIdentifier()]); - return $doc->getIdentifier(); + return $document->getIdentifier(); } public function getDocument(string $id): ?DocumentInterface