appwrite/src/Appwrite/Database/Validator/DocumentId.php

82 lines
1.4 KiB
PHP
Raw Normal View History

2020-04-22 07:03:34 +00:00
<?php
namespace Appwrite\Database\Validator;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Utopia\Validator;
class DocumentId extends Validator
{
/**
* @var string
*/
protected $message = 'Document not found.';
/**
* @var Database
*/
protected $database = null;
2020-05-04 05:04:30 +00:00
/**
* @var string
*/
protected $collection = '';
2020-04-22 07:03:34 +00:00
/**
* Structure constructor.
*
* @param Database $database
2020-05-04 05:04:30 +00:00
* @param string $collection
2020-04-22 07:03:34 +00:00
*/
2020-05-04 05:04:30 +00:00
public function __construct(Database $database, string $collection = '')
2020-04-22 07:03:34 +00:00
{
$this->database = $database;
2020-05-04 05:04:30 +00:00
$this->collection = $collection;
2020-04-22 07:03:34 +00:00
}
/**
* Get Description.
*
* Returns validator description
*
* @return string
*/
public function getDescription()
{
return $this->message;
}
/**
* Is valid.
*
* Returns true if valid or false if not.
*
* @param $value
*
* @return bool
*/
public function isValid($id)
{
$document = $this->database->getDocument($id);
2020-06-24 21:02:27 +00:00
if (!$document) {
2020-04-22 07:03:34 +00:00
return false;
}
2020-06-24 21:02:27 +00:00
if (!$document instanceof Document) {
2020-04-22 07:03:34 +00:00
return false;
}
2020-06-24 21:02:27 +00:00
if (!$document->getId()) {
2020-04-22 07:03:34 +00:00
return false;
}
2020-06-24 21:02:27 +00:00
if ($document->getCollection() !== $this->collection) {
2020-04-22 07:03:34 +00:00
return false;
}
return true;
}
}