Skip to content

Commit

Permalink
Implement "equalTo" for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Jul 20, 2016
1 parent 60d5f09 commit a60e4d3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 1.1 - 2016-07-20

* Adds support for "equalTo" queries

## 1.0.1 - 2016-07-16

* Enables the usage of egeleon/http-adapter ^1.0 in addition to ^0.8.

## 1.0 - 2016-07-15

* Removed deprecated methods and revisited the code and documentation.
Expand Down
32 changes: 30 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Query
*/
private $endAt;

/**
* @var bool|int|string
*/
private $equalTo;

/**
* Whether the query is shallow or not.
*
Expand Down Expand Up @@ -160,6 +165,20 @@ public function shallow($shallow)
return $this;
}

/**
* Return items equal to the specified key or value, depending on the order-by method chosen.
*
* @param int|string|bool $equalTo
*
* @return $this
*/
public function equalTo($equalTo)
{
$this->equalTo = $equalTo;

return $this;
}

/**
* Returns an array representation of this query.
*
Expand All @@ -181,16 +200,25 @@ public function toArray()
}

if ($this->startAt) {
$result['startAt'] = sprintf('"%s"', $this->startAt);
$result['startAt'] = $this->wrapValue($this->startAt);
}

if ($this->endAt) {
$result['endAt'] = sprintf('"%s"', $this->endAt);
$result['endAt'] = $this->wrapValue($this->endAt);
}

if ($this->equalTo) {
$result['equalTo'] = $this->wrapValue($this->equalTo);
}

return $result;
}

private function wrapValue($value)
{
return is_string($value) ? sprintf('"%s"', $value) : $value;
}

public function __toString()
{
return http_build_query($this->toArray(), null, '&', PHP_QUERY_RFC3986);
Expand Down
18 changes: 18 additions & 0 deletions tests/QueryIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,22 @@ public function testShallow()

$this->tearDownQueryData(__FUNCTION__);
}

public function testEqualTo()
{
$this->recorder->insertTape(__FUNCTION__);
$this->recorder->startRecording();

$this->setUpQueryData(__FUNCTION__);

$this->query
->orderByKey()
->equalTo('c');

$result = $this->firebase->query($this->getLocation(__FUNCTION__), $this->query);

$this->assertEquals(['c' => ['first_name' => 'c', 'height' => 2]], $result);

$this->tearDownQueryData(__FUNCTION__);
}
}
12 changes: 12 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ public function testShallow()

$this->assertStringEndsWith('shallow=true', (string) $this->query);
}

public function testEqualTo()
{
$this->query->equalTo(2);
$this->assertStringEndsWith('equalTo=2', (string) $this->query);
}

public function testStringsAreWrappedWithQuotationMarks()
{
$this->query->equalTo('string');
$this->assertStringEndsWith('equalTo=%22string%22', (string) $this->query);
}
}

0 comments on commit a60e4d3

Please sign in to comment.