forked from TYPO3/html-sanitizer
-
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.
Related: TYPO3#70
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the TYPO3 project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under the terms | ||
* of the MIT License (MIT). For the full copyright and license information, | ||
* please read the LICENSE file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
namespace TYPO3\HtmlSanitizer\Behavior; | ||
|
||
use LogicException; | ||
|
||
class MultiTokenAttrValue implements AttrValueInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $delimiter; | ||
|
||
/** | ||
* @var list<string> | ||
*/ | ||
protected $tokens; | ||
|
||
public function __construct(string $delimiter, string ...$tokens) | ||
{ | ||
if ($delimiter === '') { | ||
throw new LogicException('Delimiter cannot be empty', 1642111976); | ||
} | ||
$tokens = array_filter($tokens, [$this, 'keepNonEmpty']); | ||
if ($tokens === []) { | ||
throw new LogicException('Tokens cannot be empty or only empty strings', 1642111637); | ||
} | ||
$this->delimiter = $delimiter; | ||
$this->tokens = $tokens; | ||
} | ||
|
||
public function matches(string $value): bool | ||
{ | ||
$tokens = explode($this->delimiter, $value); | ||
$tokens = array_filter($tokens, [$this, 'keepNonEmpty']); | ||
// in case there is no token, the result implicit is `true` | ||
// @todo has to be changed, in case mandatory tokes would be implemented | ||
if (empty($tokens)) { | ||
return true; | ||
} | ||
return array_diff($tokens, $this->tokens) === []; | ||
} | ||
|
||
protected function keepNonEmpty(string $item): bool | ||
{ | ||
return $item !== ''; | ||
} | ||
} |
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