Skip to content

Commit

Permalink
Merge pull request #52 from cultuurnet/feature/WID-377-calendar-summa…
Browse files Browse the repository at this point in the history
…ry-parameter

WID-377 Add calendar summary parameter
  • Loading branch information
LucWollants committed Apr 7, 2021
2 parents d11f9e7 + cdab2e1 commit 28ed66f
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 81 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"CultuurNet\\SearchV3\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"CultuurNet\\SearchV3\\": "test"
}
},
"require": {
"php": "^7.1",
"guzzlehttp/guzzle": "~6.0",
Expand Down
36 changes: 36 additions & 0 deletions src/Parameter/CalendarSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\Parameter;

use CultuurNet\SearchV3\ParameterInterface;
use CultuurNet\SearchV3\ValueObjects\CalendarSummaryFormat;

final class CalendarSummary implements ParameterInterface
{
/**
* @var CalendarSummaryFormat
*/
private $format;

public function __construct(CalendarSummaryFormat $format)
{
$this->format = $format;
}

public function getKey(): string
{
return 'embedCalendarSummaries';
}

public function getValue(): string
{
return $this->format->getCombined();
}

public function allowsMultiple(): bool
{
return true;
}
}
57 changes: 11 additions & 46 deletions src/ValueObjects/CalendarSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,array<string,array<string,string>>>
Expand All @@ -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()];
}
}
65 changes: 65 additions & 0 deletions src/ValueObjects/CalendarSummaryFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\ValueObjects;

use InvalidArgumentException;

final class CalendarSummaryFormat
{
private const TYPES = [
'text',
'html',
];

private const SIZES = [
'xs',
'sm',
'md',
'lg',
];

/**
* @var string
*/
private $type;

/**
* @var string
*/
private $size;

public function __construct(string $type, string $size)
{
if (!in_array($type, self::TYPES)) {
throw new InvalidArgumentException(
'Unsupported type ' . $type . '. Allowed values: ' . implode(', ', self::TYPES)
);
}

if (!in_array($size, self::SIZES)) {
throw new InvalidArgumentException(
'Unsupported size ' . $size . '. Allowed values: ' . implode(', ', self::SIZES)
);
}

$this->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;
}
}
58 changes: 58 additions & 0 deletions src/ValueObjects/CalendarSummaryLanguage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\ValueObjects;

use InvalidArgumentException;

final class CalendarSummaryLanguage
{
private const LANGUAGES = [
'nl',
'fr',
'de',
'en',
];

/**
* @var string
*/
private $value;

public function __construct(string $value)
{
if (!in_array($value, self::LANGUAGES)) {
throw new InvalidArgumentException(
'Unsupported language ' . $value . '. Allowed values: ' . implode(', ', self::LANGUAGES)
);
}

$this->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;
}
}
19 changes: 18 additions & 1 deletion test/SearchQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -122,6 +127,10 @@ public function testToArrayMethod(): void
'limit' => 50,
'labels' => 'test-label',
'facets' => 'regions',
'embedCalendarSummaries' => [
'md-html',
'lg-text',
],
];

$result = $this->searchQuery->toArray();
Expand All @@ -145,6 +154,10 @@ public function testToArrayMethodWithDuplicateParameterKeys(): void
'test-label2',
],
'facets' => 'regions',
'embedCalendarSummaries' => [
'md-html',
'lg-text',
],
];

$result = $this->searchQuery->toArray();
Expand All @@ -168,6 +181,10 @@ public function testToArrayMethodWithDuplicateFacetKeys(): void
'regions',
'types',
],
'embedCalendarSummaries' => [
'md-html',
'lg-text',
],
];

$result = $this->searchQuery->toArray();
Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/ValueObjects/CalendarSummaryFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\ValueObjects;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class CalendarSummaryFormatTest extends TestCase
{
/**
* @test
*/
public function it_throws_on_unsupported_type(): void
{
$this->expectException(InvalidArgumentException::class);

new CalendarSummaryFormat('markdown', 'sm');
}

/**
* @test
*/
public function it_throws_on_unsupported_size(): void
{
$this->expectException(InvalidArgumentException::class);

new CalendarSummaryFormat('html', 'xl');
}
}
21 changes: 21 additions & 0 deletions test/ValueObjects/CalendarSummaryLanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\ValueObjects;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class CalendarSummaryLanguageTest extends TestCase
{
/**
* @test
*/
public function it_throws_on_unsupported_language(): void
{
$this->expectException(InvalidArgumentException::class);

new CalendarSummaryLanguage('es');
}
}
Loading

0 comments on commit 28ed66f

Please sign in to comment.