pot/author/project/system/models/traits/buffer.php

85 lines
2.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
${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\enumerations\language;
// Library for ArangoDB
use ArangoDBClient\Document as _document;
// Framework for ArangoDB
use mirzaev\arangodb\collection,
mirzaev\arangodb\document;
// Built-in libraries
use exception;
/**
* Buffer
*
* Storage of data in the document from ArangoDB
*
* @uses document
* @uses document_interface
* @uses collection_interface
*
* @param static COLLECTION Name of the collection in ArangoDB
* @param static TYPE Type of the collection in ArangoDB
*
* @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 buffer
{
/**
* Write to buffer of the document
*
2024-12-15 22:16:01 +07:00
* @param array $$data Data for writing (merge)
* @param array &$$errors Registry of errors
*
* @return bool Is data has written into the document from ArangoDB?
*/
2024-12-15 22:16:01 +07:00
public function write(array $$data, array &$$errors = []): bool
{
try {
2024-12-15 22:16:01 +07:00
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $$errors)) {
// Initialized the collection
// Is the instance of the document from ArangoDB are initialized?
2024-12-15 22:16:01 +07:00
if (!isset($$this->document)) throw new exception('The instance of the sessoin document from ArangoDB is not initialized');
// Writing data into buffer of the instance of the document from ArangoDB
2024-12-15 22:16:01 +07:00
$$this->document->buffer = array_replace_recursive($$this->document->buffer ?? [], $$data);
// Is the buffer of the instance of the document from ArangoDB exceed 10 megabytes?
2024-12-15 22:16:01 +07:00
if (mb_strlen(json_encode($$this->document->buffer)) > 10485760) throw new exception('The buffer size exceeds 10 megabytes');
// Serializing parameters
2024-12-15 22:16:01 +07:00
if ($$this->document->language instanceof language) $$this->document->language = $$this->document->language->name;
// Writing to ArangoDB and exit (success)
2024-12-15 22:16:01 +07:00
return document::update($$this->document, errors: $$errors);
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
2024-12-15 22:16:01 +07:00
} catch (exception $$e) {
// 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()
];
}
// Exit (fail)
return false;
}
}