Skip to content

Commit

Permalink
Allow fine-grained control over the expanded chart fields (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroloux authored Jul 15, 2024
1 parent 57821bd commit bd704df
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
50 changes: 44 additions & 6 deletions src/Charts/ChartListParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ class ChartListParams
*/
public $expandEvents;

public function __construct(string $filter = null, string $tag = null, bool $expandEvents = null, bool $withValidation = false)
/**
* @var boolean
*/
public $expandValidation;

/**
* @var boolean
*/
public $expandVenueType;

public function __construct(string $filter = null, string $tag = null, bool $expandEvents = false, bool $withExpandValidation = false, bool $withExpandVenueType = false)
{
$this->filter = $filter;
$this->tag = $tag;
$this->expandEvents = $expandEvents;
$this->validation = $withValidation;
$this->expandValidation = $withExpandValidation;
$this->expandVenueType = $withExpandVenueType;
}

public function withFilter(string $filter): self
Expand All @@ -47,9 +58,23 @@ public function withExpandEvents(bool $expandEvents): self
return $this;
}

public function withExpandValidation(bool $expandValidation): self
{
$this->expandValidation = $expandValidation;
return $this;
}

/**
* @deprecated Use withExpandValidation instead
*/
public function withValidation(bool $withValidation): self
{
$this->validation = $withValidation;
return $this->withExpandValidation($withValidation);
}

public function withExpandVenueType(bool $expandVenueType): self
{
$this->expandVenueType = $expandVenueType;
return $this;
}

Expand All @@ -65,12 +90,25 @@ public function toArray(): array
$result["tag"] = $this->tag;
}

$result["expand"] = $this->expandParams();

return $result;
}

private function expandParams()
{
$result = [];

if ($this->expandEvents) {
$result["expand"] = "events";
$result[] = "events";
}

if ($this->expandValidation) {
$result[] = "validation";
}

if ($this->validation) {
$result["validation"] = "true";
if ($this->expandVenueType) {
$result[] = "venueType";
}

return $result;
Expand Down
3 changes: 2 additions & 1 deletion src/PageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Seatsio;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Query;

class PageFetcher
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function fetch($queryParams, $pageSize)
$queryParams['limit'] = $pageSize;
}
$mergedQueryParams = $this->queryParams ? array_merge($queryParams, $this->queryParams) : $queryParams;
$res = $this->client->get($this->url, ['query' => $mergedQueryParams]);
$res = $this->client->get($this->url, ['query' => Query::build($mergedQueryParams)]);
$json = GuzzleResponseDecoder::decodeToJson($res);
$mapper = SeatsioJsonMapper::create();
return $mapper->map($json, $this->pageCreator->__invoke());
Expand Down
22 changes: 19 additions & 3 deletions tests/Charts/ListAllChartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,34 @@ public function testTagAndFilter()
self::assertEquals([$chart3->key, $chart1->key], array_values($chartKeys));
}

public function testExpand()
public function testAllFieldsExpanded()
{
$chart = $this->seatsioClient->charts->create();
$event1 = $this->seatsioClient->events->create($chart->key);
$event2 = $this->seatsioClient->events->create($chart->key);

$charts = $this->seatsioClient->charts->listAll((new ChartListParams())->withExpandEvents(true));
$params = (new ChartListParams())
->withExpandEvents(true)
->withExpandValidation(true)
->withExpandVenueType(true);
$charts = $this->seatsioClient->charts->listAll($params);

$eventIds = map($charts->current()->events, function ($event) {
return $event->id;
});

self::assertEquals([$event2->id, $event1->id], array_values($eventIds));
self::assertEquals("MIXED", $charts->current()->venueType);
self::assertNotNull($charts->current()->validation);
}

public function testNoFieldsExpanded()
{
$chart = $this->seatsioClient->charts->create();

$charts = $this->seatsioClient->charts->listAll((new ChartListParams())->withExpandEvents(true));
self::assertEmpty($charts->current()->events);
self::assertNull($charts->current()->venueType);
self::assertNull($charts->current()->validation);
}

public function testWithValidation()
Expand Down

0 comments on commit bd704df

Please sign in to comment.