1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace JSKOS; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Result set in response to query a JSKOS Service. |
7
|
|
|
*/ |
8
|
|
|
class Result extends Set |
9
|
|
|
{ |
10
|
|
|
protected $totalCount = 0; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Create a new Result. |
14
|
|
|
*/ |
15
|
|
|
public function __construct(array $members = []) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($members); |
18
|
|
|
$this->setTotalCount(count($this)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Ignores the argument to ensure that a Result is always closed. |
23
|
|
|
*/ |
24
|
|
|
public function setClosed(bool $closed = true) |
25
|
|
|
{ |
26
|
|
|
$this->closed = true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the total number of members including other pages. May be NULL. |
31
|
|
|
*/ |
32
|
|
|
public function getTotalCount() |
33
|
|
|
{ |
34
|
|
|
return $this->totalCount; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the total number of members including other pages. May be NULL for unknown. |
39
|
|
|
*/ |
40
|
|
|
public function setTotalCount(int $count) |
41
|
|
|
{ |
42
|
|
|
$this->totalCount = max($count, count($this)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Remove total count. |
47
|
|
|
*/ |
48
|
|
|
public function unsetTotalCount() |
49
|
|
|
{ |
50
|
|
|
$this->totalCount = null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Append a Resource and possibly increase totalCount if needed. |
55
|
|
|
*/ |
56
|
|
|
public function append($resource) |
57
|
|
|
{ |
58
|
|
|
parent::append($resource); |
59
|
|
|
if ($this->totalCount !== null) { |
60
|
|
|
$this->setTotalCount($this->totalCount); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Serialize with type and context fields for each member. |
66
|
|
|
*/ |
67
|
|
|
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = NULL) |
68
|
|
|
{ |
69
|
|
|
return array_map(function($m) use ($context, $types) { |
70
|
|
|
return $m->jsonLDSerialize($context, $types ?? true); |
71
|
|
|
}, $this->members); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|