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
|
2019-09-06 17:04:26 +00:00
|
|
|
* @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-09-06 17:04:26 +00:00
|
|
|
*
|
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;
|
|
|
|
|
|
2019-09-06 17:04:26 +00:00
|
|
|
if (is_null($document->getCollection())) {
|
2019-06-09 11:44:58 +00:00
|
|
|
$this->message = 'Missing collection attribute $collection';
|
2019-09-06 17:04:26 +00:00
|
|
|
|
2019-05-09 06:54:39 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 17:04:26 +00:00
|
|
|
if (!in_array($document->getCollection(), $this->collections)) {
|
2019-06-09 11:44:58 +00:00
|
|
|
$this->message = 'Collection is not allowed';
|
2019-09-06 17:04:26 +00:00
|
|
|
|
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
|
|
|
}
|
2019-09-06 17:04:26 +00:00
|
|
|
}
|