From bd704dfb056e39afdd523b36f9002ba18940697a Mon Sep 17 00:00:00 2001 From: mroloux Date: Mon, 15 Jul 2024 11:55:40 +0200 Subject: [PATCH] Allow fine-grained control over the expanded chart fields (#121) --- src/Charts/ChartListParams.php | 50 ++++++++++++++++++++++++++---- src/PageFetcher.php | 3 +- tests/Charts/ListAllChartsTest.php | 22 +++++++++++-- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/Charts/ChartListParams.php b/src/Charts/ChartListParams.php index 20d5f33..3fbba05 100644 --- a/src/Charts/ChartListParams.php +++ b/src/Charts/ChartListParams.php @@ -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 @@ -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; } @@ -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; diff --git a/src/PageFetcher.php b/src/PageFetcher.php index 25fa706..ed355f7 100644 --- a/src/PageFetcher.php +++ b/src/PageFetcher.php @@ -3,6 +3,7 @@ namespace Seatsio; use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Query; class PageFetcher { @@ -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()); diff --git a/tests/Charts/ListAllChartsTest.php b/tests/Charts/ListAllChartsTest.php index c7fe4c2..fda1900 100644 --- a/tests/Charts/ListAllChartsTest.php +++ b/tests/Charts/ListAllChartsTest.php @@ -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()