Skip to content

Commit

Permalink
Merge pull request #47 from JAK0TA/add-start-with-viewhelper
Browse files Browse the repository at this point in the history
feat: add a view helper to check what the string starts with
  • Loading branch information
tlueder authored Feb 14, 2024
2 parents c049885 + 299c0f3 commit bd8a409
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Classes/ViewHelpers/Condition/String/StartWithViewHelper.php
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;
}
}

0 comments on commit bd8a409

Please sign in to comment.