Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.00% covered (warning)
84.00%
21 / 25
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractResource
84.00% covered (warning)
84.00%
21 / 25
69.23% covered (warning)
69.23%
9 / 13
15.92
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getResourceUri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResourceLabel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOriginalResourceId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLanguageCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLanguageUri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addMetadata
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getMetadataValue
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 getMetadataLiteralValue
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 getMetadata
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addMetadataUri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMetadataUris
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2024 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\Translation\Entity;
24
25use JsonSerializable;
26use oat\tao\model\TaoOntology;
27
28abstract class AbstractResource implements JsonSerializable
29{
30    private array $metadataUris = [
31        TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI,
32        TaoOntology::PROPERTY_LANGUAGE
33    ];
34    private array $metadata = [];
35    private string $resourceUri;
36    private string $resourceLabel;
37
38    public function __construct(string $resourceUri, string $resourceLabel)
39    {
40        $this->resourceUri = $resourceUri;
41        $this->resourceLabel = $resourceLabel;
42    }
43
44    public function getResourceUri(): string
45    {
46        return $this->resourceUri;
47    }
48
49    public function getResourceLabel(): string
50    {
51        return $this->resourceLabel;
52    }
53
54    public function getOriginalResourceId(): ?string
55    {
56        return $this->getMetadataValue(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI);
57    }
58
59    public function getLanguageCode(): ?string
60    {
61        return $this->getMetadataLiteralValue(TaoOntology::PROPERTY_LANGUAGE);
62    }
63
64    public function getLanguageUri(): ?string
65    {
66        return $this->getMetadataValue(TaoOntology::PROPERTY_LANGUAGE);
67    }
68
69    /**
70     * @param string|array|null $value
71     * @param string|null $literal
72     */
73    public function addMetadata(string $uri, $value, $literal): void
74    {
75        $this->metadata[$uri] = [
76            'value' => $value,
77            'literal' => $literal
78        ];
79    }
80
81    public function getMetadataValue(string $metadata): ?string
82    {
83        if (!empty($this->metadata[$metadata])) {
84            return $this->metadata[$metadata]['value'];
85        }
86
87        return null;
88    }
89
90    public function getMetadataLiteralValue(string $metadata): ?string
91    {
92        if (!empty($this->metadata[$metadata])) {
93            return $this->metadata[$metadata]['literal'];
94        }
95
96        return null;
97    }
98
99    public function getMetadata(): array
100    {
101        return $this->metadata;
102    }
103
104    public function addMetadataUri(string $uri): void
105    {
106        $this->metadataUris = array_unique(array_merge($this->metadataUris, [$uri]));
107    }
108
109    public function getMetadataUris(): array
110    {
111        return $this->metadataUris;
112    }
113
114    public function jsonSerialize(): array
115    {
116        return [
117            'resourceUri' => $this->getResourceUri(),
118            'resourceLabel' => $this->getResourceLabel(),
119            'metadata' => $this->metadata,
120        ];
121    }
122}