Skip to content

Commit e955b21

Browse files
String function improvements and additions
TRIM's second value is optional now. It also accepts a string with characters to remove at the beginnen and the end, instead of just the type integer. Added LTRIM support Added RTRIM support
1 parent 3ffdf51 commit e955b21

File tree

4 files changed

+150
-18
lines changed

4 files changed

+150
-18
lines changed

docs/api/functions.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,25 @@ The following functions are directly supported in FluentAql.
135135

136136

137137
### String functions
138-
| Description | AQL Function |
139-
| :-------------------- | :-------------------------------------------------------------------------------------------- |
140-
| concat(...$arguments) | [CONCAT(value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat) |
141-
| concatSeparator($separator, $values) | [CONCAT_SEPARATOR(separator, value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat_separator) |
142-
| levenshteinDistance($value1, $value2) | [LEVENSHTEIN_DISTANCE(value1, value2)](https://www.arangodb.com/docs/stable/aql/functions-string.html#levenshtein_distance) |
143-
| lower($value)| [LOWER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#lower) |
144-
| regexMatches($text, $regex, $caseInsensitive)| [REGEX_MATCHES(text, regex, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_matches) |
145-
| regexReplace($text, $regex, $replacement, $caseInsensitive)| [REGEX_REPLACE(text, search, replacement, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_replace) |
146-
| regexSplit($text, $splitExpression, $caseInsensitive, $limit)| [REGEX_SPLIT(text, splitExpression, caseInsensitive, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_split) |
147-
| regexTest($text, $search, $caseInsensitive)| [REGEX_TEST(text, search, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_test) |
148-
| split($value, $separator, $limit)| [SPLIT(value, separator, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#split) |
149-
| substitute($text, $search, $replace, $limit)| [SUBSTITUTE(value, search, replace, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
150-
| substring($value, $offset, $length)| [SUBSTRING(value, offset, length)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
151-
| tokens($input, $analyzer)| [TOKENS(input, analyzer)](https://www.arangodb.com/docs/stable/aql/functions-string.html#tokens) |
152-
| trim($value, $type)| [TRIM(value, type)](https://www.arangodb.com/docs/stable/aql/functions-string.html#trim) |
153-
| upper($value)| [UPPER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#upper) |
154-
| uuid()| [UUID()](https://www.arangodb.com/docs/stable/aql/functions-string.html#uuid) |
138+
| Description | AQL Function |
139+
|:---------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------|
140+
| concat(...$arguments) | [CONCAT(value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat) |
141+
| concatSeparator($separator, $values) | [CONCAT_SEPARATOR(separator, value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat_separator) |
142+
| levenshteinDistance($value1, $value2) | [LEVENSHTEIN_DISTANCE(value1, value2)](https://www.arangodb.com/docs/stable/aql/functions-string.html#levenshtein_distance) |
143+
| lower($value) | [LOWER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#lower) |
144+
| ltrim($value, $char) | [LTRIM(value, char)](https://www.arangodb.com/docs/stable/aql/functions-string.html#ltrim) |
145+
| regexMatches($text, $regex, $caseInsensitive) | [REGEX_MATCHES(text, regex, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_matches) |
146+
| regexReplace($text, $regex, $replacement, $caseInsensitive) | [REGEX_REPLACE(text, search, replacement, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_replace) |
147+
| regexSplit($text, $splitExpression, $caseInsensitive, $limit) | [REGEX_SPLIT(text, splitExpression, caseInsensitive, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_split) |
148+
| regexTest($text, $search, $caseInsensitive) | [REGEX_TEST(text, search, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_test) |
149+
| rtrim($value, $char) | [RTRIM(value, char)](https://www.arangodb.com/docs/stable/aql/functions-string.html#rtrim) |
150+
| split($value, $separator, $limit) | [SPLIT(value, separator, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#split) |
151+
| substitute($text, $search, $replace, $limit) | [SUBSTITUTE(value, search, replace, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
152+
| substring($value, $offset, $length) | [SUBSTRING(value, offset, length)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
153+
| tokens($input, $analyzer) | [TOKENS(input, analyzer)](https://www.arangodb.com/docs/stable/aql/functions-string.html#tokens) |
154+
| trim($value, $type) | [TRIM(value, type)](https://www.arangodb.com/docs/stable/aql/functions-string.html#trim) |
155+
| upper($value) | [UPPER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#upper) |
156+
| uuid() | [UUID()](https://www.arangodb.com/docs/stable/aql/functions-string.html#uuid) |
155157

156158

157159
### Type functions

src/AQL/HasStringFunctions.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ public function lower(
8282
return new FunctionExpression('LOWER', [$value]);
8383
}
8484

85+
/**
86+
* Return the string value with whitespace (or supplied characters) stripped from the start.
87+
*
88+
* @link https://www.arangodb.com/docs/stable/aql/functions-string.html#ltrim
89+
*/
90+
public function ltrim(
91+
string|object $value,
92+
null|string|object $char = null
93+
): FunctionExpression {
94+
$arguments = [
95+
'value' => $value,
96+
'char' => $char,
97+
];
98+
99+
return new FunctionExpression('LTRIM', $arguments);
100+
}
101+
85102
/**
86103
* Check whether the string search is contained in the string text.
87104
*
@@ -153,6 +170,23 @@ public function regexTest(
153170
return new FunctionExpression('REGEX_TEST', [$text, $search, $caseInsensitive]);
154171
}
155172

173+
/**
174+
* Return the string value with whitespace (or supplied characters) stripped from the end.
175+
*
176+
* @link https://www.arangodb.com/docs/stable/aql/functions-string.html#rtrim
177+
*/
178+
public function rtrim(
179+
string|object $value,
180+
null|string|object $char = null
181+
): FunctionExpression {
182+
$arguments = [
183+
'value' => $value,
184+
'char' => $char,
185+
];
186+
187+
return new FunctionExpression('RTRIM', $arguments);
188+
}
189+
156190
/**
157191
* Split the given string text into a list of strings, using the separator.
158192
*
@@ -242,7 +276,7 @@ public function tokens(
242276
*/
243277
public function trim(
244278
string|object $value,
245-
int|object $type
279+
null|string|int|object $type = null
246280
): FunctionExpression {
247281
$arguments = [
248282
'value' => $value,

src/Traits/NormalizesStringFunctions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ protected function normalizeLower(QueryBuilder $queryBuilder): void
5353
$this->normalizeStrings($queryBuilder);
5454
}
5555

56+
protected function normalizeLtrim(QueryBuilder $queryBuilder): void
57+
{
58+
$this->parameters['value'] = $queryBuilder->normalizeArgument(
59+
$this->parameters['value'],
60+
['Query', 'Reference', 'Bind']
61+
);
62+
63+
$this->parameters['char'] = $queryBuilder->normalizeArgument(
64+
$this->parameters['char'],
65+
['Query', 'Reference', 'Bind']
66+
);
67+
}
68+
5669
protected function normalizeRegexMatches(QueryBuilder $queryBuilder): void
5770
{
5871
$this->parameters[0] = $queryBuilder->normalizeArgument(
@@ -137,6 +150,19 @@ protected function normalizeRegexTest(QueryBuilder $queryBuilder): void
137150
);
138151
}
139152

153+
protected function normalizeRtrim(QueryBuilder $queryBuilder): void
154+
{
155+
$this->parameters['value'] = $queryBuilder->normalizeArgument(
156+
$this->parameters['value'],
157+
['Query', 'Reference', 'Bind']
158+
);
159+
160+
$this->parameters['char'] = $queryBuilder->normalizeArgument(
161+
$this->parameters['char'],
162+
['Query', 'Reference', 'Bind']
163+
);
164+
}
165+
140166
protected function normalizeSplit(QueryBuilder $queryBuilder): void
141167
{
142168
$this->parameters['value'] = $queryBuilder->normalizeArgument(

tests/Unit/AQL/StringFunctionsTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ public function testLevenshteinDistance()
7575
);
7676
}
7777

78+
public function testLeftTrim()
79+
{
80+
$qb = new QueryBuilder();
81+
$qb->return($qb->ltrim("/ Lörem ipsüm, DOLOR SIT Ämet./"));
82+
self::assertEquals(
83+
'RETURN LTRIM(@'
84+
. $qb->getQueryId() . '_1, null)',
85+
$qb->get()->query
86+
);
87+
}
88+
89+
public function testLeftTrimWithChar()
90+
{
91+
$qb = new QueryBuilder();
92+
$qb->return($qb->ltrim("/ Lörem ipsüm, DOLOR SIT Ämet./", '/'));
93+
self::assertEquals(
94+
'RETURN LTRIM(@'
95+
. $qb->getQueryId() . '_1, @'
96+
. $qb->getQueryId() . '_2)',
97+
$qb->get()->query
98+
);
99+
}
100+
78101
public function testRegexMatches()
79102
{
80103
$qb = new QueryBuilder();
@@ -124,6 +147,29 @@ public function testRegexTest()
124147
);
125148
}
126149

150+
public function testRightTrim()
151+
{
152+
$qb = new QueryBuilder();
153+
$qb->return($qb->rtrim("/ Lörem ipsüm, DOLOR SIT Ämet./"));
154+
self::assertEquals(
155+
'RETURN RTRIM(@'
156+
. $qb->getQueryId() . '_1, null)',
157+
$qb->get()->query
158+
);
159+
}
160+
161+
public function testRightTrimWithChar()
162+
{
163+
$qb = new QueryBuilder();
164+
$qb->return($qb->rtrim("/ Lörem ipsüm, DOLOR SIT Ämet./", '/'));
165+
self::assertEquals(
166+
'RETURN RTRIM(@'
167+
. $qb->getQueryId() . '_1, @'
168+
. $qb->getQueryId() . '_2)',
169+
$qb->get()->query
170+
);
171+
}
172+
127173
public function testSplit()
128174
{
129175
$qb = new QueryBuilder();
@@ -172,7 +218,19 @@ public function testTokens()
172218
);
173219
}
174220

221+
175222
public function testTrim()
223+
{
224+
$qb = new QueryBuilder();
225+
$qb->return($qb->trim(" Lörem ipsüm, DOLOR SIT Ämet."));
226+
self::assertEquals(
227+
'RETURN TRIM(@'
228+
. $qb->getQueryId() . '_1, null)',
229+
$qb->get()->query
230+
);
231+
}
232+
233+
public function testTrimWithType()
176234
{
177235
$qb = new QueryBuilder();
178236
$qb->return($qb->trim(" Lörem ipsüm, DOLOR SIT Ämet.", 1));
@@ -183,6 +241,18 @@ public function testTrim()
183241
);
184242
}
185243

244+
public function testTrimWithChar()
245+
{
246+
$qb = new QueryBuilder();
247+
$qb->return($qb->trim("/ Lörem ipsüm, DOLOR SIT Ämet./", '/'));
248+
self::assertEquals(
249+
'RETURN TRIM(@'
250+
. $qb->getQueryId() . '_1, @'
251+
. $qb->getQueryId() . '_2)',
252+
$qb->get()->query
253+
);
254+
}
255+
186256
public function testLower()
187257
{
188258
$qb = new QueryBuilder();

0 commit comments

Comments
 (0)