-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from JAK0TA/add-start-with-viewhelper
feat: add a view helper to check what the string starts with
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
Classes/ViewHelpers/Condition/String/StartWithViewHelper.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,30 @@ | ||
<?php | ||
|
||
// Copyright JAKOTA Design Group GmbH. All rights reserved. | ||
declare(strict_types=1); | ||
|
||
namespace JAKOTA\Typo3ToolBox\ViewHelpers\Condition\String; | ||
|
||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper; | ||
|
||
class StartWithViewHelper extends AbstractConditionViewHelper { | ||
/** | ||
* Initialize arguments. | ||
*/ | ||
public function initializeArguments(): void { | ||
parent::initializeArguments(); | ||
|
||
$this->registerArgument('haystack', 'string', 'The string to search in.', true); | ||
$this->registerArgument('needle', 'string', 'The substring to search for in the haystack.', true); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $arguments | ||
*/ | ||
protected static function evaluateCondition($arguments = null) { | ||
$haystack = strval($arguments['haystack'] ?? ''); | ||
$needle = strval($arguments['needle'] ?? ''); | ||
|
||
return substr($haystack, 0, strlen($needle)) === $needle; | ||
} | ||
} |