Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: content-matcher also requires the resource #19

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Service/Indexer/ContentCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Service\Indexer;

use Atoolo\Resource\Resource;
use Atoolo\Search\Service\Indexer\SiteKit\ContentMatcher;

class ContentCollector
Expand All @@ -16,9 +17,9 @@ public function __construct(private readonly iterable $matchers) {}
/**
* @param array<mixed,mixed> $data
*/
public function collect(array $data): string
public function collect(array $data, Resource $resource): string
{
$content = $this->walk([], $data);
$content = $this->walk([], $data, $resource);
return implode(' ', $content);
}

Expand All @@ -27,7 +28,7 @@ public function collect(array $data): string
* @param array<mixed,mixed> $data
* @return string[]
*/
private function walk(array $path, array $data): array
private function walk(array $path, array $data, Resource $resource): array
{
$contentCollections = [];
foreach ($data as $key => $value) {
Expand All @@ -41,14 +42,14 @@ private function walk(array $path, array $data): array

$matcherContent = [];
foreach ($this->matchers as $matcher) {
$content = $matcher->match($path, $value);
$content = $matcher->match($path, $value, $resource);
if (!is_string($content)) {
continue;
}
$matcherContent[] = $content;
}
$contentCollections[] = $matcherContent;
$contentCollections[] = $this->walk($path, $value);
$contentCollections[] = $this->walk($path, $value, $resource);

if (is_string($key)) {
array_pop($path);
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Indexer/SiteKit/ContentMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Atoolo\Search\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;

/**
* The `ContentMatcher` interface is implemented in order to extract from the
* content structure of resources the content that is relevant for the `content`
Expand Down Expand Up @@ -31,5 +33,5 @@ interface ContentMatcher
* @return string|false The extracted content or `false` if the
* content is not relevant for the search index.
*/
public function match(array $path, array $value): string|false;
public function match(array $path, array $value, Resource $resource): string|false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ private function enrichContent(

$content[] = $this->contentCollector->collect(
$resource->data->getArray('content'),
$resource,
);

/** @var ContactPoint $contactPoint */
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Indexer/SiteKit/HeadlineMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Atoolo\Search\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;

class HeadlineMatcher implements ContentMatcher
{
/**
* @inheritDoc
*/
public function match(array $path, array $value): string|false
public function match(array $path, array $value, Resource $resource): string|false
{
$len = count($path);
if ($len < 2) {
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Indexer/SiteKit/QuoteSectionMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Atoolo\Search\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;

/**
* @phpstan-type Model array{quote?: ?string, citation?: ?string}
*/
Expand All @@ -12,7 +14,7 @@ class QuoteSectionMatcher implements ContentMatcher
/**
* @inheritDoc
*/
public function match(array $path, array $value): string|false
public function match(array $path, array $value, Resource $resource): string|false
{
$len = count($path);
if ($len < 1) {
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Indexer/SiteKit/RichtTextMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Atoolo\Search\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;

class RichtTextMatcher implements ContentMatcher
{
/**
* @inheritDoc
*/
public function match(array $path, array $value): string|false
public function match(array $path, array $value, Resource $resource): string|false
{
$modelType = $value['modelType'] ?? false;
if ($modelType !== 'html.richText') {
Expand Down
7 changes: 5 additions & 2 deletions test/Service/Indexer/ContentCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Test\Service\Indexer;

use Atoolo\Resource\Resource;
use Atoolo\Search\Service\Indexer\ContentCollector;
use Atoolo\Search\Service\Indexer\SiteKit\ContentMatcher;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -15,7 +16,7 @@ class ContentCollectorTest extends TestCase
public function testCollect(): void
{
$matcher = (new class implements ContentMatcher {
public function match(array $path, array $value): string|false
public function match(array $path, array $value, Resource $resource): string|false
{
$modelType = $value['modelType'] ?? false;
if ($modelType !== 'html.richText') {
Expand All @@ -41,7 +42,9 @@ public function match(array $path, array $value): string|false
],
],
];
$content = $collector->collect($data);

$resource = $this->createStub(Resource::class);
$content = $collector->collect($data, $resource);

$this->assertEquals('<p>Ein Text</p>', $content, 'unexpected content');
}
Expand Down
10 changes: 7 additions & 3 deletions test/Service/Indexer/SiteKit/HeadlineMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Test\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;
use Atoolo\Search\Service\Indexer\SiteKit\HeadlineMatcher;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -19,7 +20,8 @@ public function testMatcher(): void
"headline" => "Überschrift",
];

$content = $matcher->match(['items', 'model'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['items', 'model'], $value, $resource);

$this->assertEquals('Überschrift', $content, 'unexpected headline');
}
Expand All @@ -32,7 +34,8 @@ public function testMatcherNotMachedPathToShort(): void
"headline" => "Überschrift",
];

$content = $matcher->match(['model'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['model'], $value, $resource);

$this->assertEmpty(
$content,
Expand All @@ -48,7 +51,8 @@ public function testMatcherNotMachedNoModel(): void
"headline" => "Überschrift",
];

$content = $matcher->match(['items', 'modelX'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['items', 'modelX'], $value, $resource);

$this->assertEmpty(
$content,
Expand Down
16 changes: 11 additions & 5 deletions test/Service/Indexer/SiteKit/QuoteSectionMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Test\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;
use Atoolo\Search\Service\Indexer\SiteKit\QuoteSectionMatcher;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -23,7 +24,8 @@ public function testMatcher(): void
],
];

$content = $matcher->match(['items'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['items'], $value, $resource);

$this->assertEquals(
'Quote-Text Citation',
Expand All @@ -43,7 +45,8 @@ public function testMatcherNoMatchPathToShort(): void
],
];

$content = $matcher->match([], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match([], $value, $resource);

$this->assertEmpty(
$content,
Expand All @@ -63,7 +66,8 @@ public function testMatcherNoMatchNoItems(): void
],
];

$content = $matcher->match(['itemsX'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['itemsX'], $value, $resource);

$this->assertEmpty(
$content,
Expand All @@ -83,7 +87,8 @@ public function testMatcherNoMatchInvalidType(): void
],
];

$content = $matcher->match(['items'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['items'], $value, $resource);

$this->assertEmpty(
$content,
Expand All @@ -103,7 +108,8 @@ public function testMatcherNoMatchMissingModel(): void
],
];

$content = $matcher->match(['items'], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match(['items'], $value, $resource);

$this->assertEmpty(
$content,
Expand Down
10 changes: 7 additions & 3 deletions test/Service/Indexer/SiteKit/RichtTextMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Test\Service\Indexer\SiteKit;

use Atoolo\Resource\Resource;
use Atoolo\Search\Service\Indexer\SiteKit\RichtTextMatcher;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -21,7 +22,8 @@ public function testMatcher(): void
"text" => "<p>Ein Text</p>",
];

$content = $matcher->match([], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match([], $value, $resource);

$this->assertEquals('Ein Text', $content, 'unexpected content');
}
Expand All @@ -36,7 +38,8 @@ public function testMatcherNotMatchedInvalidType(): void
"text" => "<p>Ein Text</p>",
];

$content = $matcher->match([], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match([], $value, $resource);

$this->assertEmpty(
$content,
Expand All @@ -54,7 +57,8 @@ public function testMatcherNotMatchedTextMissing(): void
"textX" => "<p>Ein Text</p>",
];

$content = $matcher->match([], $value);
$resource = $this->createStub(Resource::class);
$content = $matcher->match([], $value, $resource);

$this->assertEmpty(
$content,
Expand Down
Loading