-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFacet.php
More file actions
115 lines (86 loc) · 2.18 KB
/
Facet.php
File metadata and controls
115 lines (86 loc) · 2.18 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace SilverStripe\Discoverer\Query\Facet;
use Exception;
use SilverStripe\Core\Injector\Injectable;
/**
* Class properties need to be public, so that they can be searched within an ArrayList. Getters are also provided, as
* those still work within SS templates
*/
class Facet
{
use Injectable;
public const string TYPE_VALUE = 'VALUE';
public const string TYPE_RANGE = 'RANGE';
public const array TYPES = [
self::TYPE_VALUE,
self::TYPE_RANGE,
];
private ?int $limit = null;
private ?string $name = null;
/**
* Usually a field_name
*/
private ?string $fieldName = null;
/**
* @var FacetRange[]
*/
private array $ranges = [];
private string $type = self::TYPE_VALUE;
public function getLimit(): ?int
{
return $this->limit;
}
public function setLimit(?int $limit): static
{
$this->limit = $limit;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
public function getFieldName(): ?string
{
return $this->fieldName;
}
public function setFieldName(?string $fieldName): static
{
$this->fieldName = $fieldName;
return $this;
}
public function getRanges(): array
{
return $this->ranges;
}
public function addRange(
string|int|float|null $from = null,
string|int|float|null $to = null,
?string $name = null
): static {
// If a range is added, then we'll update the type
$this->type = self::TYPE_RANGE;
$range = FacetRange::create($from, $to, $name);
$this->ranges[] = $range;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
/**
* @throws Exception
*/
public function setType(?string $type): static
{
if (!in_array($type, self::TYPES, true)) {
throw new Exception(sprintf('Invalid type "%s" provided', $type));
}
$this->type = $type;
return $this;
}
}