2024-12-15 04:20:44 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
|
|
|
|
|
|
|
// Files of the project
|
|
|
|
use ${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
|
|
|
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
|
|
|
|
${REPO_OWNER}\${REPO_NAME}\models\connect;
|
|
|
|
|
|
|
|
// Library для ArangoDB
|
|
|
|
use ArangoDBClient\Document as _document;
|
|
|
|
|
|
|
|
// Framework for ArangoDB
|
|
|
|
use mirzaev\arangodb\connection as arangodb,
|
|
|
|
mirzaev\arangodb\document as framework_document,
|
|
|
|
mirzaev\arangodb\collection;
|
|
|
|
|
|
|
|
// Built-in libraries
|
|
|
|
use exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait for implementing a document instance from ArangoDB
|
|
|
|
*
|
|
|
|
* @uses document_interface
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @var protected readonly _document|null $$document An instance of the ArangoDB document
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
|
|
|
*
|
|
|
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
|
|
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
|
|
|
*/
|
|
|
|
trait document
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Document
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @var _document $$document An instance of the document from ArangoDB
|
2024-12-15 04:20:44 +07:00
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
protected readonly _document $$document;
|
2024-12-15 04:20:44 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param bool $$initialize Initialize a model?
|
|
|
|
* @param ?arangodb $$arangodb Instance of a session of ArangoDB
|
|
|
|
* @param _document|null|false $$document An instance of the ArangoDB document
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2024-12-15 22:16:01 +07:00
|
|
|
bool $$initialize = true,
|
|
|
|
?arangodb $$arangodb = null,
|
|
|
|
_document|null|false $$document = false
|
2024-12-15 04:20:44 +07:00
|
|
|
) {
|
|
|
|
// For the extends system
|
2024-12-15 22:16:01 +07:00
|
|
|
parent::__construct($$initialize, $$arangodb);
|
2024-12-15 04:20:44 +07:00
|
|
|
|
|
|
|
// Writing to the property
|
2024-12-15 22:16:01 +07:00
|
|
|
if ($$document instanceof _document) $$this->__document($$document);
|
|
|
|
else if ($$document === null) throw new exception('Failed to initialize an instance of the document from ArangoDB');
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write or read document
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param _document|null $$document Instance of document from ArangoDB
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return _document|null Instance of document from ArangoDB
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function __document(?_document $$document = null): ?_document
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
// Writing a property storing a document instance to ArangoDB
|
2024-12-15 22:16:01 +07:00
|
|
|
if ($$document) $$this->document ??= $$document;
|
2024-12-15 04:20:44 +07:00
|
|
|
|
|
|
|
// Read a property storing a document instance to ArangoDB and exit (success)
|
2024-12-15 22:16:01 +07:00
|
|
|
return $$this->document ?? null;
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param collecton_interface $$document Document
|
|
|
|
* @param array &$$errors Registry of errors
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return string|null The identifier of the created edge of the "connect" collection, if created
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function connect(collection_interface $$document, array &$$errors = []): ?string
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
try {
|
2024-12-15 22:16:01 +07:00
|
|
|
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $$errors)) {
|
|
|
|
if (collection::initialize(connect::COLLECTION, connect::TYPE, errors: $$errors)) {
|
|
|
|
if (collection::initialize($$document::COLLECTION, $$document::TYPE, errors: $$errors)) {
|
2024-12-15 04:20:44 +07:00
|
|
|
// Initialized collections
|
|
|
|
|
2024-12-15 22:16:01 +07:00
|
|
|
if ($$this->document instanceof _document) {
|
2024-12-15 04:20:44 +07:00
|
|
|
// Initialized instance of the document from ArangoDB
|
|
|
|
|
|
|
|
// Writing document and exit (success)
|
|
|
|
return framework_document::write(
|
|
|
|
connect::COLLECTION,
|
|
|
|
[
|
2024-12-15 22:16:01 +07:00
|
|
|
'_from' => $$document->getId(),
|
|
|
|
'_to' => $$this->document->getId()
|
2024-12-15 04:20:44 +07:00
|
|
|
],
|
2024-12-15 22:16:01 +07:00
|
|
|
errors: $$errors
|
2024-12-15 04:20:44 +07:00
|
|
|
);
|
|
|
|
} else throw new exception('The instance of the document from ArangoDB is not initialized');
|
2024-12-15 22:16:01 +07:00
|
|
|
} else throw new exception('Failed to initialize ' . $$document::TYPE . ' collection: ' . $$document::COLLECTION);
|
2024-12-15 04:20:44 +07:00
|
|
|
} else throw new exception('Failed to initialize ' . connect::TYPE . ' collection: ' . connect::COLLECTION);
|
|
|
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
2024-12-15 22:16:01 +07:00
|
|
|
} catch (exception $$e) {
|
2024-12-15 04:20:44 +07:00
|
|
|
// Writing to the registry of errors
|
2024-12-15 22:16:01 +07:00
|
|
|
$$errors[] = [
|
|
|
|
'text' => $$e->getMessage(),
|
|
|
|
'file' => $$e->getFile(),
|
|
|
|
'line' => $$e->getLine(),
|
|
|
|
'stack' => $$e->getTrace()
|
2024-12-15 04:20:44 +07:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit (fail)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write
|
|
|
|
*
|
|
|
|
* Write a property into an instance of the ArangoDB document
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param string $$name Name of the property
|
|
|
|
* @param mixed $$value Content of the property
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function __set(string $$name, mixed $$value = null): void
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
// Writing to the property into an instance of the ArangoDB document and exit (success)
|
2024-12-15 22:16:01 +07:00
|
|
|
$$this->document->{$$name} = $$value;
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read
|
|
|
|
*
|
|
|
|
* Read a property from an instance of the ArangoDB docuemnt
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param string $$name Name of the property
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return mixed Content of the property
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function __get(string $$name): mixed
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
// Read a property from an instance of the ArangoDB document and exit (success)
|
2024-12-15 22:16:01 +07:00
|
|
|
return match ($$name) {
|
|
|
|
default => $$this->document->{$$name}
|
2024-12-15 04:20:44 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete
|
|
|
|
*
|
|
|
|
* Deinitialize the property in an instance of the ArangoDB document
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param string $$name Name of the property
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function __unset(string $$name): void
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
// Delete the property in an instance of the ArangoDB document and exit (success)
|
2024-12-15 22:16:01 +07:00
|
|
|
unset($$this->document->{$$name});
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check of initialization
|
|
|
|
*
|
|
|
|
* Check of initialization of the property into an instance of the ArangoDB document
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param string $$name Name of the property
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return bool The property is initialized?
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function __isset(string $$name): bool
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
// Check of initializatio nof the property and exit (success)
|
2024-12-15 22:16:01 +07:00
|
|
|
return isset($$this->document->{$$name});
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a method
|
|
|
|
*
|
|
|
|
* Execute a method from an instance of the ArangoDB document
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param string $$name Name of the method
|
|
|
|
* @param array $$arguments Arguments for the method
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return mixed Result of execution of the method
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
public function __call(string $$name, array $$arguments = []): mixed
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
// Execute the method and exit (success)
|
2024-12-15 22:16:01 +07:00
|
|
|
return method_exists($$this->document, $$name) ? $$this->document->{$$name}($$arguments) ?? null : null;
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
}
|