Init DB library

This commit is contained in:
Eldad Fux committed 2021-05-03 11:28:31 +03:00
1 parent 0aaf504549
commit 3739be31bb
10 files changed
+2158 -314

No files matched your search

+74
View File
@@ -43,6 +43,7 @@ use Appwrite\Utopia\Response\Model\Webhook;
use Appwrite\Utopia\Response\Model\Preferences;
use Appwrite\Utopia\Response\Model\Mock; // Keep last
use stdClass;
use Utopia\Database\Document as DatabaseDocument;
/**
* @method public function setStatusCode(int $code = 200): Response
@@ -275,6 +276,27 @@ class Response extends SwooleResponse
$this->json(!empty($output) ? $output : new stdClass());
}
/**
* Validate response objects and outputs
* the response according to given format type
*
* @param DatabaseDocument $document
* @param string $model
*
* return void
*/
public function dynamic2(DatabaseDocument $document, string $model): void
{
$output = $this->output2($document, $model);
// If filter is set, parse the output
if(self::isFilter()){
$output = self::getFilter()->parse($output, $model);
}
$this->json(!empty($output) ? $output : new stdClass());
}
/**
* Generate valid response object from document data
*
@@ -327,6 +349,58 @@ class Response extends SwooleResponse
return $this->payload;
}
/**
* Generate valid response object from document data
*
* @param DatabaseDocument $document
* @param string $model
*
* return array
*/
public function output2(DatabaseDocument $document, string $model): array
{
$data = $document;
$model = $this->getModel($model);
$output = [];
if ($model->isAny()) {
$this->payload = $document->getArrayCopy();
return $this->payload;
}
foreach ($model->getRules() as $key => $rule) {
if (!$document->isSet($key)) {
if (!is_null($rule['default'])) {
$document->setAttribute($key, $rule['default']);
} else {
throw new Exception('Model '.$model->getName().' is missing response key: '.$key);
}
}
if ($rule['array']) {
if (!is_array($data[$key])) {
throw new Exception($key.' must be an array of type '.$rule['type']);
}
foreach ($data[$key] as &$item) {
if ($item instanceof Document) {
if (!array_key_exists($rule['type'], $this->models)) {
throw new Exception('Missing model for rule: '. $rule['type']);
}
$item = $this->output($item, $rule['type']);
}
}
}
$output[$key] = $data[$key];
}
$this->payload = $output;
return $this->payload;
}
/**
* YAML
*