diff --git a/composer.json b/composer.json index d0a4fa7d..21dd090e 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,11 @@ "CultuurNet\\SearchV3\\": "src" } }, + "autoload-dev": { + "psr-4": { + "CultuurNet\\SearchV3\\": "test" + } + }, "require": { "php": "^7.1", "guzzlehttp/guzzle": "~6.0", diff --git a/src/Parameter/CalendarSummary.php b/src/Parameter/CalendarSummary.php new file mode 100644 index 00000000..b9c4ff36 --- /dev/null +++ b/src/Parameter/CalendarSummary.php @@ -0,0 +1,36 @@ +format = $format; + } + + public function getKey(): string + { + return 'embedCalendarSummaries'; + } + + public function getValue(): string + { + return $this->format->getCombined(); + } + + public function allowsMultiple(): bool + { + return true; + } +} diff --git a/src/ValueObjects/CalendarSummary.php b/src/ValueObjects/CalendarSummary.php index 6abdfd9a..5c95c128 100644 --- a/src/ValueObjects/CalendarSummary.php +++ b/src/ValueObjects/CalendarSummary.php @@ -8,25 +8,6 @@ final class CalendarSummary { - private const LANGUAGES = [ - 'nl', - 'fr', - 'de', - 'en', - ]; - - private const TYPES = [ - 'text', - 'html', - ]; - - private const FORMATS = [ - 'xs', - 'sm', - 'md', - 'lg', - ]; - /** * Associative array in the form of [language][type][format] * @var array>> @@ -38,38 +19,22 @@ public function __construct(array $summaries) $this->summaries = $summaries; } - public function getSummary(string $language, string $type, string $format): string - { - if (!in_array($language, self::LANGUAGES)) { - throw new InvalidArgumentException( - 'Unsupported language ' . $language . '. Allowed values: ' . implode(', ', self::LANGUAGES) - ); - } - - if (!isset($this->summaries[$language])) { - throw new InvalidArgumentException('The language ' . $language . ' is not provided'); - } - - if (!in_array($type, self::TYPES)) { - throw new InvalidArgumentException( - 'Unsupported type ' . $type . '. Allowed values: ' . implode(', ', self::TYPES) - ); - } - - if (!isset($this->summaries[$language][$type])) { - throw new InvalidArgumentException('The type ' . $type . ' is not provided'); + public function getSummary( + CalendarSummaryLanguage $language, + CalendarSummaryFormat $format + ): string { + if (!isset($this->summaries[$language->getValue()])) { + throw new InvalidArgumentException('The language ' . $language->getValue() . ' is not provided'); } - if (!in_array($format, self::FORMATS)) { - throw new InvalidArgumentException( - 'Unsupported format ' . $format . '. Allowed values: ' . implode(', ', self::FORMATS) - ); + if (!isset($this->summaries[$language->getValue()][$format->getType()])) { + throw new InvalidArgumentException('The type ' . $format->getType() . ' is not provided'); } - if (!isset($this->summaries[$language][$type][$format])) { - throw new InvalidArgumentException('The format ' . $format . ' is not provided'); + if (!isset($this->summaries[$language->getValue()][$format->getType()][$format->getSize()])) { + throw new InvalidArgumentException('The size ' . $format->getSize() . ' is not provided'); } - return $this->summaries[$language][$type][$format]; + return $this->summaries[$language->getValue()][$format->getType()][$format->getSize()]; } } diff --git a/src/ValueObjects/CalendarSummaryFormat.php b/src/ValueObjects/CalendarSummaryFormat.php new file mode 100644 index 00000000..7f69e12b --- /dev/null +++ b/src/ValueObjects/CalendarSummaryFormat.php @@ -0,0 +1,65 @@ +type = $type; + $this->size = $size; + } + + public function getType(): string + { + return $this->type; + } + + public function getSize(): string + { + return $this->size; + } + + public function getCombined(): string + { + return $this->size . '-' . $this->type; + } +} diff --git a/src/ValueObjects/CalendarSummaryLanguage.php b/src/ValueObjects/CalendarSummaryLanguage.php new file mode 100644 index 00000000..11be11cd --- /dev/null +++ b/src/ValueObjects/CalendarSummaryLanguage.php @@ -0,0 +1,58 @@ +value = $value; + } + + public static function nl(): self + { + return new self('nl'); + } + + public static function fr(): self + { + return new self('fr'); + } + + public static function de(): self + { + return new self('de'); + } + + public static function en(): self + { + return new self('en'); + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/test/SearchQueryTest.php b/test/SearchQueryTest.php index ff0fd8bb..d9fb4fd6 100644 --- a/test/SearchQueryTest.php +++ b/test/SearchQueryTest.php @@ -4,9 +4,11 @@ namespace CultuurNet\SearchV3; +use CultuurNet\SearchV3\Parameter\CalendarSummary; use CultuurNet\SearchV3\Parameter\Facet; use CultuurNet\SearchV3\Parameter\Id; use CultuurNet\SearchV3\Parameter\Label; +use CultuurNet\SearchV3\ValueObjects\CalendarSummaryFormat; use PHPUnit\Framework\TestCase; final class SearchQueryTest extends TestCase @@ -48,6 +50,9 @@ public function setUp(): void $this->searchQuery->addParameter($this->label); $this->searchQuery->addParameter($this->facet); + $this->searchQuery->addParameter(new CalendarSummary(new CalendarSummaryFormat('html', 'md'))); + $this->searchQuery->addParameter(new CalendarSummary(new CalendarSummaryFormat('text', 'lg'))); + $this->searchQuery->addSort($this->sorting[0], $this->sorting[1]); $this->searchQuery->setEmbed(true); $this->searchQuery->setStart(10); @@ -122,6 +127,10 @@ public function testToArrayMethod(): void 'limit' => 50, 'labels' => 'test-label', 'facets' => 'regions', + 'embedCalendarSummaries' => [ + 'md-html', + 'lg-text', + ], ]; $result = $this->searchQuery->toArray(); @@ -145,6 +154,10 @@ public function testToArrayMethodWithDuplicateParameterKeys(): void 'test-label2', ], 'facets' => 'regions', + 'embedCalendarSummaries' => [ + 'md-html', + 'lg-text', + ], ]; $result = $this->searchQuery->toArray(); @@ -168,6 +181,10 @@ public function testToArrayMethodWithDuplicateFacetKeys(): void 'regions', 'types', ], + 'embedCalendarSummaries' => [ + 'md-html', + 'lg-text', + ], ]; $result = $this->searchQuery->toArray(); @@ -177,7 +194,7 @@ public function testToArrayMethodWithDuplicateFacetKeys(): void public function testToStringMethod(): void { - $expectedQueryString = 'labels=test-label&facets=regions&sort[title]=asc&embed=1&start=10&limit=50'; + $expectedQueryString = 'labels=test-label&facets=regions&embedCalendarSummaries[0]=md-html&embedCalendarSummaries[1]=lg-text&sort[title]=asc&embed=1&start=10&limit=50'; $result = $this->searchQuery->__toString(); $this->assertEquals($result, $expectedQueryString); diff --git a/test/ValueObjects/CalendarSummaryFormatTest.php b/test/ValueObjects/CalendarSummaryFormatTest.php new file mode 100644 index 00000000..047421d5 --- /dev/null +++ b/test/ValueObjects/CalendarSummaryFormatTest.php @@ -0,0 +1,31 @@ +expectException(InvalidArgumentException::class); + + new CalendarSummaryFormat('markdown', 'sm'); + } + + /** + * @test + */ + public function it_throws_on_unsupported_size(): void + { + $this->expectException(InvalidArgumentException::class); + + new CalendarSummaryFormat('html', 'xl'); + } +} diff --git a/test/ValueObjects/CalendarSummaryLanguageTest.php b/test/ValueObjects/CalendarSummaryLanguageTest.php new file mode 100644 index 00000000..00f16970 --- /dev/null +++ b/test/ValueObjects/CalendarSummaryLanguageTest.php @@ -0,0 +1,21 @@ +expectException(InvalidArgumentException::class); + + new CalendarSummaryLanguage('es'); + } +} diff --git a/test/ValueObjects/CalendarSummaryTest.php b/test/ValueObjects/CalendarSummaryTest.php index 94226518..04d5bd0e 100644 --- a/test/ValueObjects/CalendarSummaryTest.php +++ b/test/ValueObjects/CalendarSummaryTest.php @@ -54,40 +54,13 @@ public function it_returns_summaries_by_language_and_type_and_format(): void { $this->assertEquals( 'nl-text-xs', - $this->calendarSummary->getSummary('nl', 'text', 'xs') + $this->calendarSummary->getSummary( + CalendarSummaryLanguage::nl(), + new CalendarSummaryFormat('text', 'xs') + ) ); } - /** - * @test - */ - public function it_throws_on_unsupported_language(): void - { - $this->expectException(InvalidArgumentException::class); - - $this->calendarSummary->getSummary('es', 'text', 'sm'); - } - - /** - * @test - */ - public function it_throws_on_unsupported_type(): void - { - $this->expectException(InvalidArgumentException::class); - - $this->calendarSummary->getSummary('nl', 'markdown', 'sm'); - } - - /** - * @test - */ - public function it_throws_on_unsupported_format(): void - { - $this->expectException(InvalidArgumentException::class); - - $this->calendarSummary->getSummary('nl', 'text', 'xl'); - } - /** * @test */ @@ -97,7 +70,10 @@ public function it_throws_when_language_is_not_provided(): void $calendarSummary = new CalendarSummary([]); - $calendarSummary->getSummary('de', 'html', 'md'); + $calendarSummary->getSummary( + CalendarSummaryLanguage::de(), + new CalendarSummaryFormat('html', 'md') + ); } /** @@ -107,7 +83,10 @@ public function it_throws_when_type_is_not_provided(): void { $this->expectException(InvalidArgumentException::class); - $this->calendarSummary->getSummary('de', 'html', 'md'); + $this->calendarSummary->getSummary( + CalendarSummaryLanguage::de(), + new CalendarSummaryFormat('html', 'md') + ); } /** @@ -117,6 +96,9 @@ public function it_throws_when_format_is_not_provided(): void { $this->expectException(InvalidArgumentException::class); - $this->calendarSummary->getSummary('de', 'text', 'lg'); + $this->calendarSummary->getSummary( + CalendarSummaryLanguage::de(), + new CalendarSummaryFormat('html', 'lg') + ); } }