Skip to content

Commit

Permalink
String function improvements and additions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LaravelFreelancerNL committed Jan 27, 2022
1 parent 3ffdf51 commit e955b21
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 18 deletions.
36 changes: 19 additions & 17 deletions docs/api/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,25 @@ The following functions are directly supported in FluentAql.


### String functions
| Description | AQL Function |
| :-------------------- | :-------------------------------------------------------------------------------------------- |
| concat(...$arguments) | [CONCAT(value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat) |
| concatSeparator($separator, $values) | [CONCAT_SEPARATOR(separator, value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat_separator) |
| levenshteinDistance($value1, $value2) | [LEVENSHTEIN_DISTANCE(value1, value2)](https://www.arangodb.com/docs/stable/aql/functions-string.html#levenshtein_distance) |
| lower($value)| [LOWER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#lower) |
| regexMatches($text, $regex, $caseInsensitive)| [REGEX_MATCHES(text, regex, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_matches) |
| regexReplace($text, $regex, $replacement, $caseInsensitive)| [REGEX_REPLACE(text, search, replacement, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_replace) |
| regexSplit($text, $splitExpression, $caseInsensitive, $limit)| [REGEX_SPLIT(text, splitExpression, caseInsensitive, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_split) |
| regexTest($text, $search, $caseInsensitive)| [REGEX_TEST(text, search, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_test) |
| split($value, $separator, $limit)| [SPLIT(value, separator, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#split) |
| substitute($text, $search, $replace, $limit)| [SUBSTITUTE(value, search, replace, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
| substring($value, $offset, $length)| [SUBSTRING(value, offset, length)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
| tokens($input, $analyzer)| [TOKENS(input, analyzer)](https://www.arangodb.com/docs/stable/aql/functions-string.html#tokens) |
| trim($value, $type)| [TRIM(value, type)](https://www.arangodb.com/docs/stable/aql/functions-string.html#trim) |
| upper($value)| [UPPER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#upper) |
| uuid()| [UUID()](https://www.arangodb.com/docs/stable/aql/functions-string.html#uuid) |
| Description | AQL Function |
|:---------------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------|
| concat(...$arguments) | [CONCAT(value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat) |
| concatSeparator($separator, $values) | [CONCAT_SEPARATOR(separator, value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat_separator) |
| levenshteinDistance($value1, $value2) | [LEVENSHTEIN_DISTANCE(value1, value2)](https://www.arangodb.com/docs/stable/aql/functions-string.html#levenshtein_distance) |
| lower($value) | [LOWER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#lower) |
| ltrim($value, $char) | [LTRIM(value, char)](https://www.arangodb.com/docs/stable/aql/functions-string.html#ltrim) |
| regexMatches($text, $regex, $caseInsensitive) | [REGEX_MATCHES(text, regex, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_matches) |
| regexReplace($text, $regex, $replacement, $caseInsensitive) | [REGEX_REPLACE(text, search, replacement, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_replace) |
| regexSplit($text, $splitExpression, $caseInsensitive, $limit) | [REGEX_SPLIT(text, splitExpression, caseInsensitive, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_split) |
| regexTest($text, $search, $caseInsensitive) | [REGEX_TEST(text, search, caseInsensitive)](https://www.arangodb.com/docs/stable/aql/functions-string.html#regex_test) |
| rtrim($value, $char) | [RTRIM(value, char)](https://www.arangodb.com/docs/stable/aql/functions-string.html#rtrim) |
| split($value, $separator, $limit) | [SPLIT(value, separator, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#split) |
| substitute($text, $search, $replace, $limit) | [SUBSTITUTE(value, search, replace, limit)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
| substring($value, $offset, $length) | [SUBSTRING(value, offset, length)](https://www.arangodb.com/docs/stable/aql/functions-string.html#substitute) |
| tokens($input, $analyzer) | [TOKENS(input, analyzer)](https://www.arangodb.com/docs/stable/aql/functions-string.html#tokens) |
| trim($value, $type) | [TRIM(value, type)](https://www.arangodb.com/docs/stable/aql/functions-string.html#trim) |
| upper($value) | [UPPER(value)](https://www.arangodb.com/docs/stable/aql/functions-string.html#upper) |
| uuid() | [UUID()](https://www.arangodb.com/docs/stable/aql/functions-string.html#uuid) |


### Type functions
Expand Down
36 changes: 35 additions & 1 deletion src/AQL/HasStringFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ public function lower(
return new FunctionExpression('LOWER', [$value]);
}

/**
* Return the string value with whitespace (or supplied characters) stripped from the start.
*
* @link https://www.arangodb.com/docs/stable/aql/functions-string.html#ltrim
*/
public function ltrim(
string|object $value,
null|string|object $char = null
): FunctionExpression {
$arguments = [
'value' => $value,
'char' => $char,
];

return new FunctionExpression('LTRIM', $arguments);
}

/**
* Check whether the string search is contained in the string text.
*
Expand Down Expand Up @@ -153,6 +170,23 @@ public function regexTest(
return new FunctionExpression('REGEX_TEST', [$text, $search, $caseInsensitive]);
}

/**
* Return the string value with whitespace (or supplied characters) stripped from the end.
*
* @link https://www.arangodb.com/docs/stable/aql/functions-string.html#rtrim
*/
public function rtrim(
string|object $value,
null|string|object $char = null
): FunctionExpression {
$arguments = [
'value' => $value,
'char' => $char,
];

return new FunctionExpression('RTRIM', $arguments);
}

/**
* Split the given string text into a list of strings, using the separator.
*
Expand Down Expand Up @@ -242,7 +276,7 @@ public function tokens(
*/
public function trim(
string|object $value,
int|object $type
null|string|int|object $type = null
): FunctionExpression {
$arguments = [
'value' => $value,
Expand Down
26 changes: 26 additions & 0 deletions src/Traits/NormalizesStringFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ protected function normalizeLower(QueryBuilder $queryBuilder): void
$this->normalizeStrings($queryBuilder);
}

protected function normalizeLtrim(QueryBuilder $queryBuilder): void
{
$this->parameters['value'] = $queryBuilder->normalizeArgument(
$this->parameters['value'],
['Query', 'Reference', 'Bind']
);

$this->parameters['char'] = $queryBuilder->normalizeArgument(
$this->parameters['char'],
['Query', 'Reference', 'Bind']
);
}

protected function normalizeRegexMatches(QueryBuilder $queryBuilder): void
{
$this->parameters[0] = $queryBuilder->normalizeArgument(
Expand Down Expand Up @@ -137,6 +150,19 @@ protected function normalizeRegexTest(QueryBuilder $queryBuilder): void
);
}

protected function normalizeRtrim(QueryBuilder $queryBuilder): void
{
$this->parameters['value'] = $queryBuilder->normalizeArgument(
$this->parameters['value'],
['Query', 'Reference', 'Bind']
);

$this->parameters['char'] = $queryBuilder->normalizeArgument(
$this->parameters['char'],
['Query', 'Reference', 'Bind']
);
}

protected function normalizeSplit(QueryBuilder $queryBuilder): void
{
$this->parameters['value'] = $queryBuilder->normalizeArgument(
Expand Down
70 changes: 70 additions & 0 deletions tests/Unit/AQL/StringFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ public function testLevenshteinDistance()
);
}

public function testLeftTrim()
{
$qb = new QueryBuilder();
$qb->return($qb->ltrim("/ Lörem ipsüm, DOLOR SIT Ämet./"));
self::assertEquals(
'RETURN LTRIM(@'
. $qb->getQueryId() . '_1, null)',
$qb->get()->query
);
}

public function testLeftTrimWithChar()
{
$qb = new QueryBuilder();
$qb->return($qb->ltrim("/ Lörem ipsüm, DOLOR SIT Ämet./", '/'));
self::assertEquals(
'RETURN LTRIM(@'
. $qb->getQueryId() . '_1, @'
. $qb->getQueryId() . '_2)',
$qb->get()->query
);
}

public function testRegexMatches()
{
$qb = new QueryBuilder();
Expand Down Expand Up @@ -124,6 +147,29 @@ public function testRegexTest()
);
}

public function testRightTrim()
{
$qb = new QueryBuilder();
$qb->return($qb->rtrim("/ Lörem ipsüm, DOLOR SIT Ämet./"));
self::assertEquals(
'RETURN RTRIM(@'
. $qb->getQueryId() . '_1, null)',
$qb->get()->query
);
}

public function testRightTrimWithChar()
{
$qb = new QueryBuilder();
$qb->return($qb->rtrim("/ Lörem ipsüm, DOLOR SIT Ämet./", '/'));
self::assertEquals(
'RETURN RTRIM(@'
. $qb->getQueryId() . '_1, @'
. $qb->getQueryId() . '_2)',
$qb->get()->query
);
}

public function testSplit()
{
$qb = new QueryBuilder();
Expand Down Expand Up @@ -172,7 +218,19 @@ public function testTokens()
);
}


public function testTrim()
{
$qb = new QueryBuilder();
$qb->return($qb->trim(" Lörem ipsüm, DOLOR SIT Ämet."));
self::assertEquals(
'RETURN TRIM(@'
. $qb->getQueryId() . '_1, null)',
$qb->get()->query
);
}

public function testTrimWithType()
{
$qb = new QueryBuilder();
$qb->return($qb->trim(" Lörem ipsüm, DOLOR SIT Ämet.", 1));
Expand All @@ -183,6 +241,18 @@ public function testTrim()
);
}

public function testTrimWithChar()
{
$qb = new QueryBuilder();
$qb->return($qb->trim("/ Lörem ipsüm, DOLOR SIT Ämet./", '/'));
self::assertEquals(
'RETURN TRIM(@'
. $qb->getQueryId() . '_1, @'
. $qb->getQueryId() . '_2)',
$qb->get()->query
);
}

public function testLower()
{
$qb = new QueryBuilder();
Expand Down

0 comments on commit e955b21

Please sign in to comment.