-
Notifications
You must be signed in to change notification settings - Fork 229
/
ContainsViewHelper.php
166 lines (156 loc) · 5.58 KB
/
ContainsViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Condition\Iterator;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject;
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
/**
* Condition ViewHelper. Renders the then-child if Iterator/array
* haystack contains needle value.
*
* ### Example:
*
* ```
* {v:condition.iterator.contains(needle: 'foo', haystack: {0: 'foo'}, then: 'yes', else: 'no')}
* ```
*/
class ContainsViewHelper extends AbstractConditionViewHelper
{
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('needle', 'mixed', 'Needle to search for in haystack', true);
$this->registerArgument('haystack', 'mixed', 'Haystack in which to look for needle', true);
$this->registerArgument(
'considerKeys',
'boolean',
'Tell whether to consider keys in the search assuming haystack is an array.',
false,
false
);
}
/**
* @param array $arguments
* @return bool
*/
protected static function evaluateCondition($arguments = null)
{
return is_array($arguments)
&& static::assertHaystackHasNeedle($arguments['haystack'], $arguments['needle'], $arguments) !== false;
}
/**
* @return mixed
*/
protected static function getNeedleAtIndex(int $index, array $arguments)
{
if (0 > $index) {
return null;
}
$haystack = $arguments['haystack'];
$asArray = [];
if (is_array($haystack)) {
$asArray = $haystack;
} elseif ($haystack instanceof ObjectStorage) {
$asArray = $haystack->toArray();
} elseif ($haystack instanceof QueryResult) {
$asArray = $haystack->toArray();
} elseif (is_string($haystack)) {
$asArray = str_split($haystack);
}
return $asArray[$index] ?? false;
}
/**
* @param array|DomainObjectInterface[]|QueryResult|ObjectStorage $haystack
* @param integer|DomainObjectInterface $needle
* @return boolean|integer
*/
protected static function assertHaystackHasNeedle($haystack, $needle, array $arguments)
{
if (is_array($haystack)) {
return static::assertHaystackIsArrayAndHasNeedle($haystack, $needle, $arguments);
} elseif ($haystack instanceof LazyObjectStorage) {
return static::assertHaystackIsObjectStorageAndHasNeedle($haystack, $needle);
} elseif ($haystack instanceof ObjectStorage) {
return static::assertHaystackIsObjectStorageAndHasNeedle($haystack, $needle);
} elseif ($haystack instanceof QueryResult) {
return static::assertHaystackIsQueryResultAndHasNeedle($haystack, $needle);
} elseif (is_scalar($haystack) && is_scalar($needle)) {
return strpos((string)$haystack, (string)$needle);
}
return false;
}
/**
* @param array|DomainObjectInterface[]|QueryResult|ObjectStorage $haystack
* @param string|int|DomainObjectInterface $needle
* @return boolean|integer
*/
protected static function assertHaystackIsQueryResultAndHasNeedle($haystack, $needle)
{
if ($needle instanceof DomainObjectInterface) {
$needle = $needle->getUid();
}
/**
* @var integer $index
* @var DomainObjectInterface $candidate
*/
foreach ($haystack as $index => $candidate) {
if ($candidate->getUid() === (integer) $needle) {
return $index;
}
}
return false;
}
/**
* @param array|DomainObjectInterface[]|QueryResult|ObjectStorage $haystack
* @param string|int|DomainObjectInterface $needle
* @return boolean|integer
*/
protected static function assertHaystackIsObjectStorageAndHasNeedle($haystack, $needle)
{
$index = 0;
if ($needle instanceof DomainObjectInterface) {
$needle = (integer) $needle->getUid();
}
/** @var DomainObjectInterface $candidate */
foreach ($haystack as $candidate) {
if ($candidate->getUid() === (integer) $needle) {
return $index;
}
$index++;
}
return false;
}
/**
* @param array&DomainObjectInterface[] $haystack
* @param integer|DomainObjectInterface $needle
* @param array $arguments
* @return boolean|integer
*/
protected static function assertHaystackIsArrayAndHasNeedle($haystack, $needle, $arguments)
{
if (!$needle instanceof DomainObjectInterface) {
if ($arguments['considerKeys']) {
$result = false !== array_search($needle, $haystack) || isset($haystack[$needle]);
} else {
/** @var integer|false $result */
$result = array_search($needle, $haystack);
}
return $result;
} else {
foreach ($haystack as $index => $straw) {
if ((integer) $straw->getUid() === (integer) $needle->getUid()) {
return $index;
}
}
}
return false;
}
}