Skip to content

Commit 034c32f

Browse files
committed
Added strictly typed getvalue methods
1 parent 698d1e7 commit 034c32f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

src/Response/Result/Hit.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,103 @@ public function getValue(
7878
return $this->source[$key] ?? NULL;
7979
}
8080

81+
82+
public function getStringValue(string $key): string
83+
{
84+
if (
85+
isset($this->source[$key])
86+
&& \is_string($this->source[$key]) === TRUE
87+
) {
88+
return $this->source[$key];
89+
}
90+
91+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException('Value is not string.');
92+
}
93+
94+
95+
public function getStringOrNullValue(string $key): string|null
96+
{
97+
try {
98+
return $this->getStringValue($key);
99+
100+
} catch (\Spameri\ElasticQuery\Exception\InvalidArgumentException $exception) {
101+
return NULL;
102+
}
103+
}
104+
105+
106+
public function getArrayValue(string $key): array
107+
{
108+
if (
109+
isset($this->source[$key])
110+
&& \is_array($this->source[$key]) === TRUE
111+
) {
112+
return $this->source[$key];
113+
}
114+
115+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException('Value is not array.');
116+
}
117+
118+
119+
public function getArrayOrNullValue(string $key): array|null
120+
{
121+
try {
122+
return $this->getArrayValue($key);
123+
124+
} catch (\Spameri\ElasticQuery\Exception\InvalidArgumentException $exception) {
125+
return NULL;
126+
}
127+
}
128+
129+
130+
public function getBoolValue(string $key): bool
131+
{
132+
if (
133+
isset($this->source[$key])
134+
&& \is_bool($this->source[$key]) === TRUE
135+
) {
136+
return $this->source[$key];
137+
}
138+
139+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException('Value is not bool.');
140+
}
141+
142+
143+
public function getBoolOrNullValue(string $key): bool|null
144+
{
145+
try {
146+
return $this->getBoolValue($key);
147+
148+
} catch (\Spameri\ElasticQuery\Exception\InvalidArgumentException $exception) {
149+
return NULL;
150+
}
151+
}
152+
153+
154+
public function getIntegerValue(string $key): int
155+
{
156+
if (
157+
isset($this->source[$key])
158+
&& \is_int($this->source[$key]) === TRUE
159+
) {
160+
return $this->source[$key];
161+
}
162+
163+
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException('Value is not integer.');
164+
}
165+
166+
167+
public function getIntegerOrNullValue(string $key): int|null
168+
{
169+
try {
170+
return $this->getIntegerValue($key);
171+
172+
} catch (\Spameri\ElasticQuery\Exception\InvalidArgumentException $exception) {
173+
return NULL;
174+
}
175+
}
176+
177+
81178
/**
82179
* @phpstan-return mixed
83180
*/

0 commit comments

Comments
 (0)