Skip to content

Commit 0b98bb0

Browse files
committed
Allow meta data to be added with the response builder.
Signed-off-by: Jason Lewis <[email protected]>
1 parent eaa735d commit 0b98bb0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/Http/ResponseBuilder.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class ResponseBuilder
4242
*/
4343
protected $serializer;
4444

45+
/**
46+
* Array of meta data.
47+
*
48+
* @var array
49+
*/
50+
protected $meta = [];
51+
4552
/**
4653
* Create a new response builder instance.
4754
*
@@ -134,6 +141,32 @@ public function withError($error, $statusCode)
134141
return $this->setStatusCode($statusCode)->withArray($error);
135142
}
136143

144+
/**
145+
* Add a Fractal meta key and value pair.
146+
*
147+
* @param string $key
148+
* @param mixed $value
149+
* @return \Dingo\Api\Http\ResponseBuilder
150+
*/
151+
public function addMeta($key, $value)
152+
{
153+
$this->meta[$key] = $value;
154+
155+
return $this;
156+
}
157+
158+
/**
159+
* Add a Fractal meta key and value pair.
160+
*
161+
* @param string $key
162+
* @param mixed $value
163+
* @return \Dingo\Api\Http\ResponseBuilder
164+
*/
165+
public function meta($key, $value)
166+
{
167+
return $this->addMeta($key, $value);
168+
}
169+
137170
/**
138171
* Build the response.
139172
*
@@ -145,6 +178,10 @@ protected function build($data)
145178
if ($data instanceof FractalResourceInterface) {
146179
$fractal = $this->resolveFractal();
147180

181+
foreach ($this->meta as $key => $value) {
182+
$data->setMetaValue($key, $value);
183+
}
184+
148185
if ($this->serializer) {
149186
$fractal->setSerializer($this->serializer);
150187
}
@@ -169,6 +206,7 @@ protected function reset()
169206
$this->serializer = null;
170207
$this->statusCode = 200;
171208
$this->headers = [];
209+
$this->meta = [];
172210
}
173211

174212
/**

0 commit comments

Comments
 (0)