appwrite/src/Appwrite/Utopia/Response/Model.php

138 lines
2.4 KiB
PHP
Raw Normal View History

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;
/**
* @var bool
*/
protected $any = false;
/**
* @var bool
*/
protected $public = true;
/**
* @var array
*/
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
*
* @return array
2020-06-05 09:53:06 +00:00
*/
public function getRules(): array
{
return $this->rules;
}
/**
* Add a New Rule
* 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-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
*/
public function isAny(): bool
{
return $this->any;
}
/**
* 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
}