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-09-20 06:33:11 +00:00
|
|
|
* @var array
|
2019-05-09 06:54:39 +00:00
|
|
|
*/
|
2019-09-20 06:33:11 +00:00
|
|
|
protected $collections = [];
|
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-09-20 06:33:11 +00:00
|
|
|
protected $merge = [];
|
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-09-20 06:33:11 +00:00
|
|
|
* @param array $merge
|
2019-05-09 06:54:39 +00:00
|
|
|
*/
|
2019-09-20 06:33:11 +00:00
|
|
|
public function __construct(Database $database, array $collections, array $merge = [])
|
2019-05-09 06:54:39 +00:00
|
|
|
{
|
2019-06-09 11:44:58 +00:00
|
|
|
$this->collections = $collections;
|
2019-09-20 06:33:11 +00:00
|
|
|
$this->merge = $merge;
|
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-09-20 06:33:11 +00:00
|
|
|
$document = new Document(
|
2019-09-30 06:13:40 +00:00
|
|
|
array_merge($this->merge, ($document instanceof Document) ? $document->getArrayCopy() : $document)
|
|
|
|
|
);
|
2019-10-01 04:34:01 +00:00
|
|
|
|
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
|
|
|
}
|