Skip to content

Commit

Permalink
Implemented limit() and offset() methods to support pagination (#43)
Browse files Browse the repository at this point in the history
* Implemented `limit()` and `offset()` methods to support pagination

* Code style

Co-authored-by: Garrett Massey <[email protected]>
  • Loading branch information
Plytas and gtmassey authored Jan 9, 2023
1 parent 5cbec4d commit 1602a80
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ public function withTotals(bool $useTotals = true): static
return $this;
}

public function limit(int $limit = 10_000): static
{
$this->requestData->limit = $limit;

return $this;
}

public function offset(int $offset = 0): static
{
$this->requestData->offset = $offset;

return $this;
}

/***************************************
* Process and Run Query
***************************************/
Expand Down
6 changes: 6 additions & 0 deletions src/Request/RequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function __construct(
public bool $returnPropertyQuota = true,

public bool $useTotals = false,

public int $limit = 10_000,

public int $offset = 0,
) {
}

Expand All @@ -50,6 +54,8 @@ public function toArray(): array
'metricFilter' => $this->metricFilter?->toRequest(),
'returnPropertyQuota' => $this->returnPropertyQuota,
'metricAggregations' => $this->useTotals ? [MetricAggregation::TOTAL] : [],
'limit' => $this->limit,
'offset' => $this->offset,
];
}
}
34 changes: 34 additions & 0 deletions tests/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,40 @@ public function test_custom_dimensions(): void
$this->assertEquals(['customDimension', 'browser'], $requestDimensions);
}

public function test_limit(): void
{
$analytics = Analytics::query()
->limit(5);

$this->assertInstanceOf(Analytics::class, $analytics);

/** @var RequestData $requestData */
$requestData = (new ReflectionProperty(Analytics::class, 'requestData'))->getValue($analytics);

$this->assertEquals(5, $requestData->limit);

$analytics->limit();

$this->assertEquals(10_000, $requestData->limit);
}

public function test_offset(): void
{
$analytics = Analytics::query()
->offset(5);

$this->assertInstanceOf(Analytics::class, $analytics);

/** @var RequestData $requestData */
$requestData = (new ReflectionProperty(Analytics::class, 'requestData'))->getValue($analytics);

$this->assertEquals(5, $requestData->offset);

$analytics->offset();

$this->assertEquals(0, $requestData->offset);
}

public function test_for_period(): void
{
CarbonImmutable::setTestNow(CarbonImmutable::parse('2022-10-10'));
Expand Down

0 comments on commit 1602a80

Please sign in to comment.