|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Media\Serialization; |
|
4
|
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Iri\IriGeneratorInterface; |
|
6
|
|
|
use CultuurNet\UDB3\Media\Image; |
|
7
|
|
|
use CultuurNet\UDB3\Media\MediaObject; |
|
8
|
|
|
use CultuurNet\UDB3\Media\Properties\MIMEType; |
|
9
|
|
|
use Symfony\Component\Serializer\Exception\UnsupportedException; |
|
10
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class MediaObjectSerializer implements SerializerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var IriGeneratorInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $iriGenerator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* MediaObjectSerializer constructor. |
|
21
|
|
|
* @param IriGeneratorInterface $iriGenerator |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct( |
|
24
|
|
|
IriGeneratorInterface $iriGenerator |
|
25
|
|
|
) { |
|
26
|
|
|
$this->iriGenerator = $iriGenerator; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param MediaObject|Image $mediaObject |
|
31
|
|
|
* @param string $format |
|
32
|
|
|
* @param array $context |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function serialize($mediaObject, $format, array $context = array()) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!isset($format) || $format !== 'json-ld') { |
|
38
|
|
|
throw new UnsupportedException('Unsupported format, only json-ld is available.'); |
|
39
|
|
|
}; |
|
40
|
|
|
|
|
41
|
|
|
if ($mediaObject instanceof Image) { |
|
42
|
|
|
// Some Image objects have the 'application/octet-stream' mime-type, so we hardcode the @type to |
|
43
|
|
|
// 'schema:ImageObject' to make sure an Image does not get the @type 'schema:mediaObject'. |
|
44
|
|
|
$type = 'schema:ImageObject'; |
|
45
|
|
|
} else { |
|
46
|
|
|
$type = $this->serializeMimeType($mediaObject->getMimeType()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$normalizedData = [ |
|
50
|
|
|
'@id' => $this->iriGenerator->iri($mediaObject->getMediaObjectId()), |
|
51
|
|
|
'@type' => $type, |
|
52
|
|
|
'contentUrl' => (string) $mediaObject->getSourceLocation(), |
|
53
|
|
|
'thumbnailUrl' => (string) $mediaObject->getSourceLocation(), |
|
54
|
|
|
'description' => (string) $mediaObject->getDescription(), |
|
55
|
|
|
'copyrightHolder' => (string) $mediaObject->getCopyrightHolder(), |
|
56
|
|
|
'inLanguage' => (string) $mediaObject->getLanguage(), |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
return $normalizedData; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function serializeMimeType(MIMEType $mimeType) |
|
63
|
|
|
{ |
|
64
|
|
|
$typeParts = explode('/', (string) $mimeType); |
|
65
|
|
|
$type = array_shift($typeParts); |
|
66
|
|
|
|
|
67
|
|
|
if ($type === 'image') { |
|
68
|
|
|
return 'schema:ImageObject'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ((string) $mimeType === 'application/octet-stream') { |
|
72
|
|
|
return 'schema:mediaObject'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new UnsupportedException('Unsupported MIME-type "'. $mimeType .'"'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function deserialize($data, $type, $format, array $context = array()) |
|
79
|
|
|
{ |
|
80
|
|
|
throw new \Exception('Deserialization currently not supported.'); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.