Skip to content

Commit

Permalink
Make Document variable names consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Penny committed Aug 10, 2022
1 parent 1de946d commit 1f7dc9d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 70 deletions.
28 changes: 14 additions & 14 deletions docs/en/customising_add_search_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -59,19 +59,19 @@ public function addDocument(DocumentInterface $item): ?string
);
}
return $item->getIdentifier();
return $document->getIdentifier();
}
```

**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.

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/BatchDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
4 changes: 2 additions & 2 deletions src/Interfaces/IndexingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Service/DocumentChunkFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
34 changes: 17 additions & 17 deletions src/Service/EnterpriseSearch/EnterpriseSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -116,42 +116,42 @@ 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__,
DocumentInterface::class
));
}

$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();
}
}

Expand Down Expand Up @@ -590,21 +590,21 @@ 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__,
DocumentInterface::class
));
}

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())
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Service/Naive/NaiveSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class NaiveSearchService implements IndexingInterface, BatchDocumentRemovalInterface
{

public function addDocument(DocumentInterface $item): ?string
public function addDocument(DocumentInterface $document): ?string
{
return null;
}
Expand All @@ -19,7 +19,7 @@ public function addDocuments(array $documents): array
return [];
}

public function removeDocuments(array $items): array
public function removeDocuments(array $documents): array
{
return [];
}
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Service/PageCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions tests/DataObject/DataObjectDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

Expand Down
26 changes: 13 additions & 13 deletions tests/Fake/ServiceFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 1f7dc9d

Please sign in to comment.