2020-06-05 09:53:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-06-22 12:17:14 +00:00
|
|
|
namespace Appwrite\Utopia\Response;
|
2020-06-05 09:53:06 +00:00
|
|
|
|
2021-07-31 19:07:05 +00:00
|
|
|
use Utopia\Database\Document;
|
|
|
|
|
|
2020-06-23 15:01:20 +00:00
|
|
|
abstract class Model
|
2020-06-05 09:53:06 +00:00
|
|
|
{
|
2020-11-07 22:14:36 +00:00
|
|
|
const TYPE_STRING = 'string';
|
|
|
|
|
const TYPE_INTEGER = 'integer';
|
2021-08-27 20:27:59 +00:00
|
|
|
const TYPE_FLOAT = 'double';
|
2020-11-07 22:14:36 +00:00
|
|
|
const TYPE_BOOLEAN = 'boolean';
|
|
|
|
|
const TYPE_JSON = 'json';
|
|
|
|
|
|
2020-11-11 22:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected $none = false;
|
|
|
|
|
|
2020-10-27 00:08:29 +00:00
|
|
|
/**
|
2020-10-29 13:07:56 +00:00
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected $any = false;
|
|
|
|
|
|
2020-11-12 05:12:14 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected $public = true;
|
|
|
|
|
|
2020-10-29 13:07:56 +00:00
|
|
|
/**
|
|
|
|
|
* @var array
|
2020-10-27 00:08:29 +00:00
|
|
|
*/
|
2020-06-05 09:53:06 +00:00
|
|
|
protected $rules = [];
|
|
|
|
|
|
2021-07-31 19:07:05 +00:00
|
|
|
/**
|
|
|
|
|
* Filter Document Structure
|
|
|
|
|
*
|
2021-08-16 22:59:33 +00:00
|
|
|
* @return Document
|
2021-07-31 19:07:05 +00:00
|
|
|
*/
|
|
|
|
|
public function filter(Document $document): Document
|
|
|
|
|
{
|
|
|
|
|
return $document;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 09:53:06 +00:00
|
|
|
/**
|
|
|
|
|
* Get Name
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-07-31 19:07:05 +00:00
|
|
|
abstract public function getName(): string;
|
2020-06-05 09:53:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Collection
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-07-31 19:07:05 +00:00
|
|
|
abstract public function getType(): string;
|
2020-06-05 09:53:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Rules
|
|
|
|
|
*
|
2020-10-27 00:08:29 +00:00
|
|
|
* @return array
|
2020-06-05 09:53:06 +00:00
|
|
|
*/
|
|
|
|
|
public function getRules(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->rules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a New Rule
|
2021-08-27 17:09:56 +00:00
|
|
|
* If rule is an array of documents with varying models
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param array $options
|
2020-06-05 09:53:06 +00:00
|
|
|
*/
|
2021-09-14 08:26:16 +00:00
|
|
|
protected function addRule(string $key, array $options): self
|
2020-06-05 09:53:06 +00:00
|
|
|
{
|
2020-06-22 16:25:01 +00:00
|
|
|
$this->rules[$key] = array_merge([
|
2020-11-07 22:14:36 +00:00
|
|
|
'require' => true,
|
2020-06-22 16:25:01 +00:00
|
|
|
'type' => '',
|
|
|
|
|
'description' => '',
|
|
|
|
|
'default' => null,
|
|
|
|
|
'example' => '',
|
2021-09-14 08:26:16 +00:00
|
|
|
'array' => false
|
2020-06-22 16:25:01 +00:00
|
|
|
], $options);
|
2020-06-05 09:53:06 +00:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2020-10-29 13:07:56 +00:00
|
|
|
|
2020-11-07 22:14:36 +00:00
|
|
|
public function getRequired()
|
|
|
|
|
{
|
|
|
|
|
$list = [];
|
|
|
|
|
|
|
|
|
|
foreach($this->rules as $key => $rule) {
|
|
|
|
|
if(isset($rule['require']) || $rule['require']) {
|
|
|
|
|
$list[] = $key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 22:01:31 +00:00
|
|
|
/**
|
|
|
|
|
* Is None
|
|
|
|
|
*
|
|
|
|
|
* Use to check if response is empty
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isNone(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is Any
|
|
|
|
|
*
|
|
|
|
|
* Use to check if response is a wildcard
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-10-29 13:07:56 +00:00
|
|
|
public function isAny(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->any;
|
|
|
|
|
}
|
2020-11-12 05:12:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is Public
|
|
|
|
|
*
|
|
|
|
|
* Should this model be publicly available in docs and spec files?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isPublic(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->public;
|
|
|
|
|
}
|
2020-06-05 09:53:06 +00:00
|
|
|
}
|