diff --git a/.travis.yml b/.travis.yml index 1945f93..c9eb43b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: trusty language: php dist: trusty diff --git a/src/Csv.php b/src/Csv.php index ef73092..1486472 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -1054,8 +1054,20 @@ class Csv { if (preg_match('/^(.+) (' . $operators_regex . ') (.+)$/i', trim($condition), $capture)) { $field = $capture[1]; - $op = $capture[2]; + $op = strtolower($capture[2]); $value = $capture[3]; + if ($op == 'equals' && preg_match('/^(.+) is (less|greater) than or$/i', $field, $m)) { + $field = $m[1]; + $op = $m[2] == 'less' ? '<=' : '>='; + } + if ($op == 'is' && preg_match('/^(less|greater) than (.+)$/i', $value, $m)) { + $value = $m[2]; + $op = $m[1] == 'less' ? '<' : '>'; + } + if ($op == 'is' && preg_match('/^not (.+)$/i', $value, $m)) { + $value = $m[1]; + $op = '!='; + } if (preg_match('/^([\'"])(.*)([\'"])$/', $value, $capture) && $capture[1] == $capture[3]) { $value = strtr($capture[2], array( diff --git a/tests/properties/ConditionsTest.php b/tests/properties/ConditionsTest.php index c68b65d..6d4d942 100644 --- a/tests/properties/ConditionsTest.php +++ b/tests/properties/ConditionsTest.php @@ -21,21 +21,93 @@ class ConditionsTest extends BaseClass { ]); } - public function testRating() { - $this->csv->conditions = 'rating < 3'; - $this->_compareWithExpected([ + public function testRatingEquals() { + $rating_of_3 = [ + 'The Last Templar', + 'The Broker (Paperback)', + 'Without Blood (Paperback)', + ]; + $this->csv->conditions = 'rating = 3'; + $this->_compareWithExpected($rating_of_3); + $this->csv->conditions = 'rating is 3'; + $this->_compareWithExpected($rating_of_3); + $this->csv->conditions = 'rating equals 3'; + $this->_compareWithExpected($rating_of_3); + } + + public function testRatingNotEquals() { + $rating_not_4 = [ 'The Killing Kind', 'The Third Secret', - ]); + 'The Last Templar', + 'The Traveller', + 'Prey', + 'The Broker (Paperback)', + 'Without Blood (Paperback)', + 'State of Fear (Paperback)', + 'Digital Fortress : A Thriller (Mass Market Paperback)', + 'Angels & Demons (Mass Market Paperback)', + ]; +// $this->csv->conditions = 'rating != 4'; +// $this->_compareWithExpected($rating_not_4); + $this->csv->conditions = 'rating is not 4'; + $this->_compareWithExpected($rating_not_4); +// $this->csv->conditions = 'rating does not contain 4'; +// $this->_compareWithExpected($rating_not_4); + } - $this->csv->conditions = 'rating >= 5'; - $this->_compareWithExpected([ + public function testRatingLessThan() { + $less_than_1 = [ + 'The Killing Kind', + 'The Third Secret', + ]; + $this->csv->conditions = 'rating < 1'; + $this->_compareWithExpected($less_than_1); + $this->csv->conditions = 'rating is less than 1'; + $this->_compareWithExpected($less_than_1); + } + + public function testRatingLessOrEquals() { + $less_or_equals_1 = [ + 'The Killing Kind', + 'The Third Secret', + ]; + $this->csv->conditions = 'rating <= 1'; + $this->_compareWithExpected($less_or_equals_1); + $this->csv->conditions = 'rating is less than or equals 1'; + $this->_compareWithExpected($less_or_equals_1); + } + + public function testRatingGreaterThan() { + $greater_4 = [ 'The Traveller', 'Prey', 'State of Fear (Paperback)', 'Digital Fortress : A Thriller (Mass Market Paperback)', 'Angels & Demons (Mass Market Paperback)', - ]); + ]; + $this->csv->conditions = 'rating > 4'; + $this->_compareWithExpected($greater_4); + $this->csv->conditions = 'rating is greater than 4'; + $this->_compareWithExpected($greater_4); + } + + public function testRatingGreaterOrEquals() { + $greater_or_equal_4 = [ + 'The Traveller', + 'Crisis Four', + 'Prey', + 'State of Fear (Paperback)', + 'The Rule of Four (Paperback)', + 'Deception Point (Paperback)', + 'Digital Fortress : A Thriller (Mass Market Paperback)', + 'Angels & Demons (Mass Market Paperback)', + 'The Da Vinci Code (Hardcover)', + ]; + $this->csv->conditions = 'rating >= 4'; + $this->_compareWithExpected($greater_or_equal_4); + $this->csv->conditions = 'rating is greater than or equals 4'; + $this->_compareWithExpected($greater_or_equal_4); } public function testTitleContainsSecretOrCode() {