-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: content matcher to index full-text-content
- Loading branch information
1 parent
abcd512
commit c40d02d
Showing
11 changed files
with
424 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Service\Indexer; | ||
|
||
use Atoolo\Search\Service\Indexer\SiteKit\ContentMatcher; | ||
|
||
class ContentCollector | ||
{ | ||
/** | ||
* @param iterable<ContentMatcher> $matchers | ||
*/ | ||
public function __construct(private readonly iterable $matchers) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<mixed,mixed> $data | ||
*/ | ||
public function collect(array $data): string | ||
{ | ||
$content = $this->walk([], $data); | ||
return implode(' ', $content); | ||
} | ||
|
||
/** | ||
* @param string[] $path | ||
* @param array<mixed,mixed> $data | ||
* @return string[] | ||
*/ | ||
private function walk(array $path, array $data): array | ||
{ | ||
$contentCollections = []; | ||
foreach ($data as $key => $value) { | ||
if (!is_array($value)) { | ||
continue; | ||
} | ||
|
||
if (is_string($key)) { | ||
$path[] = $key; | ||
} | ||
|
||
$matcherContent = []; | ||
foreach ($this->matchers as $matcher) { | ||
$content = $matcher->match($path, $value); | ||
if (!is_string($content)) { | ||
continue; | ||
} | ||
$matcherContent[] = $content; | ||
} | ||
$contentCollections[] = $matcherContent; | ||
$contentCollections[] = $this->walk($path, $value); | ||
|
||
if (is_string($key)) { | ||
array_pop($path); | ||
} | ||
} | ||
|
||
return array_merge([], ...$contentCollections); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Service\Indexer\SiteKit; | ||
|
||
interface ContentMatcher | ||
{ | ||
/** | ||
* @param string[] $path | ||
* @param array<mixed, mixed> $value | ||
*/ | ||
public function match(array $path, array $value): bool|string; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Service\Indexer\SiteKit; | ||
|
||
class HeadlineMatcher implements ContentMatcher | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function match(array $path, array $value): bool|string | ||
{ | ||
$len = count($path); | ||
if ($len < 2) { | ||
return false; | ||
} | ||
|
||
if ( | ||
$path[$len - 2] !== 'items' || | ||
$path[$len - 1] !== 'model' | ||
) { | ||
return false; | ||
} | ||
|
||
$headline = $value['headline'] ?? false; | ||
return is_string($headline) ? $headline : false; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Service\Indexer\SiteKit; | ||
|
||
/** | ||
* @phpstan-type Model array{quote?:string, citation?:string} | ||
*/ | ||
class QuoteSectionMatcher implements ContentMatcher | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function match(array $path, array $value): bool|string | ||
{ | ||
$len = count($path); | ||
if ($len < 1) { | ||
return false; | ||
} | ||
|
||
if ( | ||
$path[$len - 1] !== 'items' | ||
) { | ||
return false; | ||
} | ||
|
||
if (($value['type'] ?? '') !== 'quote') { | ||
return false; | ||
} | ||
|
||
$model = $value['model'] ?? false; | ||
if (!is_array($model)) { | ||
return false; | ||
} | ||
|
||
/** @var Model $model */ | ||
|
||
$content = []; | ||
$quote = $model['quote'] ?? ''; | ||
if (is_string($quote)) { | ||
$content[] = $quote; | ||
} | ||
$citation = $model['citation'] ?? ''; | ||
if (is_string($citation)) { | ||
$content[] = $citation; | ||
} | ||
|
||
return implode(' ', $content); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Service\Indexer\SiteKit; | ||
|
||
class RichtTextMatcher implements ContentMatcher | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function match(array $path, array $value): bool|string | ||
{ | ||
$modelType = $value['modelType'] ?? false; | ||
if ($modelType !== 'html.richText') { | ||
return false; | ||
} | ||
|
||
$text = $value['text'] ?? false; | ||
return is_string($text) ? strip_tags($text) : false; | ||
} | ||
} |
Oops, something went wrong.