appwrite/src/Database/Validator/Collection.php

55 lines
1.1 KiB
PHP
Raw Normal View History

2019-05-09 06:54:39 +00:00
<?php
namespace Database\Validator;
2019-06-09 11:44:58 +00:00
use Database\Database;
2019-05-09 06:54:39 +00:00
use Database\Document;
2019-06-09 11:44:58 +00:00
class Collection extends Structure
2019-05-09 06:54:39 +00:00
{
/**
2019-06-09 11:44:58 +00:00
* @var string
2019-05-09 06:54:39 +00:00
*/
2019-06-09 11:44:58 +00:00
protected $message = 'Unknown Error';
2019-05-09 06:54:39 +00:00
/**
2019-06-09 11:44:58 +00:00
* @var array
2019-05-09 06:54:39 +00:00
*/
2019-06-09 11:44:58 +00:00
protected $collections = [];
2019-05-09 06:54:39 +00:00
/**
2019-06-09 11:44:58 +00:00
* @param Database $database
* @param array $collections
2019-05-09 06:54:39 +00:00
*/
2019-06-09 11:44:58 +00:00
public function __construct(Database $database, array $collections)
2019-05-09 06:54:39 +00:00
{
2019-06-09 11:44:58 +00:00
$this->collections = $collections;
2019-05-09 06:54:39 +00:00
2019-06-09 11:44:58 +00:00
return parent::__construct($database);
2019-05-09 06:54:39 +00:00
}
/**
2019-06-09 11:44:58 +00:00
* @param Document $document
*
2019-05-09 06:54:39 +00:00
* @return bool
*/
2019-06-09 11:44:58 +00:00
public function isValid($document)
2019-05-09 06:54:39 +00:00
{
2019-06-09 11:44:58 +00:00
$document = (is_array($document)) ? new Document($document) : $document;
if (is_null($document->getCollection())) {
2019-06-09 11:44:58 +00:00
$this->message = 'Missing collection attribute $collection';
2019-05-09 06:54:39 +00:00
return false;
}
if (!in_array($document->getCollection(), $this->collections)) {
2019-06-09 11:44:58 +00:00
$this->message = 'Collection is not allowed';
2019-05-09 06:54:39 +00:00
return false;
}
2019-06-09 11:44:58 +00:00
return parent::isValid($document);
2019-05-09 06:54:39 +00:00
}
}