Skip to content

Commit 506bf34

Browse files
committed
Update Response class to implement PSR7s ResponseInterface
1 parent d535f50 commit 506bf34

File tree

1 file changed

+15
-77
lines changed

1 file changed

+15
-77
lines changed

src/Response/Response.php

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,34 @@
88
*/
99

1010
namespace Joomla\AI\Response;
11-
11+
1212
use Joomla\Filesystem\File;
1313
use Joomla\Filesystem\Folder;
1414
use Joomla\Filesystem\Path;
15+
use Joomla\Http\Response as HttpResponse;
1516

1617
/**
1718
* AI response data object class.
1819
*
1920
* @since __DEPLOY_VERSION__
2021
*/
21-
class Response
22-
{
22+
class Response extends HttpResponse
23+
{
2324
/**
24-
* The content of the response.
25+
* The provider of the response.
2526
*
2627
* @var string
2728
* @since __DEPLOY_VERSION__
2829
*/
29-
private $content;
30-
31-
/**
32-
* The status code of the response.
33-
*
34-
* @var int
35-
* @since __DEPLOY_VERSION__
36-
*/
37-
private $statusCode;
30+
private string $provider;
3831

3932
/**
4033
* The metadata of the response.
4134
*
4235
* @var array
4336
* @since __DEPLOY_VERSION__
4437
*/
45-
private $metadata;
46-
47-
/**
48-
* The provider of the response.
49-
*
50-
* @var string
51-
* @since __DEPLOY_VERSION__
52-
*/
53-
private $provider;
38+
private array $metadata;
5439

5540
/**
5641
* Constructor.
@@ -64,21 +49,25 @@ class Response
6449
*/
6550
public function __construct(string $content, string $provider, array $metadata = [], int $status = 200)
6651
{
67-
$this->content = $content;
52+
parent::__construct('php://memory', $status);
53+
54+
$body = $this->getBody();
55+
$body->write($content);
56+
$body->rewind();
57+
6858
$this->provider = $provider;
6959
$this->metadata = $metadata;
70-
$this->statusCode = $status;
7160
}
7261

7362
/**
7463
* Get the content of the response.
7564
*
76-
* @return string The content of the response.
65+
* @return string
7766
* @since __DEPLOY_VERSION__
7867
*/
7968
public function getContent(): string
8069
{
81-
return $this->content;
70+
return (string) $this->getBody();
8271
}
8372

8473
/**
@@ -191,55 +180,4 @@ public function getProvider(): string
191180
{
192181
return $this->provider;
193182
}
194-
195-
/**
196-
* Get the status code of the response.
197-
*
198-
* @return int The status code of the response.
199-
* @since __DEPLOY_VERSION__
200-
*/
201-
public function getStatusCode(): int
202-
{
203-
return $this->statusCode;
204-
}
205-
206-
/**
207-
* Magic method to access properties of the response object.
208-
*
209-
* @param string $name The name of the property to get.
210-
*
211-
* @return mixed The value of the property.
212-
* @since __DEPLOY_VERSION__
213-
*/
214-
public function __get($name)
215-
{
216-
switch (strtolower($name)) {
217-
case 'content':
218-
return $this->getContent();
219-
220-
case 'metadata':
221-
return $this->getMetadata();
222-
223-
case 'provider':
224-
return $this->getProvider();
225-
226-
case 'statuscode':
227-
return $this->getStatusCode();
228-
229-
default:
230-
$trace = debug_backtrace();
231-
232-
trigger_error(
233-
sprintf(
234-
'Undefined property via __get(): %s in %s on line %s',
235-
$name,
236-
$trace[0]['file'],
237-
$trace[0]['line']
238-
),
239-
E_USER_NOTICE
240-
);
241-
242-
break;
243-
}
244-
}
245183
}

0 commit comments

Comments
 (0)