-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-7987: Added extension point to skip nodes while extracting text
- Loading branch information
Showing
5 changed files
with
125 additions
and
1 deletion.
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
22 changes: 22 additions & 0 deletions
22
src/contracts/RichText/TextExtractor/NodeFilterInterface.php
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 | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor; | ||
|
||
use DOMNode; | ||
|
||
/** | ||
* Filters nodes for text extraction. | ||
*/ | ||
interface NodeFilterInterface | ||
{ | ||
/** | ||
* Return true to preserve the node, false to remove it. | ||
*/ | ||
public function filter(DOMNode $node): bool; | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/lib/RichText/TextExtractor/NodeFilter/AggregateFilter.php
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,37 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter; | ||
|
||
use DOMNode; | ||
use Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface; | ||
|
||
final class AggregateFilter implements NodeFilterInterface | ||
{ | ||
/** @var \Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface[] */ | ||
private iterable $filters; | ||
|
||
/** | ||
* @param \Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface[]|iterable $filters | ||
*/ | ||
public function __construct(iterable $filters) | ||
{ | ||
$this->filters = $filters; | ||
} | ||
|
||
public function filter(DOMNode $node): bool | ||
{ | ||
foreach ($this->filters as $filter) { | ||
if ($filter->filter($node)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/lib/RichText/TextExtractor/NodeFilter/NodePathFilter.php
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,40 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter; | ||
|
||
use DOMNode; | ||
use Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface; | ||
|
||
final class NodePathFilter implements NodeFilterInterface | ||
{ | ||
/** | ||
* Path in reverse order. | ||
* | ||
* @var string[] | ||
*/ | ||
private array $path; | ||
|
||
public function __construct(string ...$path) | ||
{ | ||
$this->path = array_reverse($path); | ||
} | ||
|
||
public function filter(DOMNode $node): bool | ||
{ | ||
foreach ($this->path as $name) { | ||
if ($node === null || $node->nodeName !== $name) { | ||
return true; | ||
} | ||
|
||
$node = $node->parentNode; | ||
} | ||
|
||
return false; | ||
} | ||
} |