InArrayViewHelper.php 1.29 KB
Newer Older
1
<?php
2
namespace T3o\Typo3Roadmap\ViewHelpers\Condition;
3

4
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
5
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
6
7

class InArrayViewHelper extends AbstractConditionViewHelper
8
9
{
    /**
10
     * Initializes the "then" and "else" arguments
11
     */
12
    public function initializeArguments()
13
    {
14
15
16
17
        parent::initializeArguments();
        $this->registerArgument('needle', 'mixed', 'Value to be searched for.', true);
        $this->registerArgument('array', 'array', 'Array to be searched.', false);
        $this->registerArgument('arrayPath', 'string', 'Property path to apply to each element to get actual value to be compared to.', false, null);
18
19
20
    }

    /**
21
22
     * @param array|NULL $arguments
     * @return boolean
23
     */
24
    protected static function evaluateCondition($arguments = null)
25
    {
26
27
28
29
        foreach ($arguments['array'] as $key => $value) {
            if ($arguments['arrayPath'] !== null) {
                $arrayValue = ObjectAccess::getPropertyPath($value, $arguments['arrayPath']);
                if ($arguments['needle'] === $arrayValue) {
30
31
                    return true;
                }
32
            } elseif ($arguments['needle'] === $value) {
33
34
35
36
37
38
                return true;
            }
        }

        return false;
    }
39
}