forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnnotationRequest.php
More file actions
78 lines (64 loc) · 1.94 KB
/
AnnotationRequest.php
File metadata and controls
78 lines (64 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Kitodo\Dlf\Common;
/**
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
class AnnotationRequest
{
/**
* @var string
*/
protected $apiUrl = '';
/**
* @param string $apiUrl The url of the annotation server api.
*/
public function __construct($apiUrl)
{
$this->apiUrl = trim($apiUrl, "/ ");
}
/**
* Requests the annotation server
*
* @param string $url The annotation request url.
* @return array
*/
protected function requestAnnotions($url) : array
{
$jsonld = Helper::getUrl($url);
if ($jsonld) {
$annotationData = json_decode($jsonld, true);
if ($annotationData) {
return $annotationData;
}
}
return [];
}
/**
* Returns all annotations of a document.
*
* @param string $id Document id (purl)
* @return array
*/
public function getAll($id)
{
$annotations = [];
$annotationData = $this->requestAnnotions($this->apiUrl . '?target=' . urlencode($id . '/*'));
if (array_key_exists('first', $annotationData)) {
$annotationPageData = $annotationData['first'];
$annotations = array_merge($annotations, $annotationPageData["items"]);
while (array_key_exists('next', $annotationPageData)) {
$annotationPageData = $this->requestAnnotions($annotationPageData['next']);
if (array_key_exists('items', $annotationPageData)) {
$annotations = array_merge($annotations, $annotationPageData["items"]);
}
}
}
return $annotations;
}
}