1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/src/Appwrite/Utopia/Response.php

177 lines
4.7 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;
2020-06-24 19:35:59 +12:00
use Appwrite\Utopia\Response\Model\Session;
2020-06-24 17:14:42 +12:00
use Appwrite\Utopia\Response\Model\Team;
use Appwrite\Utopia\Response\Model\TeamList;
2020-06-24 03:01:20 +12:00
use Appwrite\Utopia\Response\Model\Locale;
use Appwrite\Utopia\Response\Model\Membership;
use Appwrite\Utopia\Response\Model\MembershipList;
2020-05-16 23:28:26 +12:00
use Utopia\Response as UtopiaResponse;
class Response extends UtopiaResponse
{
2020-06-24 06:53:24 +12:00
// General
const MODEL_LOG = 'log'; // - Missing
2020-06-24 03:01:20 +12:00
const MODEL_ERROR = 'error';
const MODEL_ERROR_DEV = 'errorDev';
2020-06-24 17:14:42 +12:00
const MODEL_BASE_LIST = 'baseList';
2020-06-24 06:53:24 +12:00
// Users
2020-06-24 03:01:20 +12:00
const MODEL_USER = 'user';
2020-06-24 06:53:24 +12:00
const MODEL_SESSION = 'session';
const MODEL_TOKEN = 'token'; // - Missing
2020-06-24 06:53:24 +12:00
// Database
const MODEL_COLLECTION = 'collection'; // - Missing
2020-06-24 06:53:24 +12:00
// Locale
2020-06-24 03:01:20 +12:00
const MODEL_LOCALE = 'locale';
const MODEL_COUNTRY = 'country'; // - Missing
const MODEL_CONTINENT = 'continent'; // - Missing
const MODEL_CURRENCY = 'currency'; // - Missing
const MODEL_LANGUAGE = 'langauge'; // - Missing
const MODEL_PHONE = 'phone'; // - Missing
2020-06-24 06:53:24 +12:00
// Storage
const MODEL_FILE = 'file'; // - Missing
const MODEL_BUCKET = 'bucket'; // - Missing
2020-06-24 06:53:24 +12:00
// Teams
const MODEL_TEAM = 'team';
2020-06-24 17:14:42 +12:00
const MODEL_TEAM_LIST = 'teamList';
2020-06-26 08:26:02 +12:00
const MODEL_MEMBERSHIP = 'membership';
const MODEL_MEMBERSHIP_LIST = 'membershipList';
2020-06-05 21:53:06 +12:00
2020-06-27 00:27:58 +12:00
/**
* Response constructor.
*/
2020-07-07 16:40:18 +12:00
public function __construct(int $time = 0)
2020-06-05 21:53:06 +12:00
{
$this
2020-06-24 03:01:20 +12:00
->setModel(new Error())
->setModel(new ErrorDev())
->setModel(new User())
2020-06-24 19:35:59 +12:00
->setModel(new Session())
2020-06-24 03:01:20 +12:00
->setModel(new Locale())
2020-06-24 17:14:42 +12:00
->setModel(new Team())
->setModel(new TeamList())
->setModel(new Membership())
->setModel(new MembershipList())
2020-06-05 21:53:06 +12:00
;
2020-07-05 06:21:42 +12:00
2020-07-07 16:40:18 +12:00
parent::__construct($time);
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
{
2020-06-24 18:05:43 +12:00
return $this->json($this->output($document, $model));
}
/**
* Generate valid response object from document data
*/
protected function output(Document $document, string $model): array
{
$data = $document;
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-24 18:05:43 +12:00
if(!$document->isSet($key)) {
2020-06-23 02:33:37 +12:00
if(!is_null($rule['default'])) {
2020-06-24 18:05:43 +12:00
$document->setAttribute($key, $rule['default']);
2020-06-23 02:33:37 +12:00
}
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 18:05:43 +12:00
if($rule['array']) {
if(!is_array($data[$key])) {
throw new Exception($key.' must be an array of '.$rule['type'].' types');
}
foreach ($data[$key] as &$item) {
if(array_key_exists($rule['type'], $this->models) && $item instanceof Document) {
$item = $this->output($item, $rule['type']);
}
}
2020-06-24 03:01:20 +12:00
}
2020-06-05 21:53:06 +12:00
$output[$key] = $data[$key];
}
2020-06-24 18:05:43 +12:00
return $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))
;
}
}