2021-02-16 15:08:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2021-08-10 09:38:59 +02:00
|
|
|
namespace WerkraumMedia\ThueCat\Domain\Model\Frontend;
|
|
|
|
|
2023-08-29 13:54:16 +02:00
|
|
|
use TYPO3\CMS\Core\Resource\FileReference;
|
2021-02-16 15:08:15 +01:00
|
|
|
use TYPO3\CMS\Core\Type\TypeInterface;
|
|
|
|
|
|
|
|
class Media implements TypeInterface
|
|
|
|
{
|
2021-04-13 14:43:04 +02:00
|
|
|
/**
|
2022-11-29 11:56:53 +01:00
|
|
|
* @var array[]
|
2021-04-13 14:43:04 +02:00
|
|
|
*/
|
2023-12-04 15:37:57 +01:00
|
|
|
private readonly array $data;
|
2021-02-16 15:08:15 +01:00
|
|
|
|
2023-08-29 13:54:16 +02:00
|
|
|
/**
|
|
|
|
* @var FileReference[]
|
|
|
|
*/
|
2023-12-04 15:19:20 +01:00
|
|
|
protected array $editorialImages = [];
|
2023-08-29 13:54:16 +02:00
|
|
|
|
2023-12-04 15:37:57 +01:00
|
|
|
public function __construct(
|
|
|
|
private readonly string $serialized
|
|
|
|
) {
|
2023-12-05 08:55:20 +01:00
|
|
|
$data = json_decode($serialized, true);
|
2023-01-04 07:33:48 +01:00
|
|
|
$this->data = $this->prepareData(is_array($data) ? $data : []);
|
2021-02-16 15:08:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getMainImage(): array
|
|
|
|
{
|
|
|
|
foreach ($this->data as $media) {
|
|
|
|
if (
|
|
|
|
$media['type'] === 'image'
|
|
|
|
&& $media['mainImage'] === true
|
|
|
|
) {
|
|
|
|
return $media;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImages(): array
|
|
|
|
{
|
2022-11-29 11:56:53 +01:00
|
|
|
return array_filter($this->data, function (array $media): bool {
|
2021-02-16 15:08:15 +01:00
|
|
|
return $media['type'] === 'image';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-29 11:56:53 +01:00
|
|
|
public function getExtraImages(): array
|
|
|
|
{
|
|
|
|
return array_filter($this->data, function (array $media): bool {
|
|
|
|
return $media['type'] === 'image'
|
|
|
|
&& $media['mainImage'] === false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-29 13:54:16 +02:00
|
|
|
public function getAllImages(): array
|
|
|
|
{
|
|
|
|
return array_merge($this->getEditorialImages(), $this->getImages());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FileReference[]
|
|
|
|
*/
|
|
|
|
public function getEditorialImages(): array
|
|
|
|
{
|
|
|
|
return $this->editorialImages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Only used to set the values while mapping objects.
|
2023-11-30 13:52:23 +01:00
|
|
|
*
|
2023-08-29 13:54:16 +02:00
|
|
|
* @see: AfterObjectThawedHandler
|
|
|
|
*/
|
|
|
|
public function setEditorialImages(array $images): void
|
|
|
|
{
|
|
|
|
$this->editorialImages = $images;
|
|
|
|
}
|
|
|
|
|
2021-02-16 15:08:15 +01:00
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return $this->serialized;
|
|
|
|
}
|
2023-01-04 07:33:48 +01:00
|
|
|
|
|
|
|
private function prepareData(array $data): array
|
|
|
|
{
|
|
|
|
return array_map(function (array $media) {
|
|
|
|
$copyrightAuthor = $media['author'] ?? $media['license']['author'] ?? '';
|
|
|
|
|
|
|
|
if ($copyrightAuthor) {
|
|
|
|
$media['copyrightAuthor'] = $copyrightAuthor;
|
|
|
|
}
|
|
|
|
return $media;
|
|
|
|
}, $data);
|
|
|
|
}
|
2021-02-16 15:08:15 +01:00
|
|
|
}
|