Skip to content

Commit b4f8977

Browse files
committed
Collection querying: Add andWhere GreaterThan/LessThan()
1 parent bd055e0 commit b4f8977

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/Collections/CollectionQuery.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ public function select(string ...$columns): self
6060
* Adds a filter condition with an "and" clause.
6161
*
6262
* @param string $columnName
63+
* @param string $operator
6364
* @param bool|int|float|string|array $expectedValue
6465
* @return $this
6566
*/
66-
public function andWhereEquals(string $columnName, $expectedValue): self
67+
protected function andWhere(string $columnName, $operator, $expectedValue): self
6768
{
6869
if (!empty($this->filter)) {
6970
$this->filter .= " and ";
@@ -74,17 +75,53 @@ public function andWhereEquals(string $columnName, $expectedValue): self
7475
}
7576

7677
if (is_integer($expectedValue) || is_float($expectedValue)) {
77-
$this->filter .= "{$columnName} eq {$expectedValue}";
78+
$this->filter .= "{$columnName} {$operator} {$expectedValue}";
7879
} else if (is_string($expectedValue)) {
79-
$this->filter .= "{$columnName} eq '{$expectedValue}'";
80-
} else if (is_array($expectedValue)) {
80+
$this->filter .= "{$columnName} {$operator} '{$expectedValue}'";
81+
} else if (($operator === "eq" || $operator === "equals" || $operator === "oneOf") && is_array($expectedValue)) {
8182
$oneOfList = "'" . implode("','", $expectedValue) . "'";
8283
$this->filter .= "{$columnName} oneOf({$oneOfList})";
8384
}
8485

8586
return $this;
8687
}
8788

89+
/**
90+
* Adds a filter condition with an "and equals" clause.
91+
*
92+
* @param string $columnName
93+
* @param bool|int|float|string|array $expectedValue
94+
* @return $this
95+
*/
96+
public function andWhereEquals(string $columnName, $expectedValue): self
97+
{
98+
return $this->andWhere($columnName, "eq", $expectedValue);
99+
}
100+
101+
/**
102+
* Adds a filter condition with an "and greater than" clause.
103+
*
104+
* @param string $columnName
105+
* @param int|float $expectedValue
106+
* @return $this
107+
*/
108+
public function andWhereGreaterThan(string $columnName, $expectedValue): self
109+
{
110+
return $this->andWhere($columnName, "gt", $expectedValue);
111+
}
112+
113+
/**
114+
* Adds a filter condition with an "and less than" clause.
115+
*
116+
* @param string $columnName
117+
* @param int|float $expectedValue
118+
* @return $this
119+
*/
120+
public function andWhereLessThan(string $columnName, $expectedValue): self
121+
{
122+
return $this->andWhere($columnName, "lt", $expectedValue);
123+
}
124+
88125
/**
89126
* Set query limit ($top).
90127
*

tests/Collections/CollectionQueryTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function testGetQueryString()
9393
* @depends testLimit
9494
* @depends testOffset
9595
*/
96-
public function testFilter()
96+
public function testFilterEquals()
9797
{
9898
$cl = new Client(new Config([]));
9999
$dc = new CollectionQueryTestDummyCollection($cl);
@@ -107,6 +107,23 @@ public function testFilter()
107107
$cq->getQueryString()
108108
);
109109
}
110+
111+
/**
112+
* @depends testFilterEquals
113+
*/
114+
public function testFilterGreaterThanLessThan()
115+
{
116+
$cl = new Client(new Config([]));
117+
$dc = new CollectionQueryTestDummyCollection($cl);
118+
$cq = new CollectionQuery($dc);
119+
120+
$cq->andWhereGreaterThan('number', 123);
121+
$cq->andWhereLessThan('number', 456);
122+
123+
$this->assertSame("\$filter=number gt 123 and number lt 456",
124+
$cq->getQueryString()
125+
);
126+
}
110127
}
111128

112129
class CollectionQueryTestDummyCollection extends Collection

0 commit comments

Comments
 (0)