thuecat/Classes/Domain/Import/JsonLD/Parser/Media.php

135 lines
3.8 KiB
PHP
Raw Normal View History

2021-02-16 15:08:15 +01:00
<?php
namespace WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser;
/*
* 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.
*/
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData;
2021-06-08 11:54:20 +02:00
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData\InvalidResponseException;
2021-02-16 15:08:15 +01:00
class Media
{
/**
* @var FetchData
*/
private $fetchData;
2021-02-16 15:08:15 +01:00
public function __construct(
FetchData $fetchData
) {
$this->fetchData = $fetchData;
}
public function get(array $jsonLD): array
{
$media = [];
2021-06-08 11:54:20 +02:00
$media = $this->addMainImage($jsonLD, $media);
$media = $this->addSingleImage($jsonLD, $media);
$media = $this->addImages($jsonLD, $media);
return $media;
}
private function addMainImage(array $jsonLD, array $media): array
{
if (isset($jsonLD['schema:photo']['@id']) === false) {
return $media;
}
try {
2021-02-16 15:08:15 +01:00
$media[] = array_merge(
[
'mainImage' => true,
],
$this->getMedia($jsonLD['schema:photo']['@id'])
);
2021-06-08 11:54:20 +02:00
} catch (InvalidResponseException $e) {
// Nothing todo
2021-02-16 15:08:15 +01:00
}
2021-06-08 11:54:20 +02:00
return $media;
}
private function addSingleImage(array $jsonLD, array $media): array
{
if (isset($jsonLD['schema:image']['@id']) === false) {
return $media;
}
try {
2021-02-16 15:08:15 +01:00
$media[] = array_merge(
[
'mainImage' => false,
],
$this->getMedia($jsonLD['schema:image']['@id'])
);
2021-06-08 11:54:20 +02:00
} catch (InvalidResponseException $e) {
// Nothing todo
2021-02-16 15:08:15 +01:00
}
2021-06-08 11:54:20 +02:00
return $media;
}
private function addImages(array $jsonLD, array $media): array
{
2021-02-16 15:08:15 +01:00
if (
2021-06-08 11:54:20 +02:00
isset($jsonLD['schema:image']) === false
|| isset($jsonLD['schema:image']['@id'])
|| is_array($jsonLD['schema:image']) === false
2021-02-16 15:08:15 +01:00
) {
2021-06-08 11:54:20 +02:00
return $media;
}
foreach ($jsonLD['schema:image'] as $image) {
try {
2021-02-16 15:08:15 +01:00
$media[] = array_merge(
[
'mainImage' => false,
],
$this->getMedia($image['@id'])
);
2021-06-08 11:54:20 +02:00
} catch (InvalidResponseException $e) {
// Nothing todo
2021-02-16 15:08:15 +01:00
}
}
return $media;
}
private function getMedia(string $resourceId): array
{
$jsonLD = $this->fetchData->jsonLDFromUrl($resourceId);
$resource = $jsonLD['@graph'][0] ?? [];
return [
'type' => 'image',
'title' => $resource['schema:name']['@value'] ?? '',
'description' => $resource['schema:description']['@value'] ?? '',
'url' => $resource['schema:url']['@value'] ?? '',
'copyrightYear' => intval($resource['schema:copyrightYear']['@value'] ?? 0),
'license' => [
'type' => $resource['schema:license']['@value'] ?? '',
'author' => $resource['thuecat:licenseAuthor']['@value'] ?? '',
],
];
}
}