1
0
Fork 0
mirror of synced 2024-06-14 00:34:51 +12:00
appwrite/src/Appwrite/Utopia/Response.php

120 lines
3 KiB
PHP
Raw Normal View History

2020-05-16 23:28:26 +12:00
<?php
2020-06-23 00:17:14 +12:00
namespace Appwrite\Utopia;
2020-05-16 23:28:26 +12:00
use Exception;
2020-06-05 21:53:06 +12:00
use Appwrite\Database\Document;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response\Model\Error;
use Appwrite\Utopia\Response\Model\ErrorDev;
use Appwrite\Utopia\Response\Model\User;
use Appwrite\Utopia\Response\Model\Locale;
2020-05-16 23:28:26 +12:00
use Utopia\Response as UtopiaResponse;
class Response extends UtopiaResponse
{
2020-06-24 03:01:20 +12:00
const MODEL_ERROR = 'error';
const MODEL_ERROR_DEV = 'errorDev';
const MODEL_USER = 'user';
const MODEL_LOCALE = 'locale';
2020-06-05 21:53:06 +12:00
public function __construct()
{
$this
2020-06-24 03:01:20 +12:00
->setModel(new Error())
->setModel(new ErrorDev())
->setModel(new User())
->setModel(new Locale())
2020-06-05 21:53:06 +12:00
;
}
2020-05-16 23:28:26 +12:00
/**
* HTTP content types
*/
const CONTENT_TYPE_YAML = 'application/x-yaml';
2020-06-05 21:53:06 +12:00
/**
* List of defined output objects
*/
2020-06-24 03:01:20 +12:00
protected $models = [];
2020-06-05 21:53:06 +12:00
/**
2020-06-24 03:01:20 +12:00
* Set Model Object
2020-06-05 21:53:06 +12:00
*
* @return self
*/
2020-06-24 03:01:20 +12:00
public function setModel(Model $instance): self
2020-05-16 23:28:26 +12:00
{
2020-06-24 03:01:20 +12:00
$this->models[$instance->getType()] = $instance;
2020-06-05 21:53:06 +12:00
return $this;
}
/**
2020-06-24 03:01:20 +12:00
* Get Model Object
2020-06-05 21:53:06 +12:00
*
2020-06-24 03:01:20 +12:00
* @return Model
2020-06-05 21:53:06 +12:00
*/
2020-06-24 03:01:20 +12:00
public function getModel(string $key): Model
2020-06-05 21:53:06 +12:00
{
2020-06-24 03:01:20 +12:00
if(!isset($this->models[$key])) {
throw new Exception('Undefined model: '.$key);
2020-06-05 21:53:06 +12:00
}
2020-06-24 03:01:20 +12:00
return $this->models[$key];
2020-06-05 21:53:06 +12:00
}
/**
* Validate response objects and outputs
* the response according to given format type
*/
2020-06-24 03:01:20 +12:00
public function dynamic(Document $document, string $model)
2020-06-05 21:53:06 +12:00
{
$data = $document->getArrayCopy();
2020-06-24 03:01:20 +12:00
$model = $this->getModel($model);
2020-06-05 21:53:06 +12:00
$output = [];
2020-06-24 03:01:20 +12:00
foreach($model->getRules() as $key => $rule) {
2020-06-05 21:53:06 +12:00
if(!isset($data[$key])) {
2020-06-23 02:33:37 +12:00
if(!is_null($rule['default'])) {
$data[$key] = $rule['default'];
}
else {
2020-06-24 03:01:20 +12:00
throw new Exception('Missing response key: '.$key);
2020-06-23 02:33:37 +12:00
}
2020-06-05 21:53:06 +12:00
}
2020-06-24 03:01:20 +12:00
if($rule['array'] && !is_array($data[$key])) {
throw new Exception($key.' must be an array of '.$rule['type'].' types');
}
2020-06-05 21:53:06 +12:00
$output[$key] = $data[$key];
}
2020-06-24 03:01:20 +12:00
return $this->json($output);
//return $this->yaml($output);
2020-05-16 23:28:26 +12:00
}
/**
* YAML
*
* This helper is for sending YAML HTTP response.
* It sets relevant content type header ('application/x-yaml') and convert a PHP array ($data) to valid YAML using native yaml_parse
*
* @see https://en.wikipedia.org/wiki/YAML
*
* @param array $data
*/
public function yaml(array $data)
{
if(!extension_loaded('yaml')) {
throw new Exception('Missing yaml extension. Learn more at: https://www.php.net/manual/en/book.yaml.php');
}
$this
->setContentType(Response::CONTENT_TYPE_YAML)
->send(yaml_emit($data, YAML_UTF8_ENCODING))
;
}
}