Allow to get all images except main image (#88)

Relates: #10194
This commit is contained in:
Daniel Siepmann 2022-11-29 11:56:53 +01:00 committed by GitHub
parent 20dc69694d
commit 6c119f28af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 28 deletions

View file

@ -33,14 +33,15 @@ class Media implements TypeInterface
private $serialized;
/**
* @var mixed[]
* @var array[]
*/
private $data;
public function __construct(string $serialized)
{
$this->serialized = $serialized;
$this->data = json_decode($serialized, true);
$data = json_decode($serialized, true);
$this->data = is_array($data) ? $data : [];
}
public function getMainImage(): array
@ -59,11 +60,19 @@ class Media implements TypeInterface
public function getImages(): array
{
return array_filter($this->data, function (array $media) {
return array_filter($this->data, function (array $media): bool {
return $media['type'] === 'image';
});
}
public function getExtraImages(): array
{
return array_filter($this->data, function (array $media): bool {
return $media['type'] === 'image'
&& $media['mainImage'] === false;
});
}
public function __toString(): string
{
return $this->serialized;

View file

@ -30,6 +30,9 @@ Features
* The URL of attractions is now imported and provided to the template.
* Media has a new method ``getExtraImages()`` which will return everything from
``getImages()`` except the ``getMainImage()``.
Fixes
-----

View file

@ -85,4 +85,20 @@ class MediaTest extends TestCase
],
], $subject->getImages());
}
/**
* @test
*/
public function returnsExtraImagesAsArray(): void
{
$subject = new Media('[{"mainImage":false,"type":"image","title":"Erfurt-Dom-und-Severikirche.jpg"},{"mainImage":true,"type":"image","title":"Erfurt-Dom und Severikirche-beleuchtet.jpg"}]');
self::assertSame([
[
'mainImage' => false,
'type' => 'image',
'title' => 'Erfurt-Dom-und-Severikirche.jpg',
],
], $subject->getExtraImages());
}
}

View file

@ -224,31 +224,6 @@ parameters:
count: 1
path: Classes/Domain/Model/Frontend/Address.php
-
message: "#^Cannot access offset 'mainImage' on mixed\\.$#"
count: 1
path: Classes/Domain/Model/Frontend/Media.php
-
message: "#^Cannot access offset 'type' on mixed\\.$#"
count: 1
path: Classes/Domain/Model/Frontend/Media.php
-
message: "#^Method WerkraumMedia\\\\ThueCat\\\\Domain\\\\Model\\\\Frontend\\\\Media\\:\\:getMainImage\\(\\) should return array but returns mixed\\.$#"
count: 1
path: Classes/Domain/Model/Frontend/Media.php
-
message: "#^Parameter \\#2 \\$callback of function array_filter expects callable\\(mixed\\)\\: mixed, Closure\\(array\\)\\: bool given\\.$#"
count: 1
path: Classes/Domain/Model/Frontend/Media.php
-
message: "#^Property WerkraumMedia\\\\ThueCat\\\\Domain\\\\Model\\\\Frontend\\\\Media\\:\\:\\$data \\(array\\) does not accept mixed\\.$#"
count: 1
path: Classes/Domain/Model/Frontend/Media.php
-
message: "#^Method WerkraumMedia\\\\ThueCat\\\\Domain\\\\Model\\\\Frontend\\\\Offers\\:\\:current\\(\\) should return WerkraumMedia\\\\ThueCat\\\\Domain\\\\Model\\\\Frontend\\\\Offer but returns mixed\\.$#"
count: 1