Skip to content

Commit

Permalink
Added new where operators: equal, notEqual, regExp, notRegExp, startW…
Browse files Browse the repository at this point in the history
…ith, notStartWith, endWith, notEndWith
  • Loading branch information
ivopetkov committed Jan 14, 2017
1 parent 9687afe commit 3287c16
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,38 @@ public function execute($commands)
if ($value === $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'regexp') {
if (preg_match("/" . $conditionData[1] . "/", $value) === 1) {
} elseif ($conditionData[0] === 'equal') {
if ($value === $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'notEqual') {
if ($value !== $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'regexp' || $conditionData[0] === 'regExp') {
if (preg_match('/' . $conditionData[1] . '/', $value) === 1) {
return true;
}
} elseif ($conditionData[0] === 'notRegExp') {
if (preg_match('/' . $conditionData[1] . '/', $value) === 0) {
return true;
}
} elseif ($conditionData[0] === 'startsWith') {
} elseif ($conditionData[0] === 'startsWith' || $conditionData[0] === 'startWith') {
if (substr($value, 0, strlen($conditionData[1])) === $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'notStartWith') {
if (substr($value, 0, strlen($conditionData[1])) !== $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'endWith') {
if (substr($value, -strlen($conditionData[1])) === $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'notEndWith') {
if (substr($value, -strlen($conditionData[1])) !== $conditionData[1]) {
return true;
}
} elseif ($conditionData[0] === 'search') {
if (strpos(strtolower($value), strtolower($conditionData[1])) !== false) {
return true;
Expand Down Expand Up @@ -360,7 +384,7 @@ public function execute($commands)
$whereData[$whereDataItem[0]] = [];
}
$whereOperator = isset($whereDataItem[2]) ? $whereDataItem[2] : '==';
if ($whereOperator !== '==' && $whereOperator !== 'regexp' && $whereOperator !== 'search' && $whereOperator !== 'startsWith') {
if (array_search($whereOperator, ['==', 'regexp', 'search', 'startsWith', 'equal', 'notEqual', 'regExp', 'notRegExp', 'startWith', 'notStartWith', 'endWith', 'notEndWith']) === false) {
throw new \InvalidArgumentException('invalid where operator - ' . $whereOperator);
}
if (is_string($whereDataItem[1])) {
Expand Down
183 changes: 183 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,187 @@ public function testRemoveOldMetadata()
$this->removeDataDir();
}

/**
*
*/
public function testWhereOperators()
{
$this->removeDataDir();
$objectStorage = $this->getInstance();

$objectStorage->set(
[
'key' => 'data1',
'body' => ''
]
);
$objectStorage->set(
[
'key' => 'data2',
'body' => ''
]
);
$objectStorage->set(
[
'key' => 'data3',
'body' => ''
]
);

// Test equal
$result = $objectStorage->search(
[
'where' => [
['key', 'data1', 'equal']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data1'
)
));

// Test notEqual
$result = $objectStorage->search(
[
'where' => [
['key', 'data1', 'notEqual']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data2'
),
1 =>
array(
'key' => 'data3'
)
));

// Test startWith
$result = $objectStorage->search(
[
'where' => [
['key', 'data', 'startWith']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data1'
),
1 =>
array(
'key' => 'data2'
),
2 =>
array(
'key' => 'data3'
)
));

// Test notStartWith
$result = $objectStorage->search(
[
'where' => [
['key', 'data2', 'notStartWith']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data1'
),
1 =>
array(
'key' => 'data3'
)
));


// Test endWith
$result = $objectStorage->search(
[
'where' => [
['key', '2', 'endWith']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data2'
)
));

// Test notEndWith
$result = $objectStorage->search(
[
'where' => [
['key', '2', 'notEndWith']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data1'
),
1 =>
array(
'key' => 'data3'
)
));

// Test regExp
$result = $objectStorage->search(
[
'where' => [
['key', '1', 'regExp']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data1'
)
));

// Test notRegExp
$result = $objectStorage->search(
[
'where' => [
['key', '1', 'notRegExp']
],
'result' => ['key']
]
);
$this->assertTrue($result === array(
0 =>
array(
'key' => 'data2'
),
1 =>
array(
'key' => 'data3'
)
));

$this->removeDataDir();
}

}

0 comments on commit 3287c16

Please sign in to comment.